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

Commit e46662a

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

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

569556
// 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
@@ -221,7 +221,6 @@ impl<'tcx> BestObligation<'tcx> {
221221
nested_goal.source(),
222222
GoalSource::ImplWhereBound
223223
| GoalSource::AliasBoundConstCondition
224-
| GoalSource::InstantiateHigherRanked
225224
| GoalSource::AliasWellFormed
226225
) && nested_goal.result().is_err()
227226
},
@@ -511,10 +510,6 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
511510
));
512511
impl_where_bound_count += 1;
513512
}
514-
// Skip over a higher-ranked predicate.
515-
(_, GoalSource::InstantiateHigherRanked) => {
516-
obligation = self.obligation.clone();
517-
}
518513
(ChildMode::PassThrough, _)
519514
| (_, GoalSource::AliasWellFormed | GoalSource::AliasBoundConstCondition) => {
520515
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)