Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4cd157e

Browse files
authoredDec 19, 2024
Rollup merge of #133643 - lcnr:merge-candidates, r=compiler-errors
-Znext-solver: modify candidate preference rules This implements the design proposed in the FCP in #132325 and matches the old solver behavior. I hope the inline comments are all sufficiently clear, I personally think this is a fairly clear improvement over the existing approach using `fn discard_impls_shadowed_by_env`. This fixes rust-lang/trait-system-refactor-initiative#96. This also fixes #133639 which encounters an ICE in negative coherence when evaluating the where-clause. Given the features required to trigger this ICE 🤷 r? ``@compiler-errors``
2 parents e018796 + 5fa4b09 commit 4cd157e

File tree

22 files changed

+332
-181
lines changed

22 files changed

+332
-181
lines changed
 

‎compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ pub struct ParamEnv<'tcx> {
971971
}
972972

973973
impl<'tcx> rustc_type_ir::inherent::ParamEnv<TyCtxt<'tcx>> for ParamEnv<'tcx> {
974-
fn caller_bounds(self) -> impl IntoIterator<Item = ty::Clause<'tcx>> {
974+
fn caller_bounds(self) -> impl inherent::SliceLike<Item = ty::Clause<'tcx>> {
975975
self.caller_bounds()
976976
}
977977
}

‎compiler/rustc_next_trait_solver/src/canonicalizer.rs

