Skip to content

Commit b21b116

Browse files
committed
update new solver candidate assembly
1 parent 9456bfe commit b21b116

21 files changed

+170
-105
lines changed

compiler/rustc_middle/src/ty/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ pub use adt::*;
2323
pub use assoc::*;
2424
pub use generic_args::{GenericArgKind, TermKind, *};
2525
pub use generics::*;
26+
// Can't use a glob import here as it would cause
27+
// ambiguity when importing the actual types implementing
28+
// the inherent traits from this module.
29+
#[allow(rustc::non_glob_import_of_type_ir_inherent)]
30+
use inherent::SliceLike;
2631
pub use intrinsic::IntrinsicDef;
2732
use rustc_abi::{Align, FieldIdx, Integer, IntegerType, ReprFlags, ReprOptions, VariantIdx};
2833
use rustc_ast::expand::StrippedCfgItem;
@@ -970,7 +975,7 @@ pub struct ParamEnv<'tcx> {
970975
}
971976

972977
impl<'tcx> rustc_type_ir::inherent::ParamEnv<TyCtxt<'tcx>> for ParamEnv<'tcx> {
973-
fn caller_bounds(self) -> impl IntoIterator<Item = ty::Clause<'tcx>> {
978+
fn caller_bounds(self) -> impl SliceLike<Item = ty::Clause<'tcx>> {
974979
self.caller_bounds()
975980
}
976981
}

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

+32-74
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::ProvenVia;
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,38 @@ 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.
740-
///
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>>(
728+
// TODO comments
729+
#[instrument(level = "debug", skip(self), ret)]
730+
pub(super) fn merge_candidates(
747731
&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-
});
732+
proven_via: Option<ProvenVia>,
733+
candidates: Vec<Candidate<I>>,
734+
) -> QueryResult<I> {
735+
let Some(proven_via) = proven_via else {
736+
// We don't care about overflow. If proving the trait goal overflowed, then
737+
// it's enough to report an overflow error for that, we don't also have to
738+
// overflow during normalization.
739+
return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Ambiguity));
740+
};
760741

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-
}
742+
let responses: Vec<_> = match proven_via {
743+
// Even when a trait bound has been proven using a where-bound, we
744+
// still need to consider alias-bounds for normalization, see
745+
// tests/ui/next-solver/alias-bound-shadowed-by-env.rs.
746+
//
747+
// FIXME(const_trait_impl): should this behavior also be used by
748+
// constness checking. Doing so is *at least theoretically* breaking,
749+
// see github.com/rust-lang/rust/issues/133044#issuecomment-2500709754
750+
ProvenVia::ParamEnv | ProvenVia::AliasBound => candidates
751+
.iter()
752+
.filter(|c| {
753+
matches!(c.source, CandidateSource::AliasBound | CandidateSource::ParamEnv(_))
754+
})
755+
.map(|c| c.result)
756+
.collect(),
757+
ProvenVia::Impl => candidates.iter().map(|c| c.result).collect(),
758+
};
790759

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-
}
760+
self.try_merge_responses(&responses).map_or_else(|| self.flounder(&responses), Ok)
803761
}
804762
}

compiler/rustc_next_trait_solver/src/solve/effect_goals.rs

+7-1
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/mod.rs

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

+15-10
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

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

