Skip to content

Commit

Permalink
improve normalization of Pointee::Metadata in next solver
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Jan 30, 2024
1 parent 3b6d00d commit 469f0b8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
59 changes: 42 additions & 17 deletions compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,45 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
) -> QueryResult<'tcx> {
let tcx = ecx.tcx();
ecx.probe_misc_candidate("builtin pointee").enter(|ecx| {
fn handle_product_ty<'tcx>(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, NormalizesTo<'tcx>>,
tail_ty: Ty<'tcx>,
) -> QueryResult<'tcx> {
let tcx = ecx.tcx();

// If `Self: Sized`, then `<Self as Pointee>::Metadata == ()`.
let sized_probe =
ecx.probe_misc_candidate("builtin pointee via sized").enter(|ecx| {
let sized_predicate = ty::TraitRef::from_lang_item(
tcx,
LangItem::Sized,
DUMMY_SP,
[ty::GenericArg::from(goal.predicate.self_ty())],
);
// FIXME(-Znext-solver=coinductive): Should this be `GoalSource::ImplWhereBound`?
ecx.add_goal(GoalSource::Misc, goal.with(tcx, sized_predicate));
ecx.eq(goal.param_env, goal.predicate.term, tcx.types.unit.into())
.expect("expected goal term to be fully unconstrained");
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
});

if let Ok(response) = sized_probe {
return Ok(response);
}

// Otherwise, if `Self: ?Sized`, then normalize from
// `<Self as Pointee>::Metadata` to `<Tail as Pointee>::Metadata`.
let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, None);
let projection_ty = ty::AliasTy::new(tcx, metadata_def_id, [tail_ty]);
let predicate =
ty::ProjectionPredicate { projection_ty, term: goal.predicate.term };

// FIXME(-Znext-solver=coinductive): Should this be `GoalSource::ImplWhereBound`?
ecx.add_goal(GoalSource::Misc, goal.with(tcx, predicate));
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}

let metadata_ty = match goal.predicate.self_ty().kind() {
ty::Bool
| ty::Char
Expand Down Expand Up @@ -423,29 +462,15 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
ty::Adt(def, args) if def.is_struct() => match def.non_enum_variant().tail_opt() {
None => tcx.types.unit,
Some(field_def) => {
let self_ty = field_def.ty(tcx, args);
// FIXME(-Znext-solver=coinductive): Should this be `GoalSource::ImplWhereBound`?
ecx.add_goal(
GoalSource::Misc,
goal.with(tcx, goal.predicate.with_self_ty(tcx, self_ty)),
);
return ecx
.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
let tail_ty = field_def.ty(tcx, args);
return handle_product_ty(ecx, goal, tail_ty);
}
},
ty::Adt(_, _) => tcx.types.unit,

ty::Tuple(elements) => match elements.last() {
None => tcx.types.unit,
Some(&self_ty) => {
// FIXME(-Znext-solver=coinductive): Should this be `GoalSource::ImplWhereBound`?
ecx.add_goal(
GoalSource::Misc,
goal.with(tcx, goal.predicate.with_self_ty(tcx, self_ty)),
);
return ecx
.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
}
Some(&tail_ty) => return handle_product_ty(ecx, goal, tail_ty),
},

ty::Infer(
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/traits/pointee-normalize-equate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// check-pass
// revisions: old next
//[next] compile-flags: -Znext-solver

#![feature(ptr_metadata)]

Expand Down

0 comments on commit 469f0b8

Please sign in to comment.