Lines changed: 95 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::cmp::Ordering;
33
use rustc_type_ir::data_structures::HashMap;
44
use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
55
use rustc_type_ir::inherent::*;
6+
use rustc_type_ir::solve::{Goal, QueryInput};
67
use rustc_type_ir::visit::TypeVisitableExt;
78
use rustc_type_ir::{
89
self as ty, Canonical, CanonicalTyVarKind, CanonicalVarInfo, CanonicalVarKind, InferCtxtLike,
@@ -17,8 +18,11 @@ use crate::delegate::SolverDelegate;
1718
/// while canonicalizing the response happens in the context of the
1819
/// query.
1920
#[derive(Debug, Clone, Copy)]
20-
pub enum CanonicalizeMode {
21-
Input,
21+
enum CanonicalizeMode {
22+
/// When canonicalizing the `param_env`, we keep `'static` as merging
23+
/// trait candidates relies on it when deciding whether a where-bound
24+
/// is trivial.
25+
Input { keep_static: bool },
2226
/// FIXME: We currently return region constraints referring to
2327
/// placeholders and inference variables from a binder instantiated
2428
/// inside of the query.
@@ -59,15 +63,15 @@ pub struct Canonicalizer<'a, D: SolverDelegate<Interner = I>, I: Interner> {
5963
}
6064

6165
impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
62-
pub fn canonicalize<T: TypeFoldable<I>>(
66+
pub fn canonicalize_response<T: TypeFoldable<I>>(
6367
delegate: &'a D,
64-
canonicalize_mode: CanonicalizeMode,
68+
max_input_universe: ty::UniverseIndex,
6569
variables: &'a mut Vec<I::GenericArg>,
6670
value: T,
6771
) -> ty::Canonical<I, T> {
6872
let mut canonicalizer = Canonicalizer {
6973
delegate,
70-
canonicalize_mode,
74+
canonicalize_mode: CanonicalizeMode::Response { max_input_universe },
7175

7276
variables,
7377
variable_lookup_table: Default::default(),
@@ -80,9 +84,67 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
8084
let value = value.fold_with(&mut canonicalizer);
8185
assert!(!value.has_infer(), "unexpected infer in {value:?}");
8286
assert!(!value.has_placeholders(), "unexpected placeholders in {value:?}");
83-
8487
let (max_universe, variables) = canonicalizer.finalize();
88+
Canonical { max_universe, variables, value }
89+
}
90+
91+
/// When canonicalizing query inputs, we keep `'static` in the `param_env`
92+
/// but erase it everywhere else. We generally don't want to depend on region
93+
/// identity, so while it should not matter whether `'static` is kept in the
94+
/// value or opaque type storage as well, this prevents us from accidentally
95+
/// relying on it in the future.
96+
///
97+
/// We want to keep the option of canonicalizing `'static` to an existential
98+
/// variable in the future by changing the way we detect global where-bounds.
99+
pub fn canonicalize_input<P: TypeFoldable<I>>(
100+
delegate: &'a D,
101+
variables: &'a mut Vec<I::GenericArg>,
102+
input: QueryInput<I, P>,
103+
) -> ty::Canonical<I, QueryInput<I, P>> {
104+
// First canonicalize the `param_env` while keeping `'static`
105+
let mut env_canonicalizer = Canonicalizer {
106+
delegate,
107+
canonicalize_mode: CanonicalizeMode::Input { keep_static: true },
108+
109+
variables,
110+
variable_lookup_table: Default::default(),
111+
primitive_var_infos: Vec::new(),
112+
binder_index: ty::INNERMOST,
113+
114+
cache: Default::default(),
115+
};
116+
let param_env = input.goal.param_env.fold_with(&mut env_canonicalizer);
117+
debug_assert_eq!(env_canonicalizer.binder_index, ty::INNERMOST);
118+
// Then canonicalize the rest of the input without keeping `'static`
119+
// while *mostly* reusing the canonicalizer from above.
120+
let mut rest_canonicalizer = Canonicalizer {
121+
delegate,
122+
canonicalize_mode: CanonicalizeMode::Input { keep_static: false },
123+
124+
variables: env_canonicalizer.variables,
125+
// We're able to reuse the `variable_lookup_table` as whether or not
126+
// it already contains an entry for `'static` does not matter.
127+
variable_lookup_table: env_canonicalizer.variable_lookup_table,
128+
primitive_var_infos: env_canonicalizer.primitive_var_infos,
129+
binder_index: ty::INNERMOST,
85130

131+
// We do not reuse the cache as it may contain entries whose canonicalized
132+
// value contains `'static`. While we could alternatively handle this by
133+
// checking for `'static` when using cached entries, this does not
134+
// feel worth the effort. I do not expect that a `ParamEnv` will ever
135+
// contain large enough types for caching to be necessary.
136+
cache: Default::default(),
137+
};
138+
139+
let predicate = input.goal.predicate.fold_with(&mut rest_canonicalizer);
140+
let goal = Goal { param_env, predicate };
141+
let predefined_opaques_in_body =
142+
input.predefined_opaques_in_body.fold_with(&mut rest_canonicalizer);
143+
let value = QueryInput { goal, predefined_opaques_in_body };
144+
145+
assert!(!value.has_infer(), "unexpected infer in {value:?}");
146+
assert!(!value.has_placeholders(), "unexpected placeholders in {value:?}");
147+
let (max_universe, variables) = rest_canonicalizer.finalize();
86148
Canonical { max_universe, variables, value }
87149
}
88150

@@ -126,7 +188,7 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
126188
// all information which should not matter for the solver.
127189
//
128190
// For this we compress universes as much as possible.
129-
CanonicalizeMode::Input => {}
191+
CanonicalizeMode::Input { .. } => {}
130192
// When canonicalizing a response we map a universes already entered
131193
// by the caller to the root universe and only return useful universe
132194
// information for placeholders and inference variables created inside
@@ -290,17 +352,15 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
290352
}
291353
},
292354
ty::Placeholder(placeholder) => match self.canonicalize_mode {
293-
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderTy(PlaceholderLike::new(
294-
placeholder.universe(),
295-
self.variables.len().into(),
296-
)),
355+
CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderTy(
356+
PlaceholderLike::new(placeholder.universe(), self.variables.len().into()),
357+
),
297358
CanonicalizeMode::Response { .. } => CanonicalVarKind::PlaceholderTy(placeholder),
298359
},
299360
ty::Param(_) => match self.canonicalize_mode {
300-
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderTy(PlaceholderLike::new(
301-
ty::UniverseIndex::ROOT,
302-
self.variables.len().into(),
303-
)),
361+
CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderTy(
362+
PlaceholderLike::new(ty::UniverseIndex::ROOT, self.variables.len().into()),
363+
),
304364
CanonicalizeMode::Response { .. } => panic!("param ty in response: {t:?}"),
305365
},
306366
ty::Bool
@@ -357,29 +417,38 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
357417
let kind = match r.kind() {
358418
ty::ReBound(..) => return r,
359419

360-
// We may encounter `ReStatic` in item signatures or the hidden type
361-
// of an opaque. `ReErased` should only be encountered in the hidden
420+
// We don't canonicalize `ReStatic` in the `param_env` as we use it
421+
// when checking whether a `ParamEnv` candidate is global.
422+
ty::ReStatic => match self.canonicalize_mode {
423+
CanonicalizeMode::Input { keep_static: false } => {
424+
CanonicalVarKind::Region(ty::UniverseIndex::ROOT)
425+
}
426+
CanonicalizeMode::Input { keep_static: true }
427+
| CanonicalizeMode::Response { .. } => return r,
428+
},
429+
430+
// `ReErased` should only be encountered in the hidden
362431
// type of an opaque for regions that are ignored for the purposes of
363432
// captures.
364433
//
365434
// FIXME: We should investigate the perf implications of not uniquifying
366435
// `ReErased`. We may be able to short-circuit registering region
367436
// obligations if we encounter a `ReErased` on one side, for example.
368-
ty::ReStatic | ty::ReErased | ty::ReError(_) => match self.canonicalize_mode {
369-
CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
437+
ty::ReErased | ty::ReError(_) => match self.canonicalize_mode {
438+
CanonicalizeMode::Input { .. } => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
370439
CanonicalizeMode::Response { .. } => return r,
371440
},
372441

373442
ty::ReEarlyParam(_) | ty::ReLateParam(_) => match self.canonicalize_mode {
374-
CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
443+
CanonicalizeMode::Input { .. } => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
375444
CanonicalizeMode::Response { .. } => {
376445
panic!("unexpected region in response: {r:?}")
377446
}
378447
},
379448

380449
ty::RePlaceholder(placeholder) => match self.canonicalize_mode {
381450
// We canonicalize placeholder regions as existentials in query inputs.
382-
CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
451+
CanonicalizeMode::Input { .. } => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
383452
CanonicalizeMode::Response { max_input_universe } => {
384453
// If we have a placeholder region inside of a query, it must be from
385454
// a new universe.
@@ -397,7 +466,9 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
397466
"region vid should have been resolved fully before canonicalization"
398467
);
399468
match self.canonicalize_mode {
400-
CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
469+
CanonicalizeMode::Input { keep_static: _ } => {
470+
CanonicalVarKind::Region(ty::UniverseIndex::ROOT)
471+
}
401472
CanonicalizeMode::Response { .. } => {
402473
CanonicalVarKind::Region(self.delegate.universe_of_lt(vid).unwrap())
403474
}
@@ -434,15 +505,15 @@ impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicaliz
434505
ty::InferConst::Fresh(_) => todo!(),
435506
},
436507
ty::ConstKind::Placeholder(placeholder) => match self.canonicalize_mode {
437-
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderConst(
508+
CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderConst(
438509
PlaceholderLike::new(placeholder.universe(), self.variables.len().into()),
439510
),
440511
CanonicalizeMode::Response { .. } => {
441512
CanonicalVarKind::PlaceholderConst(placeholder)
442513
}
443514
},
444515
ty::ConstKind::Param(_) => match self.canonicalize_mode {
445-
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderConst(
516+
CanonicalizeMode::Input { .. } => CanonicalVarKind::PlaceholderConst(
446517
PlaceholderLike::new(ty::UniverseIndex::ROOT, self.variables.len().into()),
447518
),
448519
CanonicalizeMode::Response { .. } => panic!("param ty in response: {c:?}"),

‎compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

Lines changed: 57 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_type_ir::visit::TypeVisitableExt as _;
1111
use rustc_type_ir::{self as ty, Interner, TypingMode, Upcast as _, elaborate};
1212
use tracing::{debug, instrument};
1313

14+
use super::trait_goals::TraitGoalProvenVia;
1415
use crate::delegate::SolverDelegate;
1516
use crate::solve::inspect::ProbeKind;
1617
use crate::solve::{
@@ -337,15 +338,6 @@ where
337338

338339
self.assemble_param_env_candidates(goal, &mut candidates);
339340

340-
match self.typing_mode() {
341-
TypingMode::Coherence => {}
342-
TypingMode::Analysis { .. }
343-
| TypingMode::PostBorrowckAnalysis { .. }
344-
| TypingMode::PostAnalysis => {
345-
self.discard_impls_shadowed_by_env(goal, &mut candidates);
346-
}
347-
}
348-
349341
candidates
350342
}
351343

@@ -500,7 +492,7 @@ where
500492
goal: Goal<I, G>,
501493
candidates: &mut Vec<Candidate<I>>,
502494
) {
503-
for (i, assumption) in goal.param_env.caller_bounds().into_iter().enumerate() {
495+
for (i, assumption) in goal.param_env.caller_bounds().iter().enumerate() {
504496
candidates.extend(G::probe_and_consider_implied_clause(
505497
self,
506498
CandidateSource::ParamEnv(i),
@@ -733,72 +725,64 @@ where
733725
})
734726
}
735727

736-
/// If there's a where-bound for the current goal, do not use any impl candidates
737-
/// to prove the current goal. Most importantly, if there is a where-bound which does
738-
/// not specify any associated types, we do not allow normalizing the associated type
739-
/// by using an impl, even if it would apply.
728+
/// We sadly can't simply take all possible candidates for normalization goals
729+
/// and check whether they result in the same constraints. We want to make sure
730+
/// that trying to normalize an alias doesn't result in constraints which aren't
731+
/// otherwise required.
732+
///
733+
/// Most notably, when proving a trait goal by via a where-bound, we should not
734+
/// normalize via impls which have stricter region constraints than the where-bound:
735+
///
736+
/// ```rust
737+
/// trait Trait<'a> {
738+
/// type Assoc;
739+
/// }
740+
///
741+
/// impl<'a, T: 'a> Trait<'a> for T {
742+
/// type Assoc = u32;
743+
/// }
744+
///
745+
/// fn with_bound<'a, T: Trait<'a>>(_value: T::Assoc) {}
746+
/// ```
740747
///
741-
/// <https://github.com/rust-lang/trait-system-refactor-initiative/issues/76>
742-
// FIXME(@lcnr): The current structure here makes me unhappy and feels ugly. idk how
743-
// to improve this however. However, this should make it fairly straightforward to refine
744-
// the filtering going forward, so it seems alright-ish for now.
745-
#[instrument(level = "debug", skip(self, goal))]
746-
fn discard_impls_shadowed_by_env<G: GoalKind<D>>(
748+
/// The where-bound of `with_bound` doesn't specify the associated type, so we would
749+
/// only be able to normalize `<T as Trait<'a>>::Assoc` by using the impl. This impl
750+
/// adds a `T: 'a` bound however, which would result in a region error. Given that the
751+
/// user explicitly wrote that `T: Trait<'a>` holds, this is undesirable and we instead
752+
/// treat the alias as rigid.
753+
///
754+
/// See trait-system-refactor-initiative#124 for more details.
755+
#[instrument(level = "debug", skip(self), ret)]
756+
pub(super) fn merge_candidates(
747757
&mut self,
748-
goal: Goal<I, G>,
749-
candidates: &mut Vec<Candidate<I>>,
750-
) {
751-
let cx = self.cx();
752-
let trait_goal: Goal<I, ty::TraitPredicate<I>> =
753-
goal.with(cx, goal.predicate.trait_ref(cx));
754-
755-
let mut trait_candidates_from_env = vec![];
756-
self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
757-
ecx.assemble_param_env_candidates(trait_goal, &mut trait_candidates_from_env);
758-
ecx.assemble_alias_bound_candidates(trait_goal, &mut trait_candidates_from_env);
759-
});
758+
proven_via: Option<TraitGoalProvenVia>,
759+
candidates: Vec<Candidate<I>>,
760+
) -> QueryResult<I> {
761+
let Some(proven_via) = proven_via else {
762+
// We don't care about overflow. If proving the trait goal overflowed, then
763+
// it's enough to report an overflow error for that, we don't also have to
764+
// overflow during normalization.
765+
return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Ambiguity));
766+
};
760767

761-
if !trait_candidates_from_env.is_empty() {
762-
let trait_env_result = self.merge_candidates(trait_candidates_from_env);
763-
match trait_env_result.unwrap().value.certainty {
764-
// If proving the trait goal succeeds by using the env,
765-
// we freely drop all impl candidates.
766-
//
767-
// FIXME(@lcnr): It feels like this could easily hide
768-
// a forced ambiguity candidate added earlier.
769-
// This feels dangerous.
770-
Certainty::Yes => {
771-
candidates.retain(|c| match c.source {
772-
CandidateSource::Impl(_) | CandidateSource::BuiltinImpl(_) => {
773-
debug!(?c, "discard impl candidate");
774-
false
775-
}
776-
CandidateSource::ParamEnv(_) | CandidateSource::AliasBound => true,
777-
CandidateSource::CoherenceUnknowable => panic!("uh oh"),
778-
});
779-
}
780-
// If it is still ambiguous we instead just force the whole goal
781-
// to be ambig and wait for inference constraints. See
782-
// tests/ui/traits/next-solver/env-shadows-impls/ambig-env-no-shadow.rs
783-
Certainty::Maybe(cause) => {
784-
debug!(?cause, "force ambiguity");
785-
*candidates = self.forced_ambiguity(cause).into_iter().collect();
786-
}
787-
}
788-
}
789-
}
768+
let responses: Vec<_> = match proven_via {
769+
// Even when a trait bound has been proven using a where-bound, we
770+
// still need to consider alias-bounds for normalization, see
771+
// tests/ui/next-solver/alias-bound-shadowed-by-env.rs.
772+
//
773+
// FIXME(const_trait_impl): should this behavior also be used by
774+
// constness checking. Doing so is *at least theoretically* breaking,
775+
// see github.com/rust-lang/rust/issues/133044#issuecomment-2500709754
776+
TraitGoalProvenVia::ParamEnv | TraitGoalProvenVia::AliasBound => candidates
777+
.iter()
778+
.filter(|c| {
779+
matches!(c.source, CandidateSource::AliasBound | CandidateSource::ParamEnv(_))
780+
})
781+
.map(|c| c.result)
782+
.collect(),
783+
TraitGoalProvenVia::Misc => candidates.iter().map(|c| c.result).collect(),
784+
};
790785

791-
/// If there are multiple ways to prove a trait or projection goal, we have
792-
/// to somehow try to merge the candidates into one. If that fails, we return
793-
/// ambiguity.
794-
#[instrument(level = "debug", skip(self), ret)]
795-
pub(super) fn merge_candidates(&mut self, candidates: Vec<Candidate<I>>) -> QueryResult<I> {
796-
// First try merging all candidates. This is complete and fully sound.
797-
let responses = candidates.iter().map(|c| c.result).collect::<Vec<_>>();
798-
if let Some(result) = self.try_merge_responses(&responses) {
799-
return Ok(result);
800-
} else {
801-
self.flounder(&responses)
802-
}
786+
self.try_merge_responses(&responses).map_or_else(|| self.flounder(&responses), Ok)
803787
}
804788
}

‎compiler/rustc_next_trait_solver/src/solve/effect_goals.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use rustc_type_ir::fast_reject::DeepRejectCtxt;
55
use rustc_type_ir::inherent::*;
66
use rustc_type_ir::lang_items::TraitSolverLangItem;
7+
use rustc_type_ir::solve::inspect::ProbeKind;
78
use rustc_type_ir::{self as ty, Interner, elaborate};
89
use tracing::instrument;
910

@@ -391,6 +392,11 @@ where
391392
goal: Goal<I, ty::HostEffectPredicate<I>>,
392393
) -> QueryResult<I> {
393394
let candidates = self.assemble_and_evaluate_candidates(goal);
394-
self.merge_candidates(candidates)
395+
let (_, proven_via) = self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
396+
let trait_goal: Goal<I, ty::TraitPredicate<I>> =
397+
goal.with(ecx.cx(), goal.predicate.trait_ref);
398+
ecx.compute_trait_goal(trait_goal)
399+
})?;
400+
self.merge_candidates(proven_via, candidates)
395401
}
396402
}

‎compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_type_ir::relate::solver_relating::RelateExt;
1818
use rustc_type_ir::{self as ty, Canonical, CanonicalVarValues, InferCtxtLike, Interner};
1919
use tracing::{debug, instrument, trace};
2020

21-
use crate::canonicalizer::{CanonicalizeMode, Canonicalizer};
21+
use crate::canonicalizer::Canonicalizer;
2222
use crate::delegate::SolverDelegate;
2323
use crate::resolve::EagerResolver;
2424
use crate::solve::eval_ctxt::NestedGoals;
@@ -60,17 +60,13 @@ where
6060
(goal, opaque_types).fold_with(&mut EagerResolver::new(self.delegate));
6161

6262
let mut orig_values = Default::default();
63-
let canonical = Canonicalizer::canonicalize(
64-
self.delegate,
65-
CanonicalizeMode::Input,
66-
&mut orig_values,
67-
QueryInput {
63+
let canonical =
64+
Canonicalizer::canonicalize_input(self.delegate, &mut orig_values, QueryInput {
6865
goal,
6966
predefined_opaques_in_body: self
7067
.cx()
7168
.mk_predefined_opaques_in_body(PredefinedOpaquesData { opaque_types }),
72-
},
73-
);
69+
});
7470
let query_input = ty::CanonicalQueryInput { canonical, typing_mode: self.typing_mode() };
7571
(orig_values, query_input)
7672
}
@@ -148,9 +144,9 @@ where
148144
.region_constraints
149145
.retain(|outlives| outlives.0.as_region().map_or(true, |re| re != outlives.1));
150146

