Skip to content

Commit a80049f

Browse files
compiler-errorslcnr
authored andcommitted
Instantiate predicate binder without recanonicalizing goal in new solver
1 parent 0eb0b8c commit a80049f

21 files changed

+86
-233
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs

+1-16
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

+46-59
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.
@@ -500,63 +497,53 @@ where
500497
fn compute_goal(&mut self, goal: Goal<I, I::Predicate>) -> QueryResult<I> {
501498
let Goal { param_env, predicate } = goal;
502499
let kind = predicate.kind();
503-
if let Some(kind) = kind.no_bound_vars() {
504-
match kind {
505-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
506-
self.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
507-
}
508-
ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
509-
self.compute_host_effect_goal(Goal { param_env, predicate })
510-
}
511-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
512-
self.compute_projection_goal(Goal { param_env, predicate })
513-
}
514-
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
515-
self.compute_type_outlives_goal(Goal { param_env, predicate })
516-
}
517-
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
518-
self.compute_region_outlives_goal(Goal { param_env, predicate })
519-
}
520-
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
521-
self.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
522-
}
523-
ty::PredicateKind::Subtype(predicate) => {
524-
self.compute_subtype_goal(Goal { param_env, predicate })
525-
}
526-
ty::PredicateKind::Coerce(predicate) => {
527-
self.compute_coerce_goal(Goal { param_env, predicate })
528-
}
529-
ty::PredicateKind::DynCompatible(trait_def_id) => {
530-
self.compute_dyn_compatible_goal(trait_def_id)
531-
}
532-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
533-
self.compute_well_formed_goal(Goal { param_env, predicate: term })
534-
}
535-
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
536-
self.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
537-
}
538-
ty::PredicateKind::ConstEquate(_, _) => {
539-
panic!("ConstEquate should not be emitted when `-Znext-solver` is active")
540-
}
541-
ty::PredicateKind::NormalizesTo(predicate) => {
542-
self.compute_normalizes_to_goal(Goal { param_env, predicate })
543-
}
544-
ty::PredicateKind::AliasRelate(lhs, rhs, direction) => self
545-
.compute_alias_relate_goal(Goal {
546-
param_env,
547-
predicate: (lhs, rhs, direction),
548-
}),
549-
ty::PredicateKind::Ambiguous => {
550-
self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
551-
}
500+
self.enter_forall(kind, |ecx, kind| match kind {
501+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
502+
ecx.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
552503
}
553-
} else {
554-
self.enter_forall(kind, |ecx, kind| {
555-
let goal = goal.with(ecx.cx(), ty::Binder::dummy(kind));
556-
ecx.add_goal(GoalSource::InstantiateHigherRanked, goal);
557-
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
558-
})
559-
}
504+
ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
505+
ecx.compute_host_effect_goal(Goal { param_env, predicate })
506+
}
507+
ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
508+
ecx.compute_projection_goal(Goal { param_env, predicate })
509+
}
510+
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
511+
ecx.compute_type_outlives_goal(Goal { param_env, predicate })
512+
}
513+
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
514+
ecx.compute_region_outlives_goal(Goal { param_env, predicate })
515+
}
516+
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
517+
ecx.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
518+
}
519+
ty::PredicateKind::Subtype(predicate) => {
520+
ecx.compute_subtype_goal(Goal { param_env, predicate })
521+
}
522+
ty::PredicateKind::Coerce(predicate) => {
523+
ecx.compute_coerce_goal(Goal { param_env, predicate })
524+
}
525+
ty::PredicateKind::DynCompatible(trait_def_id) => {
526+
ecx.compute_dyn_compatible_goal(trait_def_id)
527+
}
528+
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
529+
ecx.compute_well_formed_goal(Goal { param_env, predicate: arg })
530+
}
531+
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
532+
ecx.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
533+
}
534+
ty::PredicateKind::ConstEquate(_, _) => {
535+
panic!("ConstEquate should not be emitted when `-Znext-solver` is active")
536+
}
537+
ty::PredicateKind::NormalizesTo(predicate) => {
538+
ecx.compute_normalizes_to_goal(Goal { param_env, predicate })
539+
}
540+
ty::PredicateKind::AliasRelate(lhs, rhs, direction) => {
541+
ecx.compute_alias_relate_goal(Goal { param_env, predicate: (lhs, rhs, direction) })
542+
}
543+
ty::PredicateKind::Ambiguous => {
544+
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
545+
}
546+
})
560547
}
561548

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

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

-5
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

+4-3
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

-2
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

+1-1
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

-23
This file was deleted.

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

+1-2
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

-15
This file was deleted.

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

+1-2
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() {}

tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-project.current.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/candidate-from-env-universe-err-project.rs:38:5
2+
--> $DIR/candidate-from-env-universe-err-project.rs:37:5
33
|
44
LL | projection_bound::<T>();
55
| ^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
@@ -13,7 +13,7 @@ LL | fn projection_bound<T: for<'a> Trait<'a, Assoc = usize>>() {}
1313
| ^^^^^^^^^^^^^
1414

1515
error[E0308]: mismatched types
16-
--> $DIR/candidate-from-env-universe-err-project.rs:52:30
16+
--> $DIR/candidate-from-env-universe-err-project.rs:51:30
1717
|
1818
LL | let _higher_ranked_norm: for<'a> fn(<T as Trait<'a>>::Assoc) = |_| ();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
@@ -22,7 +22,7 @@ LL | let _higher_ranked_norm: for<'a> fn(<T as Trait<'a>>::Assoc) = |_| ();
2222
found associated type `<T as Trait<'a>>::Assoc`
2323

2424
error[E0308]: mismatched types
25-
--> $DIR/candidate-from-env-universe-err-project.rs:52:30
25+
--> $DIR/candidate-from-env-universe-err-project.rs:51:30
2626
|
2727
LL | let _higher_ranked_norm: for<'a> fn(<T as Trait<'a>>::Assoc) = |_| ();
2828
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
1-
error[E0277]: the trait bound `for<'a> T: Trait<'a>` is not satisfied
2-
--> $DIR/candidate-from-env-universe-err-project.rs:28:19
3-
|
4-
LL | trait_bound::<T>();
5-
| ^ the trait `for<'a> Trait<'a>` is not implemented for `T`
6-
|
7-
note: required by a bound in `trait_bound`
8-
--> $DIR/candidate-from-env-universe-err-project.rs:17:19
9-
|
10-
LL | fn trait_bound<T: for<'a> Trait<'a>>() {}
11-
| ^^^^^^^^^^^^^^^^^ required by this bound in `trait_bound`
12-
13-
error[E0277]: the trait bound `for<'a> T: Trait<'a>` is not satisfied
14-
--> $DIR/candidate-from-env-universe-err-project.rs:38:24
1+
error[E0271]: type mismatch resolving `<T as Trait<'a>>::Assoc == usize`
2+
--> $DIR/candidate-from-env-universe-err-project.rs:37:24
153
|
164
LL | projection_bound::<T>();
17-
| ^ the trait `for<'a> Trait<'a>` is not implemented for `T`
5+
| ^ type mismatch resolving `<T as Trait<'a>>::Assoc == usize`
6+
|
7+
note: types differ
8+
--> $DIR/candidate-from-env-universe-err-project.rs:14:18
189
|
10+
LL | type Assoc = usize;
11+
| ^^^^^
1912
note: required by a bound in `projection_bound`
20-
--> $DIR/candidate-from-env-universe-err-project.rs:18:24
13+
--> $DIR/candidate-from-env-universe-err-project.rs:18:42
2114
|
2215
LL | fn projection_bound<T: for<'a> Trait<'a, Assoc = usize>>() {}
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `projection_bound`
16+
| ^^^^^^^^^^^^^ required by this bound in `projection_bound`
2417

2518
error: higher-ranked subtype error
26-
--> $DIR/candidate-from-env-universe-err-project.rs:52:30
19+
--> $DIR/candidate-from-env-universe-err-project.rs:51:30
2720
|
2821
LL | let _higher_ranked_norm: for<'a> fn(<T as Trait<'a>>::Assoc) = |_| ();
2922
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3023

3124
error: higher-ranked subtype error
32-
--> $DIR/candidate-from-env-universe-err-project.rs:52:30
25+
--> $DIR/candidate-from-env-universe-err-project.rs:51:30
3326
|
3427
LL | let _higher_ranked_norm: for<'a> fn(<T as Trait<'a>>::Assoc) = |_| ();
3528
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3629
|
3730
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
3831

39-
error: aborting due to 4 previous errors
32+
error: aborting due to 3 previous errors
4033

41-
For more information about this error, try `rustc --explain E0277`.
34+
For more information about this error, try `rustc --explain E0271`.

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ fn projection_bound<T: for<'a> Trait<'a, Assoc = usize>>() {}
2020
// We use a function with a trivial where-bound which is more
2121
// restrictive than the impl.
2222
fn function1<T: Trait<'static>>() {
23-
// err
23+
// ok
2424
//
2525
// Proving `for<'a> T: Trait<'a>` using the where-bound does not
2626
// result in a leak check failure even though it does not apply.
2727
// We prefer env candidates over impl candidatescausing this to succeed.
2828
trait_bound::<T>();
29-
//[next]~^ ERROR the trait bound `for<'a> T: Trait<'a>` is not satisfied
3029
}
3130

3231
fn function2<T: Trait<'static, Assoc = usize>>() {
@@ -36,7 +35,7 @@ fn function2<T: Trait<'static, Assoc = usize>>() {
3635
// does not use the leak check when trying the where-bound, causing us
3736
// to prefer it over the impl, resulting in a placeholder error.
3837
projection_bound::<T>();
39-
//[next]~^ ERROR the trait bound `for<'a> T: Trait<'a>` is not satisfied
38+
//[next]~^ ERROR type mismatch resolving `<T as Trait<'a>>::Assoc == usize`
4039
//[current]~^^ ERROR mismatched types
4140
}
4241

0 commit comments

Comments
 (0)