+76-2
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};
@@ -1139,13 +1140,86 @@ where
11391140
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
11401141
})
11411142
}
1143+
}
1144+
1145+
/// How we've proven this trait goal.
1146+
#[derive(Debug, Clone, Copy)]
1147+
pub(super) enum ProvenVia {
1148+
Impl,
1149+
ParamEnv,
1150+
AliasBound,
1151+
}
1152+
1153+
impl<D, I> EvalCtxt<'_, D>
1154+
where
1155+
D: SolverDelegate<Interner = I>,
1156+
I: Interner,
1157+
{
1158+
pub(super) fn merge_trait_candidates(
1159+
&mut self,
1160+
goal: Goal<I, TraitPredicate<I>>,
1161+
candidates: Vec<Candidate<I>>,
1162+
) -> Result<(CanonicalResponse<I>, Option<ProvenVia>), NoSolution> {
1163+
if let TypingMode::Coherence = self.typing_mode() {
1164+
let all_candidates: Vec<_> = candidates.into_iter().map(|c| c.result).collect();
1165+
return if let Some(response) = self.try_merge_responses(&all_candidates) {
1166+
Ok((response, Some(ProvenVia::Impl)))
1167+
} else {
1168+
self.flounder(&all_candidates).map(|r| (r, None))
1169+
};
1170+
}
1171+
// FIXME: prefer trivial builtin impls
1172+
1173+
// If there are non-global where-bounds, prefer where-bounds
1174+
// (including global ones) over everything else.
1175+
let has_non_global_where_bounds = candidates.iter().any(|c| match c.source {
1176+
CandidateSource::ParamEnv(idx) => {
1177+
let where_bound = goal.param_env.caller_bounds().get(idx);
1178+
where_bound.has_bound_vars() || !where_bound.is_global()
1179+
}
1180+
_ => false,
1181+
});
1182+
if has_non_global_where_bounds {
1183+
let where_bounds: Vec<_> = candidates
1184+
.iter()
1185+
.filter(|c| matches!(c.source, CandidateSource::ParamEnv(_)))
1186+
.map(|c| c.result)
1187+
.collect();
1188+
1189+
return if let Some(response) = self.try_merge_responses(&where_bounds) {
1190+
Ok((response, Some(ProvenVia::ParamEnv)))
1191+
} else {
1192+
Ok((self.bail_with_ambiguity(&where_bounds), None))
1193+
};
1194+
}
1195+
1196+
if candidates.iter().any(|c| matches!(c.source, CandidateSource::AliasBound)) {
1197+
let alias_bounds: Vec<_> = candidates
1198+
.iter()
1199+
.filter(|c| matches!(c.source, CandidateSource::AliasBound))
1200+
.map(|c| c.result)
1201+
.collect();
1202+
return if let Some(response) = self.try_merge_responses(&alias_bounds) {
1203+
Ok((response, Some(ProvenVia::AliasBound)))
1204+
} else {
1205+
Ok((self.bail_with_ambiguity(&alias_bounds), None))
1206+
};
1207+
}
1208+
1209+
let all_candidates: Vec<_> = candidates.into_iter().map(|c| c.result).collect();
1210+
if let Some(response) = self.try_merge_responses(&all_candidates) {
1211+
Ok((response, Some(ProvenVia::Impl)))
1212+
} else {
1213+
self.flounder(&all_candidates).map(|r| (r, None))
1214+
}
1215+
}
11421216

11431217
#[instrument(level = "trace", skip(self))]
11441218
pub(super) fn compute_trait_goal(
11451219
&mut self,
11461220
goal: Goal<I, TraitPredicate<I>>,
1147-
) -> QueryResult<I> {
1221+
) -> Result<(CanonicalResponse<I>, Option<ProvenVia>), NoSolution> {
11481222
let candidates = self.assemble_and_evaluate_candidates(goal);
1149-
self.merge_candidates(candidates)
1223+
self.merge_trait_candidates(goal, candidates)
11501224
}
11511225
}

compiler/rustc_type_ir/src/inherent.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ pub trait AdtDef<I: Interner>: Copy + Debug + Hash + Eq {
542542
}
543543

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

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

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

-9
This file was deleted.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
error: item does not constrain `Foo::{opaque#0}`, but has it in its signature
2-
--> $DIR/norm-before-method-resolution-opaque-type.rs:16:4
2+
--> $DIR/norm-before-method-resolution-opaque-type.rs:17: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
| ^^^^^^^^^^
1313

1414
error: unconstrained opaque type
15-
--> $DIR/norm-before-method-resolution-opaque-type.rs:13:12
15+
--> $DIR/norm-before-method-resolution-opaque-type.rs:14:12
1616
|
1717
LL | type Foo = impl Sized;
1818
| ^^^^^^^^^^

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

+1-1
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> {
@@ -15,7 +16,6 @@ type Foo = impl Sized;
1516

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

0 commit comments

Comments
 (0)