Check associated opaque types in check_opaque_types
This commit is contained in:
parent
50c0192c64
commit
b87e8eae29
6 changed files with 58 additions and 27 deletions
|
|
@ -259,7 +259,13 @@ impl UniversalRegionRelationsBuilder<'cx, 'tcx> {
|
|||
.param_env
|
||||
.and(type_op::normalize::Normalize::new(ty))
|
||||
.fully_perform(self.infcx)
|
||||
.unwrap_or_else(|_| bug!("failed to normalize {:?}", ty));
|
||||
.unwrap_or_else(|_| {
|
||||
self.infcx
|
||||
.tcx
|
||||
.sess
|
||||
.delay_span_bug(DUMMY_SP, &format!("failed to normalize {:?}", ty));
|
||||
(self.infcx.tcx.types.err, None)
|
||||
});
|
||||
let constraints2 = self.add_implied_bounds(ty);
|
||||
normalized_inputs_and_output.push(ty);
|
||||
constraints1.into_iter().chain(constraints2)
|
||||
|
|
|
|||
|
|
@ -806,7 +806,11 @@ fn check_where_clauses<'tcx, 'fcx>(
|
|||
|
||||
let mut predicates = predicates.instantiate_identity(fcx.tcx);
|
||||
|
||||
if let Some((return_ty, span)) = return_ty {
|
||||
if let Some((mut return_ty, span)) = return_ty {
|
||||
if return_ty.has_infer_types_or_consts() {
|
||||
fcx.select_obligations_where_possible(false, |_| {});
|
||||
return_ty = fcx.resolve_vars_if_possible(&return_ty);
|
||||
}
|
||||
let opaque_types = check_opaque_types(tcx, fcx, def_id.expect_local(), span, return_ty);
|
||||
for _ in 0..opaque_types.len() {
|
||||
predicates.spans.push(span);
|
||||
|
|
@ -893,10 +897,16 @@ fn check_opaque_types<'fcx, 'tcx>(
|
|||
trace!("check_opaque_types: opaque_ty, {:?}, {:?}", def_id, substs);
|
||||
let generics = tcx.generics_of(def_id);
|
||||
// Only check named `impl Trait` types defined in this crate.
|
||||
// FIXME(eddyb) is `generics.parent.is_none()` correct? It seems
|
||||
// potentially risky wrt associated types in `impl`s.
|
||||
if generics.parent.is_none() && def_id.is_local() {
|
||||
if !def_id.is_local() {
|
||||
return ty;
|
||||
}
|
||||
let opaque_hir_id = tcx.hir().as_local_hir_id(def_id.expect_local());
|
||||
if let hir::ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn: Some(_), .. }) =
|
||||
tcx.hir().expect_item(opaque_hir_id).kind
|
||||
{
|
||||
// Don't check return position impl trait.
|
||||
return ty;
|
||||
}
|
||||
if may_define_opaque_type(tcx, fn_def_id, opaque_hir_id) {
|
||||
trace!("check_opaque_types: may define, generics={:#?}", generics);
|
||||
let mut seen_params: FxHashMap<_, Vec<_>> = FxHashMap::default();
|
||||
|
|
@ -924,9 +934,7 @@ fn check_opaque_types<'fcx, 'tcx>(
|
|||
true
|
||||
}
|
||||
|
||||
GenericArgKind::Const(ct) => {
|
||||
matches!(ct.val, ty::ConstKind::Param(_))
|
||||
}
|
||||
GenericArgKind::Const(ct) => matches!(ct.val, ty::ConstKind::Param(_)),
|
||||
};
|
||||
|
||||
if arg_is_param {
|
||||
|
|
@ -988,7 +996,6 @@ fn check_opaque_types<'fcx, 'tcx>(
|
|||
substituted_predicates.push(substituted_pred);
|
||||
}
|
||||
}
|
||||
} // if is_named_opaque_type
|
||||
} // if let Opaque
|
||||
ty
|
||||
},
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
// Ensure that we don't ICE if associated type impl trait is used in an impl
|
||||
// with an unconstrained type parameter.
|
||||
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
trait X {
|
||||
type I;
|
||||
fn f() -> Self::I;
|
||||
}
|
||||
|
||||
impl<T> X for () {
|
||||
type I = impl Sized;
|
||||
//~^ ERROR could not find defining uses
|
||||
fn f() -> Self::I {}
|
||||
//~^ ERROR type annotations needed
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
error[E0282]: type annotations needed
|
||||
--> $DIR/impl-with-unconstrained-param.rs:14:23
|
||||
|
|
||||
LL | fn f() -> Self::I {}
|
||||
| ^^ cannot infer type for type parameter `T`
|
||||
|
||||
error: could not find defining uses
|
||||
--> $DIR/impl-with-unconstrained-param.rs:12:14
|
||||
|
|
||||
LL | type I = impl Sized;
|
||||
| ^^^^^^^^^^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0282`.
|
||||
|
|
@ -17,11 +17,8 @@ where
|
|||
{
|
||||
type BitsIter = IterBitsIter<T, E, u8>;
|
||||
fn iter_bits(self, n: u8) -> Self::BitsIter {
|
||||
//~^ ERROR non-defining opaque type use in defining scope
|
||||
//~| ERROR non-defining opaque type use in defining scope
|
||||
(0u8..n)
|
||||
.rev()
|
||||
.map(move |shift| ((self >> T::from(shift)) & T::from(1)).try_into().unwrap())
|
||||
//~^ ERROR non-defining opaque type use in defining scope
|
||||
(0u8..n).rev().map(move |shift| ((self >> T::from(shift)) & T::from(1)).try_into().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,3 @@
|
|||
error: non-defining opaque type use in defining scope
|
||||
--> $DIR/issue-60564.rs:19:34
|
||||
|
|
||||
LL | fn iter_bits(self, n: u8) -> Self::BitsIter {
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
note: used non-generic type `_` for generic parameter
|
||||
--> $DIR/issue-60564.rs:8:22
|
||||
|
|
||||
LL | type IterBitsIter<T, E, I> = impl std::iter::Iterator<Item = I>;
|
||||
| ^
|
||||
|
||||
error: non-defining opaque type use in defining scope
|
||||
--> $DIR/issue-60564.rs:19:34
|
||||
|
|
||||
|
|
@ -22,5 +10,5 @@ note: used non-generic type `u8` for generic parameter
|
|||
LL | type IterBitsIter<T, E, I> = impl std::iter::Iterator<Item = I>;
|
||||
| ^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue