Skip to content

Commit e7a0eb0

Browse files
compiler-errorslcnr
authored andcommitted
instantiate predicate binder without recanonicalizing goal in new solver
1 parent 4793ef5 commit e7a0eb0

23 files changed

+110
-213
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::Certainty;
@@ -127,21 +126,7 @@ impl<'a, 'tcx> ProofTreeVisitor<'tcx> for NestedObligationsForSelfTy<'a, 'tcx> {
127126

128127
let tcx = self.fcx.tcx;
129128
let goal = inspect_goal.goal();
130-
if self.fcx.predicate_has_self_ty(goal.predicate, self.self_ty)
131-
// We do not push the instantiated forms of goals as it would cause any
132-
// aliases referencing bound vars to go from having escaping bound vars to
133-
// being able to be normalized to an inference variable.
134-
//
135-
// This is mostly just a hack as arbitrary nested goals could still contain
136-
// such aliases while having a different `GoalSource`. Closure signature inference
137-
// however can't really handle *every* higher ranked `Fn` goal also being present
138-
// in the form of `?c: Fn<(<?x as Trait<'!a>>::Assoc)`.
139-
//
140-
// This also just better matches the behaviour of the old solver where we do not
141-
// encounter instantiated forms of goals, only nested goals that referred to bound
142-
// vars from instantiated goals.
143-
&& !matches!(inspect_goal.source(), GoalSource::InstantiateHigherRanked)
144-
{
129+
if self.fcx.predicate_has_self_ty(goal.predicate, self.self_ty) {
145130
self.obligations_for_self_ty.push(traits::Obligation::new(
146131
tcx,
147132
self.root_cause.clone(),

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

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,6 @@ where
265265
// corecursive functions as explained in #136824, relating types never
266266
// introduces a constructor which could cause the recursion to be guarded.
267267
GoalSource::TypeRelating => PathKind::Inductive,
268-
// Instantiating a higher ranked goal can never cause the recursion to be
269-
// guarded and is therefore unproductive.
270-
GoalSource::InstantiateHigherRanked => PathKind::Inductive,
271268
// These goal sources are likely unproductive and can be changed to
272269
// `PathKind::Inductive`. Keeping them as unknown until we're confident
273270
// about this and have an example where it is necessary.
@@ -533,66 +530,56 @@ where
533530
pub(super) fn compute_goal(&mut self, goal: Goal<I, I::Predicate>) -> QueryResult<I> {
534531
let Goal { param_env, predicate } = goal;
535532
let kind = predicate.kind();
536-
if let Some(kind) = kind.no_bound_vars() {
537-
match kind {
538-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
539-
self.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
540-
}
541-
ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
542-
self.compute_host_effect_goal(Goal { param_env, predicate })
543-
}
544-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
545-
self.compute_projection_goal(Goal { param_env, predicate })
546-
}
547-
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
548-
self.compute_type_outlives_goal(Goal { param_env, predicate })
549-
}
550-
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
551-
self.compute_region_outlives_goal(Goal { param_env, predicate })
552-
}
553-
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
554-
self.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
555-
}
556-
ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(symbol)) => {
557-
self.compute_unstable_feature_goal(param_env, symbol)
558-
}
559-
ty::PredicateKind::Subtype(predicate) => {
560-
self.compute_subtype_goal(Goal { param_env, predicate })
561-
}
562-
ty::PredicateKind::Coerce(predicate) => {
563-
self.compute_coerce_goal(Goal { param_env, predicate })
564-
}
565-
ty::PredicateKind::DynCompatible(trait_def_id) => {
566-
self.compute_dyn_compatible_goal(trait_def_id)
567-
}
568-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
569-
self.compute_well_formed_goal(Goal { param_env, predicate: term })
570-
}
571-
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
572-
self.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
573-
}
574-
ty::PredicateKind::ConstEquate(_, _) => {
575-
panic!("ConstEquate should not be emitted when `-Znext-solver` is active")
576-
}
577-
ty::PredicateKind::NormalizesTo(predicate) => {
578-
self.compute_normalizes_to_goal(Goal { param_env, predicate })
579-
}
580-
ty::PredicateKind::AliasRelate(lhs, rhs, direction) => self
581-
.compute_alias_relate_goal(Goal {
582-
param_env,
583-
predicate: (lhs, rhs, direction),
584-
}),
585-
ty::PredicateKind::Ambiguous => {
586-
self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
587-
}
533+
self.enter_forall(kind, |ecx, kind| match kind {
534+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
535+
ecx.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
588536
}
589-
} else {
590-
self.enter_forall(kind, |ecx, kind| {
591-
let goal = goal.with(ecx.cx(), ty::Binder::dummy(kind));
592-
ecx.add_goal(GoalSource::InstantiateHigherRanked, goal);
593-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
594-
})
595-
}
537+
ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
538+
ecx.compute_host_effect_goal(Goal { param_env, predicate })
539+
}
540+
ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
541+
ecx.compute_projection_goal(Goal { param_env, predicate })
542+
}
543+
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
544+
ecx.compute_type_outlives_goal(Goal { param_env, predicate })
545+
}
546+
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
547+
ecx.compute_region_outlives_goal(Goal { param_env, predicate })
548+
}
549+
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
550+
ecx.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
551+
}
552+
ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(symbol)) => {
553+
ecx.compute_unstable_feature_goal(param_env, symbol)
554+
}
555+
ty::PredicateKind::Subtype(predicate) => {
556+
ecx.compute_subtype_goal(Goal { param_env, predicate })
557+
}
558+
ty::PredicateKind::Coerce(predicate) => {
559+
ecx.compute_coerce_goal(Goal { param_env, predicate })
560+
}
561+
ty::PredicateKind::DynCompatible(trait_def_id) => {
562+
ecx.compute_dyn_compatible_goal(trait_def_id)
563+
}
564+
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
565+
ecx.compute_well_formed_goal(Goal { param_env, predicate: term })
566+
}
567+
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
568+
ecx.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
569+
}
570+
ty::PredicateKind::ConstEquate(_, _) => {
571+
panic!("ConstEquate should not be emitted when `-Znext-solver` is active")
572+
}
573+
ty::PredicateKind::NormalizesTo(predicate) => {
574+
ecx.compute_normalizes_to_goal(Goal { param_env, predicate })
575+
}
576+
ty::PredicateKind::AliasRelate(lhs, rhs, direction) => {
577+
ecx.compute_alias_relate_goal(Goal { param_env, predicate: (lhs, rhs, direction) })
578+
}
579+
ty::PredicateKind::Ambiguous => {
580+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
581+
}
582+
})
596583
}
597584

598585
// 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
@@ -229,7 +229,6 @@ impl<'tcx> BestObligation<'tcx> {
229229
nested_goal.source(),
230230
GoalSource::ImplWhereBound
231231
| GoalSource::AliasBoundConstCondition
232-
| GoalSource::InstantiateHigherRanked
233232
| GoalSource::AliasWellFormed
234233
) && nested_goal.result().is_err()
235234
},
@@ -519,10 +518,6 @@ impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
519518
));
520519
impl_where_bound_count += 1;
521520
}
522-
// Skip over a higher-ranked predicate.
523-
(_, GoalSource::InstantiateHigherRanked) => {
524-
obligation = self.obligation.clone();
525-
}
526521
(ChildMode::PassThrough, _)
527522
| (_, GoalSource::AliasWellFormed | GoalSource::AliasBoundConstCondition) => {
528523
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
@@ -688,9 +688,10 @@ impl<'a, 'tcx> ProofTreeVisitor<'tcx> for AmbiguityCausesVisitor<'a, 'tcx> {
688688
// For bound predicates we simply call `infcx.enter_forall`
689689
// and then prove the resulting predicate as a nested goal.
690690
let Goal { param_env, predicate } = goal.goal();
691-
let trait_ref = match predicate.kind().no_bound_vars() {
692-
Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(tr))) => tr.trait_ref,
693-
Some(ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj)))
691+
let predicate_kind = goal.infcx().enter_forall_and_leak_universe(predicate.kind());
692+
let trait_ref = match predicate_kind {
693+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(tr)) => tr.trait_ref,
694+
ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj))
694695
if matches!(
695696
infcx.tcx.def_kind(proj.projection_term.def_id),
696697
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
@@ -78,8 +78,6 @@ pub enum GoalSource {
7878
ImplWhereBound,
7979
/// Const conditions that need to hold for `[const]` alias bounds to hold.
8080
AliasBoundConstCondition,
81-
/// Instantiating a higher-ranked goal and re-proving it.
82-
InstantiateHigherRanked,
8381
/// Predicate required for an alias projection to be well-formed.
8482
/// This is used in three places:
8583
/// 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/crashes/140609.rs renamed to tests/ui/const-generics/generic_const_exprs/negative-coherence-ice-140609.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
//@ known-bug: #140609
1+
//@ check-pass
22
#![feature(with_negative_coherence)]
33
#![feature(generic_const_exprs)]
4+
#![allow(incomplete_features)]
45
#![crate_type = "lib"]
56
trait Trait {}
67
struct A<const B: bool>;

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ revisions: old next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
23
//@[next] compile-flags: -Znext-solver
3-
//@[old] check-pass
4+
//@ check-pass
45

56
// cc #119820
67

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

3131
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.

0 commit comments

Comments
 (0)