Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d0ca5a5

Browse files
compiler-errorslcnr
authored andcommitted
Instantiate predicate binder without recanonicalizing goal in new solver
1 parent afa833e commit d0ca5a5

21 files changed

+86
-233
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! A utility module to inspect currently ambiguous obligations in the current context.
22
33
use rustc_infer::traits::{self, ObligationCause, PredicateObligations};
4-
use rustc_middle::traits::solve::GoalSource;
54
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
65
use rustc_span::Span;
76
use rustc_trait_selection::solve::inspect::{
@@ -119,21 +118,7 @@ impl<'a, 'tcx> ProofTreeVisitor<'tcx> for NestedObligationsForSelfTy<'a, 'tcx> {
119118
fn visit_goal(&mut self, inspect_goal: &InspectGoal<'_, 'tcx>) {
120119
let tcx = self.fcx.tcx;
121120
let goal = inspect_goal.goal();
122-
if self.fcx.predicate_has_self_ty(goal.predicate, self.self_ty)
123-
// We do not push the instantiated forms of goals as it would cause any
124-
// aliases referencing bound vars to go from having escaping bound vars to
125-
// being able to be normalized to an inference variable.
126-
//
127-
// This is mostly just a hack as arbitrary nested goals could still contain
128-
// such aliases while having a different `GoalSource`. Closure signature inference
129-
// however can't really handle *every* higher ranked `Fn` goal also being present
130-
// in the form of `?c: Fn<(<?x as Trait<'!a>>::Assoc)`.
131-
//
132-
// This also just better matches the behaviour of the old solver where we do not
133-
// encounter instantiated forms of goals, only nested goals that referred to bound
134-
// vars from instantiated goals.
135-
&& !matches!(inspect_goal.source(), GoalSource::InstantiateHigherRanked)
136-
{
121+
if self.fcx.predicate_has_self_ty(goal.predicate, self.self_ty) {
137122
self.obligations_for_self_ty.push(traits::Obligation::new(
138123
tcx,
139124
self.root_cause.clone(),

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,6 @@ where
276276
// corecursive functions as explained in #136824, relating types never
277277
// introduces a constructor which could cause the recursion to be guarded.
278278
GoalSource::TypeRelating => PathKind::Inductive,
279-
// Instantiating a higher ranked goal can never cause the recursion to be
280-
// guarded and is therefore unproductive.
281-
GoalSource::InstantiateHigherRanked => PathKind::Inductive,
282279
// These goal sources are likely unproductive and can be changed to
283280
// `PathKind::Inductive`. Keeping them as unknown until we're confident
284281
// about this and have an example where it is necessary.
@@ -508,63 +505,53 @@ where
508505
fn compute_goal(&mut self, goal: Goal<I, I::Predicate>) -> QueryResult<I> {
509506
let Goal { param_env, predicate } = goal;
510507
let kind = predicate.kind();
511-
if let Some(kind) = kind.no_bound_vars() {
512-
match kind {
513-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
514-
self.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
515-
}
516-
ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
517-
self.compute_host_effect_goal(Goal { param_env, predicate })
518-
}
519-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
520-
self.compute_projection_goal(Goal { param_env, predicate })
521-
}
522-
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
523-
self.compute_type_outlives_goal(Goal { param_env, predicate })
524-
}
525-
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
526-
self.compute_region_outlives_goal(Goal { param_env, predicate })
527-
}
528-
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
529-
self.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
530-
}
531-
ty::PredicateKind::Subtype(predicate) => {
532-
self.compute_subtype_goal(Goal { param_env, predicate })
533-
}
534-
ty::PredicateKind::Coerce(predicate) => {
535-
self.compute_coerce_goal(Goal { param_env, predicate })
536-
}
537-
ty::PredicateKind::DynCompatible(trait_def_id) => {
538-
self.compute_dyn_compatible_goal(trait_def_id)
539-
}
540-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
541-
self.compute_well_formed_goal(Goal { param_env, predicate: term })
542-
}
543-
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
544-
self.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
545-
}
546-
ty::PredicateKind::ConstEquate(_, _) => {
547-
panic!("ConstEquate should not be emitted when `-Znext-solver` is active")
548-
}
549-
ty::PredicateKind::NormalizesTo(predicate) => {
550-
self.compute_normalizes_to_goal(Goal { param_env, predicate })
551-
}
552-
ty::PredicateKind::AliasRelate(lhs, rhs, direction) => self
553-
.compute_alias_relate_goal(Goal {
554-
param_env,
555-
predicate: (lhs, rhs, direction),
556-
}),
557-
ty::PredicateKind::Ambiguous => {
558-
self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
559-
}
508+
self.enter_forall(kind, |ecx, kind| match kind {
509+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
510+
ecx.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
560511
}
561-
} else {
562-
self.enter_forall(kind, |ecx, kind| {
563-
let goal = goal.with(ecx.cx(), ty::Binder::dummy(kind));
564-
ecx.add_goal(GoalSource::InstantiateHigherRanked, goal);
565-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
566-
})
567-
}
512+
ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
513+
ecx.compute_host_effect_goal(Goal { param_env, predicate })
514+
}
515+
ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
516+
ecx.compute_projection_goal(Goal { param_env, predicate })
517+
}
518+
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
519+
ecx.compute_type_outlives_goal(Goal { param_env, predicate })
520+
}
521+
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
522+
ecx.compute_region_outlives_goal(Goal { param_env, predicate })
523+
}
524+
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
525+
ecx.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
526+
}
527+
ty::PredicateKind::Subtype(predicate) => {
528+
ecx.compute_subtype_goal(Goal { param_env, predicate })
529+
}
530+
ty::PredicateKind::Coerce(predicate) => {
531+
ecx.compute_coerce_goal(Goal { param_env, predicate })
532+
}
533+
ty::PredicateKind::DynCompatible(trait_def_id) => {
534+
ecx.compute_dyn_compatible_goal(trait_def_id)
535+
}
536+
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
537+
ecx.compute_well_formed_goal(Goal { param_env, predicate: arg })
538+
}
539+
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
540+
ecx.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
541+
}
542+
ty::PredicateKind::ConstEquate(_, _) => {
543+
panic!("ConstEquate should not be emitted when `-Znext-solver` is active")
544+
}
545+
ty::PredicateKind::NormalizesTo(predicate) => {
546+
ecx.compute_normalizes_to_goal(Goal { param_env, predicate })
547+
}
548+
ty::PredicateKind::AliasRelate(lhs, rhs, direction) => {
549+
ecx.compute_alias_relate_goal(Goal { param_env, predicate: (lhs, rhs, direction) })
550+
}
551+
ty::PredicateKind::Ambiguous => {
552+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
553+
}
554+
})
568555
}
569556