151-
let canonical = Canonicalizer::canonicalize(
147+
let canonical = Canonicalizer::canonicalize_response(
152148
self.delegate,
153-
CanonicalizeMode::Response { max_input_universe: self.max_input_universe },
149+
self.max_input_universe,
154150
&mut Default::default(),
155151
Response {
156152
var_values,
@@ -428,12 +424,7 @@ where
428424
let var_values = CanonicalVarValues { var_values: delegate.cx().mk_args(var_values) };
429425
let state = inspect::State { var_values, data };
430426
let state = state.fold_with(&mut EagerResolver::new(delegate));
431-
Canonicalizer::canonicalize(
432-
delegate,
433-
CanonicalizeMode::Response { max_input_universe },
434-
&mut vec![],
435-
state,
436-
)
427+
Canonicalizer::canonicalize_response(delegate, max_input_universe, &mut vec![], state)
437428
}
438429

439430
// FIXME: needs to be pub to be accessed by downstream

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ where
440440
if let Some(kind) = kind.no_bound_vars() {
441441
match kind {
442442
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
443-
self.compute_trait_goal(Goal { param_env, predicate })
443+
self.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
444444
}
445445
ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
446446
self.compute_host_effect_goal(Goal { param_env, predicate })

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,22 +243,27 @@ where
243243
.copied()
244244
}
245245

246+
fn bail_with_ambiguity(&mut self, responses: &[CanonicalResponse<I>]) -> CanonicalResponse<I> {
247+
debug_assert!(!responses.is_empty());
248+
if let Certainty::Maybe(maybe_cause) =
249+
responses.iter().fold(Certainty::AMBIGUOUS, |certainty, response| {
250+
certainty.unify_with(response.value.certainty)
251+
})
252+
{
253+
self.make_ambiguous_response_no_constraints(maybe_cause)
254+
} else {
255+
panic!("expected flounder response to be ambiguous")
256+
}
257+
}
258+
246259
/// If we fail to merge responses we flounder and return overflow or ambiguity.
247260
#[instrument(level = "trace", skip(self), ret)]
248261
fn flounder(&mut self, responses: &[CanonicalResponse<I>]) -> QueryResult<I> {
249262
if responses.is_empty() {
250263
return Err(NoSolution);
264+
} else {
265+
Ok(self.bail_with_ambiguity(responses))
251266
}
252-
253-
let Certainty::Maybe(maybe_cause) =
254-
responses.iter().fold(Certainty::AMBIGUOUS, |certainty, response| {
255-
certainty.unify_with(response.value.certainty)
256-
})
257-
else {
258-
panic!("expected flounder response to be ambiguous")
259-
};
260-
261-
Ok(self.make_ambiguous_response_no_constraints(maybe_cause))
262267
}
263268

264269
/// Normalize a type for when it is structurally matched on.

‎compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,17 @@ where
8888
/// returns `NoSolution`.
8989
#[instrument(level = "trace", skip(self), ret)]
9090
fn normalize_at_least_one_step(&mut self, goal: Goal<I, NormalizesTo<I>>) -> QueryResult<I> {
91-
match goal.predicate.alias.kind(self.cx()) {
91+
let cx = self.cx();
92+
match goal.predicate.alias.kind(cx) {
9293
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
9394
let candidates = self.assemble_and_evaluate_candidates(goal);
94-
self.merge_candidates(candidates)
95+
let (_, proven_via) =
96+
self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
97+
let trait_goal: Goal<I, ty::TraitPredicate<I>> =
98+
goal.with(cx, goal.predicate.alias.trait_ref(cx));
99+
ecx.compute_trait_goal(trait_goal)
100+
})?;
101+
self.merge_candidates(proven_via, candidates)
95102
}
96103
ty::AliasTermKind::InherentTy => self.normalize_inherent_associated_type(goal),
97104
ty::AliasTermKind::OpaqueTy => self.normalize_opaque_type(goal),

‎compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_type_ir::data_structures::IndexSet;
55
use rustc_type_ir::fast_reject::DeepRejectCtxt;
66
use rustc_type_ir::inherent::*;
77
use rustc_type_ir::lang_items::TraitSolverLangItem;
8+
use rustc_type_ir::solve::CanonicalResponse;
89
use rustc_type_ir::visit::TypeVisitableExt as _;
910
use rustc_type_ir::{self as ty, Interner, TraitPredicate, TypingMode, Upcast as _, elaborate};
1011
use tracing::{instrument, trace};
@@ -1147,13 +1148,101 @@ where
11471148
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
11481149
})
11491150
}
1151+
}
1152+
1153+
/// How we've proven this trait goal.
1154+
///
1155+
/// This is used by `NormalizesTo` goals to only normalize
1156+
/// by using the same 'kind of candidate' we've used to prove
1157+
/// its corresponding trait goal. Most notably, we do not
1158+
/// normalize by using an impl if the trait goal has been
1159+
/// proven via a `ParamEnv` candidate.
1160+
///
1161+
/// This is necessary to avoid unnecessary region constraints,
1162+
/// see trait-system-refactor-initiative#125 for more details.
1163+
#[derive(Debug, Clone, Copy)]
1164+
pub(super) enum TraitGoalProvenVia {
1165+
/// We've proven the trait goal by something which is
1166+
/// is not a non-global where-bound or an alias-bound.
1167+
///
1168+
/// This means we don't disable any candidates during
1169+
/// normalization.
1170+
Misc,
1171+
ParamEnv,
1172+
AliasBound,
1173+
}
1174+
1175+
impl<D, I> EvalCtxt<'_, D>
1176+
where
1177+
D: SolverDelegate<Interner = I>,
1178+
I: Interner,
1179+
{
1180+
pub(super) fn merge_trait_candidates(
1181+
&mut self,
1182+
goal: Goal<I, TraitPredicate<I>>,
1183+
candidates: Vec<Candidate<I>>,
1184+
) -> Result<(CanonicalResponse<I>, Option<TraitGoalProvenVia>), NoSolution> {
1185+
if let TypingMode::Coherence = self.typing_mode() {
1186+
let all_candidates: Vec<_> = candidates.into_iter().map(|c| c.result).collect();
1187+
return if let Some(response) = self.try_merge_responses(&all_candidates) {
1188+
Ok((response, Some(TraitGoalProvenVia::Misc)))
1189+
} else {
1190+
self.flounder(&all_candidates).map(|r| (r, None))
1191+
};
1192+
}
1193+
1194+
// FIXME: prefer trivial builtin impls
1195+
1196+
// If there are non-global where-bounds, prefer where-bounds
1197+
// (including global ones) over everything else.
1198+
let has_non_global_where_bounds = candidates.iter().any(|c| match c.source {
1199+
CandidateSource::ParamEnv(idx) => {
1200+
let where_bound = goal.param_env.caller_bounds().get(idx);
1201+
where_bound.has_bound_vars() || !where_bound.is_global()
1202+
}
1203+
_ => false,
1204+
});
1205+
if has_non_global_where_bounds {
1206+
let where_bounds: Vec<_> = candidates
1207+
.iter()
1208+
.filter(|c| matches!(c.source, CandidateSource::ParamEnv(_)))
1209+
.map(|c| c.result)
1210+
.collect();
1211+
1212+
return if let Some(response) = self.try_merge_responses(&where_bounds) {
1213+
Ok((response, Some(TraitGoalProvenVia::ParamEnv)))
1214+
} else {
1215+
Ok((self.bail_with_ambiguity(&where_bounds), None))
1216+
};
1217+
}
1218+
1219+
if candidates.iter().any(|c| matches!(c.source, CandidateSource::AliasBound)) {
1220+
let alias_bounds: Vec<_> = candidates
1221+
.iter()
1222+
.filter(|c| matches!(c.source, CandidateSource::AliasBound))
1223+
.map(|c| c.result)
1224+
.collect();
1225+
return if let Some(response) = self.try_merge_responses(&alias_bounds) {
1226+
Ok((response, Some(TraitGoalProvenVia::AliasBound)))
1227+
} else {
1228+
Ok((self.bail_with_ambiguity(&alias_bounds), None))
1229+
};
1230+
}
1231+
1232+
let all_candidates: Vec<_> = candidates.into_iter().map(|c| c.result).collect();
1233+
if let Some(response) = self.try_merge_responses(&all_candidates) {
1234+
Ok((response, Some(TraitGoalProvenVia::Misc)))
1235+
} else {
1236+
self.flounder(&all_candidates).map(|r| (r, None))
1237+
}
1238+
}
11501239

11511240
#[instrument(level = "trace", skip(self))]
11521241
pub(super) fn compute_trait_goal(
11531242
&mut self,
11541243
goal: Goal<I, TraitPredicate<I>>,
1155-
) -> QueryResult<I> {
1244+
) -> Result<(CanonicalResponse<I>, Option<TraitGoalProvenVia>), NoSolution> {
11561245
let candidates = self.assemble_and_evaluate_candidates(goal);
1157-
self.merge_candidates(candidates)
1246+
self.merge_trait_candidates(goal, candidates)
11581247
}
11591248
}

