Don't ICE on DiscriminantKind projection in new solver
This commit is contained in:
parent
17c1167216
commit
f5c78c4d3a
@ -485,9 +485,49 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
|
|||||||
ecx: &mut EvalCtxt<'_, 'tcx>,
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
||||||
goal: Goal<'tcx, Self>,
|
goal: Goal<'tcx, Self>,
|
||||||
) -> QueryResult<'tcx> {
|
) -> QueryResult<'tcx> {
|
||||||
let discriminant = goal.predicate.self_ty().discriminant_ty(ecx.tcx());
|
let self_ty = goal.predicate.self_ty();
|
||||||
|
let discriminant_ty = match *self_ty.kind() {
|
||||||
|
ty::Bool
|
||||||
|
| ty::Char
|
||||||
|
| ty::Int(..)
|
||||||
|
| ty::Uint(..)
|
||||||
|
| ty::Float(..)
|
||||||
|
| ty::Array(..)
|
||||||
|
| ty::RawPtr(..)
|
||||||
|
| ty::Ref(..)
|
||||||
|
| ty::FnDef(..)
|
||||||
|
| ty::FnPtr(..)
|
||||||
|
| ty::Closure(..)
|
||||||
|
| ty::Infer(ty::IntVar(..) | ty::FloatVar(..))
|
||||||
|
| ty::Generator(..)
|
||||||
|
| ty::GeneratorWitness(..)
|
||||||
|
| ty::GeneratorWitnessMIR(..)
|
||||||
|
| ty::Never
|
||||||
|
| ty::Foreign(..)
|
||||||
|
| ty::Adt(_, _)
|
||||||
|
| ty::Str
|
||||||
|
| ty::Slice(_)
|
||||||
|
| ty::Dynamic(_, _, _)
|
||||||
|
| ty::Tuple(_)
|
||||||
|
| ty::Error(_) => self_ty.discriminant_ty(ecx.tcx()),
|
||||||
|
|
||||||
|
// We do not call `Ty::discriminant_ty` on alias, param, or placeholder
|
||||||
|
// types, which return `<self_ty as DiscriminantKind>::Discriminant`
|
||||||
|
// (or ICE in the case of placeholders). Projecting a type to itself
|
||||||
|
// is never really productive.
|
||||||
|
ty::Alias(_, _) | ty::Param(_) | ty::Placeholder(..) => {
|
||||||
|
return Err(NoSolution);
|
||||||
|
}
|
||||||
|
|
||||||
|
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
|
||||||
|
| ty::Bound(..) => bug!(
|
||||||
|
"unexpected self ty `{:?}` when normalizing `<T as DiscriminantKind>::Discriminant`",
|
||||||
|
goal.predicate.self_ty()
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
ecx.probe(|ecx| {
|
ecx.probe(|ecx| {
|
||||||
ecx.eq(goal.param_env, goal.predicate.term, discriminant.into())?;
|
ecx.eq(goal.param_env, goal.predicate.term, discriminant_ty.into())?;
|
||||||
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
|
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
8
tests/ui/traits/new-solver/param-discr-kind.rs
Normal file
8
tests/ui/traits/new-solver/param-discr-kind.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
// compile-flags: -Ztrait-solver=next
|
||||||
|
// check-pass
|
||||||
|
|
||||||
|
fn foo<T>(x: T) {
|
||||||
|
std::mem::discriminant(&x);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
18
tests/ui/traits/new-solver/projection-discr-kind.rs
Normal file
18
tests/ui/traits/new-solver/projection-discr-kind.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
// compile-flags: -Ztrait-solver=next
|
||||||
|
|
||||||
|
// Check that `<T::Assoc as DiscriminantKind>::Discriminant` doesn't normalize
|
||||||
|
// to itself and cause overflow/ambiguity.
|
||||||
|
|
||||||
|
trait Foo {
|
||||||
|
type Assoc;
|
||||||
|
}
|
||||||
|
|
||||||
|
trait Bar {}
|
||||||
|
fn needs_bar(_: impl Bar) {}
|
||||||
|
|
||||||
|
fn foo<T: Foo>(x: T::Assoc) {
|
||||||
|
needs_bar(std::mem::discriminant(&x));
|
||||||
|
//~^ ERROR the trait bound `Discriminant<<T as Foo>::Assoc>: Bar` is not satisfied
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
17
tests/ui/traits/new-solver/projection-discr-kind.stderr
Normal file
17
tests/ui/traits/new-solver/projection-discr-kind.stderr
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
error[E0277]: the trait bound `Discriminant<<T as Foo>::Assoc>: Bar` is not satisfied
|
||||||
|
--> $DIR/projection-discr-kind.rs:14:15
|
||||||
|
|
|
||||||
|
LL | needs_bar(std::mem::discriminant(&x));
|
||||||
|
| --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `Discriminant<<T as Foo>::Assoc>`
|
||||||
|
| |
|
||||||
|
| required by a bound introduced by this call
|
||||||
|
|
|
||||||
|
note: required by a bound in `needs_bar`
|
||||||
|
--> $DIR/projection-discr-kind.rs:11:22
|
||||||
|
|
|
||||||
|
LL | fn needs_bar(_: impl Bar) {}
|
||||||
|
| ^^^ required by this bound in `needs_bar`
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0277`.
|
Loading…
x
Reference in New Issue
Block a user