570557
// Recursively evaluates all the goals added to this `EvalCtxt` to completion, returning

compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ impl<'tcx> BestObligation<'tcx> {
227227
nested_goal.source(),
228228
GoalSource::ImplWhereBound
229229
| GoalSource::AliasBoundConstCondition
230-
| GoalSource::InstantiateHigherRanked
231230
| GoalSource::AliasWellFormed
232231
) && nested_goal.result().is_err()
233232
},
@@ -517,10 +516,6 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
517516
));
518517
impl_where_bound_count += 1;
519518
}
520-
// Skip over a higher-ranked predicate.
521-
(_, GoalSource::InstantiateHigherRanked) => {
522-
obligation = self.obligation.clone();
523-
}
524519
(ChildMode::PassThrough, _)
525520
| (_, GoalSource::AliasWellFormed | GoalSource::AliasBoundConstCondition) => {
526521
obligation = make_obligation(self.obligation.cause.clone());

compiler/rustc_trait_selection/src/traits/coherence.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,10 @@ impl<'a, 'tcx> ProofTreeVisitor<'tcx> for AmbiguityCausesVisitor<'a, 'tcx> {
661661
// For bound predicates we simply call `infcx.enter_forall`
662662
// and then prove the resulting predicate as a nested goal.
663663
let Goal { param_env, predicate } = goal.goal();
664-
let trait_ref = match predicate.kind().no_bound_vars() {
665-
Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(tr))) => tr.trait_ref,
666-
Some(ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj)))
664+
let predicate_kind = goal.infcx().enter_forall_and_leak_universe(predicate.kind());
665+
let trait_ref = match predicate_kind {
666+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(tr)) => tr.trait_ref,
667+
ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj))
667668
if matches!(
668669
infcx.tcx.def_kind(proj.projection_term.def_id),
669670
DefKind::AssocTy | DefKind::AssocConst

compiler/rustc_type_ir/src/solve/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ pub enum GoalSource {
8080
ImplWhereBound,
8181
/// Const conditions that need to hold for `~const` alias bounds to hold.
8282
AliasBoundConstCondition,
83-
/// Instantiating a higher-ranked goal and re-proving it.
84-
InstantiateHigherRanked,
8583
/// Predicate required for an alias projection to be well-formed.
8684
/// This is used in three places:
8785
/// 1. projecting to an opaque whose hidden type is already registered in

tests/ui/coherence/coherence-overlap-unnormalizable-projection-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | impl<T> Trait for Box<T> {}
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Box<_>`
1313
|
1414
= note: downstream crates may implement trait `WithAssoc<'a>` for type `std::boxed::Box<_>`
15-
= note: downstream crates may implement trait `WhereBound` for type `std::boxed::Box<_>`
15+
= note: downstream crates may implement trait `WhereBound` for type `std::boxed::Box<<std::boxed::Box<_> as WithAssoc<'a>>::Assoc>`
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-1.next.stderr

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ revisions: old next
22
//@[next] compile-flags: -Znext-solver
3-
//@[old] check-pass
3+
//@ check-pass
44

55
// cc #119820
66

@@ -25,7 +25,6 @@ where
2525
// the leak check both candidates may apply and we prefer the
2626
// `param_env` candidate in winnowing.
2727
hr_bound::<&T>();
28-
//[next]~^ ERROR the trait bound `for<'a> &'a &T: Trait` is not satisfied
2928
}
3029

3130
fn main() {}

tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-2.next.stderr

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ revisions: current next
22
//@[next] compile-flags: -Znext-solver
3-
//@[current] check-pass
3+
//@ check-pass
44

55
// cc #119820
66

@@ -13,7 +13,6 @@ fn impl_hr<'b, T: for<'a> Trait<'a, 'b>>() {}
1313

1414
fn not_hr<'a, T: for<'b> Trait<'a, 'b> + OtherTrait<'static>>() {
1515
impl_hr::<T>();
16-
//[next]~^ ERROR the trait bound `for<'a> T: Trait<'a, '_>` is not satisfied
1716
}
1817

1918
fn main() {}

0 commit comments

Comments
 (0)