Skip to content

Commit f5c78c4

Browse files
Don't ICE on DiscriminantKind projection in new solver
1 parent 17c1167 commit f5c78c4

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

compiler/rustc_trait_selection/src/solve/project_goals.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,49 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
485485
ecx: &mut EvalCtxt<'_, 'tcx>,
486486
goal: Goal<'tcx, Self>,
487487
) -> QueryResult<'tcx> {
488-
let discriminant = goal.predicate.self_ty().discriminant_ty(ecx.tcx());
488+
let self_ty = goal.predicate.self_ty();
489+
let discriminant_ty = match *self_ty.kind() {
490+
ty::Bool
491+
| ty::Char
492+
| ty::Int(..)
493+
| ty::Uint(..)
494+
| ty::Float(..)
495+
| ty::Array(..)
496+
| ty::RawPtr(..)
497+
| ty::Ref(..)
498+
| ty::FnDef(..)
499+
| ty::FnPtr(..)
500+
| ty::Closure(..)
501+
| ty::Infer(ty::IntVar(..) | ty::FloatVar(..))
502+
| ty::Generator(..)
503+
| ty::GeneratorWitness(..)
504+
| ty::GeneratorWitnessMIR(..)
505+
| ty::Never
506+
| ty::Foreign(..)
507+
| ty::Adt(_, _)
508+
| ty::Str
509+
| ty::Slice(_)
510+
| ty::Dynamic(_, _, _)
511+
| ty::Tuple(_)
512+
| ty::Error(_) => self_ty.discriminant_ty(ecx.tcx()),
513+
514+
// We do not call `Ty::discriminant_ty` on alias, param, or placeholder
515+
// types, which return `<self_ty as DiscriminantKind>::Discriminant`
516+
// (or ICE in the case of placeholders). Projecting a type to itself
517+
// is never really productive.
518+
ty::Alias(_, _) | ty::Param(_) | ty::Placeholder(..) => {
519+
return Err(NoSolution);
520+
}
521+
522+
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
523+
| ty::Bound(..) => bug!(
524+
"unexpected self ty `{:?}` when normalizing `<T as DiscriminantKind>::Discriminant`",
525+
goal.predicate.self_ty()
526+
),
527+
};
528+
489529
ecx.probe(|ecx| {
490-
ecx.eq(goal.param_env, goal.predicate.term, discriminant.into())?;
530+
ecx.eq(goal.param_env, goal.predicate.term, discriminant_ty.into())?;
491531
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
492532
})
493533
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// compile-flags: -Ztrait-solver=next
2+
// check-pass
3+
4+
fn foo<T>(x: T) {
5+
std::mem::discriminant(&x);
6+
}
7+
8+
fn main() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// compile-flags: -Ztrait-solver=next
2+
3+
// Check that `<T::Assoc as DiscriminantKind>::Discriminant` doesn't normalize
4+
// to itself and cause overflow/ambiguity.
5+
6+
trait Foo {
7+
type Assoc;
8+
}
9+
10+
trait Bar {}
11+
fn needs_bar(_: impl Bar) {}
12+
13+
fn foo<T: Foo>(x: T::Assoc) {
14+
needs_bar(std::mem::discriminant(&x));
15+
//~^ ERROR the trait bound `Discriminant<<T as Foo>::Assoc>: Bar` is not satisfied
16+
}
17+
18+
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0277]: the trait bound `Discriminant<<T as Foo>::Assoc>: Bar` is not satisfied
2+
--> $DIR/projection-discr-kind.rs:14:15
3+
|
4+
LL | needs_bar(std::mem::discriminant(&x));
5+
| --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `Discriminant<<T as Foo>::Assoc>`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
note: required by a bound in `needs_bar`
10+
--> $DIR/projection-discr-kind.rs:11:22
11+
|
12+
LL | fn needs_bar(_: impl Bar) {}
13+
| ^^^ required by this bound in `needs_bar`
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)