‎compiler/rustc_type_ir/src/inherent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ pub trait AdtDef<I: Interner>: Copy + Debug + Hash + Eq {
543543
}
544544

545545
pub trait ParamEnv<I: Interner>: Copy + Debug + Hash + Eq + TypeFoldable<I> {
546-
fn caller_bounds(self) -> impl IntoIterator<Item = I::Clause>;
546+
fn caller_bounds(self) -> impl SliceLike<Item = I::Clause>;
547547
}
548548

549549
pub trait Features<I: Interner>: Copy {

‎tests/crashes/133639.rs

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ check-pass
2+
3+
// Regression test for #133639.
4+
5+
#![feature(with_negative_coherence)]
6+
#![feature(min_specialization)]
7+
#![feature(generic_const_exprs)]
8+
//~^ WARNING the feature `generic_const_exprs` is incomplete
9+
10+
#![crate_type = "lib"]
11+
trait Trait {}
12+
struct A<const B: bool>;
13+
14+
trait C {}
15+
16+
impl<const D: u32> Trait for E<D> where A<{ D <= 2 }>: C {}
17+
struct E<const D: u32>;
18+
19+
impl<const D: u32> Trait for E<D> where A<{ D <= 2 }>: C {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/specialization-fuzzing-ice-133639.rs:7:12
3+
|
4+
LL | #![feature(generic_const_exprs)]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
warning: 1 warning emitted
11+

‎tests/ui/const-generics/min_const_generics/param-env-eager-norm-dedup.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//@ revisions: current next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
3+
//@[next] compile-flags: -Znext-solver
14
//@ check-pass
25

36
// This caused a regression in a crater run in #132325.

‎tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.next.stderr

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

‎tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.old.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error: item does not constrain `Foo::{opaque#0}`, but has it in its signature
2-
--> $DIR/norm-before-method-resolution-opaque-type.rs:15:4
2+
--> $DIR/norm-before-method-resolution-opaque-type.rs:16:4
33
|
44
LL | fn weird_bound<X>(x: &<X as Trait<'static>>::Out<Foo>) -> X
55
| ^^^^^^^^^^^
66
|
77
= note: consider moving the opaque type's declaration and defining uses into a separate module
88
note: this opaque type is in the signature
9-
--> $DIR/norm-before-method-resolution-opaque-type.rs:13:12
9+
--> $DIR/norm-before-method-resolution-opaque-type.rs:14:12
1010
|
1111
LL | type Foo = impl Sized;
1212
| ^^^^^^^^^^

‎tests/ui/higher-ranked/trait-bounds/normalize-under-binder/norm-before-method-resolution-opaque-type.rs

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

45
#![feature(type_alias_impl_trait)]
56
trait Trait<'a> {
@@ -14,7 +15,6 @@ type Foo = impl Sized;
1415

1516
fn weird_bound<X>(x: &<X as Trait<'static>>::Out<Foo>) -> X
1617
//[old]~^ ERROR: item does not constrain
17-
//[next]~^^ ERROR: cannot satisfy `Foo == _`
1818
where
1919
for<'a> X: Trait<'a>,
2020
for<'a> <X as Trait<'a>>::Out<()>: Copy,

‎tests/ui/traits/next-solver/generalize/occurs-check-nested-alias.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,4 @@ fn foo<T: Unnormalizable>() {
3737
// result in a cyclic type. However, we can still unify these types by first
3838
// normalizing the inner associated type. Emitting an error here would be incomplete.
3939
drop::<T>(t);
40-
41-
// FIXME(-Znext-solver): This line is necessary due to an unrelated solver bug
42-
// and should get removed in the future.
43-
// https://github.com/rust-lang/trait-system-refactor-initiative/issues/96
44-
drop::<Inv<<T as Unnormalizable>::Assoc>>(u);
4540
}

‎tests/ui/traits/winnowing/global-non-global-env-1.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//@ revisions: current next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
3+
//@[next] compile-flags: -Znext-solver
14
//@ check-pass
25

36
// A regression test for an edge case of candidate selection

‎tests/ui/traits/winnowing/global-non-global-env-2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//@ revisions: current next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
3+
//@[next] compile-flags: -Znext-solver
14
//@ check-pass
25

36
// A regression test for an edge case of candidate selection

‎tests/ui/traits/winnowing/global-non-global-env-3.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//@ revisions: current next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
3+
//@[next] compile-flags: -Znext-solver
14
//@ check-pass
25

36
// A regression test for an edge case of candidate selection

‎tests/ui/traits/winnowing/global-non-global-env-4.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//@ revisions: current next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
3+
//@[next] compile-flags: -Znext-solver
14
//@ check-pass
25

36
// A regression test for an edge case of candidate selection

0 commit comments

Comments
 (0)
Please sign in to comment.