Skip to content

Commit c1b28c3

Browse files
authored
Rollup merge of #109748 - compiler-errors:new-solver-discr-kind-ice, r=lcnr
Don't ICE on `DiscriminantKind` projection in new solver As title says, since we now actually call `Ty::discriminant_kind` on placeholder types 😃 Also drive-by simplify `Pointee::Metadata` projection logic, and fix the UI test because the `<T as Pointee>::Metadata` tests weren't actually exercising the new projection logic, since we still eagerly normalize (which hits `project.rs` in the old solver) in HIR typeck. r? `@lcnr` tho feel free to re-roll, this pr is very low-priority and not super specific to the new trait solver. Fixes compiler-errors/next-solver-hir-issues#14
2 parents ae9c1bf + 321a5db commit c1b28c3

File tree

5 files changed

+94
-15
lines changed

5 files changed

+94
-15
lines changed

compiler/rustc_trait_selection/src/solve/project_goals.rs

+43-5
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,8 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
344344
LangItem::Sized,
345345
[ty::GenericArg::from(goal.predicate.self_ty())],
346346
));
347-
348347
ecx.add_goal(goal.with(tcx, sized_predicate));
349-
ecx.eq(goal.param_env, goal.predicate.term, tcx.types.unit.into())?;
350-
return ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
348+
tcx.types.unit
351349
}
352350

353351
ty::Adt(def, substs) if def.is_struct() => {
@@ -483,9 +481,49 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
483481
ecx: &mut EvalCtxt<'_, 'tcx>,
484482
goal: Goal<'tcx, Self>,
485483
) -> QueryResult<'tcx> {
486-
let discriminant = goal.predicate.self_ty().discriminant_ty(ecx.tcx());
484+
let self_ty = goal.predicate.self_ty();
485+
let discriminant_ty = match *self_ty.kind() {
486+
ty::Bool
487+
| ty::Char
488+
| ty::Int(..)
489+
| ty::Uint(..)
490+
| ty::Float(..)
491+
| ty::Array(..)
492+
| ty::RawPtr(..)
493+
| ty::Ref(..)
494+
| ty::FnDef(..)
495+
| ty::FnPtr(..)
496+
| ty::Closure(..)
497+
| ty::Infer(ty::IntVar(..) | ty::FloatVar(..))
498+
| ty::Generator(..)
499+
| ty::GeneratorWitness(..)
500+
| ty::GeneratorWitnessMIR(..)
501+
| ty::Never
502+
| ty::Foreign(..)
503+
| ty::Adt(_, _)
504+
| ty::Str
505+
| ty::Slice(_)
506+
| ty::Dynamic(_, _, _)
507+
| ty::Tuple(_)
508+
| ty::Error(_) => self_ty.discriminant_ty(ecx.tcx()),
509+
510+
// We do not call `Ty::discriminant_ty` on alias, param, or placeholder
511+
// types, which return `<self_ty as DiscriminantKind>::Discriminant`
512+
// (or ICE in the case of placeholders). Projecting a type to itself
513+
// is never really productive.
514+
ty::Alias(_, _) | ty::Param(_) | ty::Placeholder(..) => {
515+
return Err(NoSolution);
516+
}
517+
518+
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
519+
| ty::Bound(..) => bug!(
520+
"unexpected self ty `{:?}` when normalizing `<T as DiscriminantKind>::Discriminant`",
521+
goal.predicate.self_ty()
522+
),
523+
};
524+
487525
ecx.probe(|ecx| {
488-
ecx.eq(goal.param_env, goal.predicate.term, discriminant.into())?;
526+
ecx.eq(goal.param_env, goal.predicate.term, discriminant_ty.into())?;
489527
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
490528
})
491529
}
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() {}

tests/ui/traits/new-solver/pointee.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ use std::ptr::{DynMetadata, Pointee};
77
trait Trait<U> {}
88
struct MyDst<T: ?Sized>(T);
99

10-
fn works<T>() {
11-
let _: <T as Pointee>::Metadata = ();
12-
let _: <[T] as Pointee>::Metadata = 1_usize;
13-
let _: <str as Pointee>::Metadata = 1_usize;
14-
let _: <dyn Trait<T> as Pointee>::Metadata = give::<DynMetadata<dyn Trait<T>>>();
15-
let _: <MyDst<T> as Pointee>::Metadata = ();
16-
let _: <((((([u8],),),),),) as Pointee>::Metadata = 1_usize;
17-
}
10+
fn meta_is<T: Pointee<Metadata = U> + ?Sized, U>() {}
1811

19-
fn give<U>() -> U {
20-
loop {}
12+
fn works<T>() {
13+
meta_is::<T, ()>();
14+
meta_is::<[T], usize>();
15+
meta_is::<str, usize>();
16+
meta_is::<dyn Trait<T>, DynMetadata<dyn Trait<T>>>();
17+
meta_is::<MyDst<T>, ()>();
18+
meta_is::<((((([u8],),),),),), usize>();
2119
}
2220

2321
fn main() {}
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() {}
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)