Skip to content

Commit

Permalink
update new solver candidate assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Nov 26, 2024
1 parent 9456bfe commit b21b116
Show file tree
Hide file tree
Showing 21 changed files with 170 additions and 105 deletions.
7 changes: 6 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ pub use adt::*;
pub use assoc::*;
pub use generic_args::{GenericArgKind, TermKind, *};
pub use generics::*;
// Can't use a glob import here as it would cause
// ambiguity when importing the actual types implementing
// the inherent traits from this module.
#[allow(rustc::non_glob_import_of_type_ir_inherent)]
use inherent::SliceLike;
pub use intrinsic::IntrinsicDef;
use rustc_abi::{Align, FieldIdx, Integer, IntegerType, ReprFlags, ReprOptions, VariantIdx};
use rustc_ast::expand::StrippedCfgItem;
Expand Down Expand Up @@ -970,7 +975,7 @@ pub struct ParamEnv<'tcx> {
}

impl<'tcx> rustc_type_ir::inherent::ParamEnv<TyCtxt<'tcx>> for ParamEnv<'tcx> {
fn caller_bounds(self) -> impl IntoIterator<Item = ty::Clause<'tcx>> {
fn caller_bounds(self) -> impl SliceLike<Item = ty::Clause<'tcx>> {
self.caller_bounds()
}
}
Expand Down
106 changes: 32 additions & 74 deletions compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rustc_type_ir::visit::TypeVisitableExt as _;
use rustc_type_ir::{self as ty, Interner, TypingMode, Upcast as _, elaborate};
use tracing::{debug, instrument};

use super::trait_goals::ProvenVia;
use crate::delegate::SolverDelegate;
use crate::solve::inspect::ProbeKind;
use crate::solve::{
Expand Down Expand Up @@ -337,15 +338,6 @@ where

self.assemble_param_env_candidates(goal, &mut candidates);

match self.typing_mode() {
TypingMode::Coherence => {}
TypingMode::Analysis { .. }
| TypingMode::PostBorrowckAnalysis { .. }
| TypingMode::PostAnalysis => {
self.discard_impls_shadowed_by_env(goal, &mut candidates);
}
}

candidates
}

Expand Down Expand Up @@ -500,7 +492,7 @@ where
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
) {
for (i, assumption) in goal.param_env.caller_bounds().into_iter().enumerate() {
for (i, assumption) in goal.param_env.caller_bounds().iter().enumerate() {
candidates.extend(G::probe_and_consider_implied_clause(
self,
CandidateSource::ParamEnv(i),
Expand Down Expand Up @@ -733,72 +725,38 @@ where
})
}

/// If there's a where-bound for the current goal, do not use any impl candidates
/// to prove the current goal. Most importantly, if there is a where-bound which does
/// not specify any associated types, we do not allow normalizing the associated type
/// by using an impl, even if it would apply.
///
/// <https://github.com/rust-lang/trait-system-refactor-initiative/issues/76>
// FIXME(@lcnr): The current structure here makes me unhappy and feels ugly. idk how
// to improve this however. However, this should make it fairly straightforward to refine
// the filtering going forward, so it seems alright-ish for now.
#[instrument(level = "debug", skip(self, goal))]
fn discard_impls_shadowed_by_env<G: GoalKind<D>>(
// TODO comments
#[instrument(level = "debug", skip(self), ret)]
pub(super) fn merge_candidates(
&mut self,
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
) {
let cx = self.cx();
let trait_goal: Goal<I, ty::TraitPredicate<I>> =
goal.with(cx, goal.predicate.trait_ref(cx));

let mut trait_candidates_from_env = vec![];
self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
ecx.assemble_param_env_candidates(trait_goal, &mut trait_candidates_from_env);
ecx.assemble_alias_bound_candidates(trait_goal, &mut trait_candidates_from_env);
});
proven_via: Option<ProvenVia>,
candidates: Vec<Candidate<I>>,
) -> QueryResult<I> {
let Some(proven_via) = proven_via else {
// We don't care about overflow. If proving the trait goal overflowed, then
// it's enough to report an overflow error for that, we don't also have to
// overflow during normalization.
return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Ambiguity));
};

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

/// If there are multiple ways to prove a trait or projection goal, we have
/// to somehow try to merge the candidates into one. If that fails, we return
/// ambiguity.
#[instrument(level = "debug", skip(self), ret)]
pub(super) fn merge_candidates(&mut self, candidates: Vec<Candidate<I>>) -> QueryResult<I> {
// First try merging all candidates. This is complete and fully sound.
let responses = candidates.iter().map(|c| c.result).collect::<Vec<_>>();
if let Some(result) = self.try_merge_responses(&responses) {
return Ok(result);
} else {
self.flounder(&responses)
}
self.try_merge_responses(&responses).map_or_else(|| self.flounder(&responses), Ok)
}
}
8 changes: 7 additions & 1 deletion compiler/rustc_next_trait_solver/src/solve/effect_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use rustc_type_ir::fast_reject::DeepRejectCtxt;
use rustc_type_ir::inherent::*;
use rustc_type_ir::lang_items::TraitSolverLangItem;
use rustc_type_ir::solve::inspect::ProbeKind;
use rustc_type_ir::{self as ty, Interner, elaborate};
use tracing::instrument;

Expand Down Expand Up @@ -391,6 +392,11 @@ where
goal: Goal<I, ty::HostEffectPredicate<I>>,
) -> QueryResult<I> {
let candidates = self.assemble_and_evaluate_candidates(goal);
self.merge_candidates(candidates)
let (_, proven_via) = self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
let trait_goal: Goal<I, ty::TraitPredicate<I>> =
goal.with(ecx.cx(), goal.predicate.trait_ref);
ecx.compute_trait_goal(trait_goal)
})?;
self.merge_candidates(proven_via, candidates)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ where
if let Some(kind) = kind.no_bound_vars() {
match kind {
ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
self.compute_trait_goal(Goal { param_env, predicate })
self.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
}
ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
self.compute_host_effect_goal(Goal { param_env, predicate })
Expand Down
25 changes: 15 additions & 10 deletions compiler/rustc_next_trait_solver/src/solve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,22 +243,27 @@ where
.copied()
}

fn bail_with_ambiguity(&mut self, responses: &[CanonicalResponse<I>]) -> CanonicalResponse<I> {
debug_assert!(!responses.is_empty());
if let Certainty::Maybe(maybe_cause) =
responses.iter().fold(Certainty::AMBIGUOUS, |certainty, response| {
certainty.unify_with(response.value.certainty)
})
{
self.make_ambiguous_response_no_constraints(maybe_cause)
} else {
panic!("expected flounder response to be ambiguous")
}
}

/// If we fail to merge responses we flounder and return overflow or ambiguity.
#[instrument(level = "trace", skip(self), ret)]
fn flounder(&mut self, responses: &[CanonicalResponse<I>]) -> QueryResult<I> {
if responses.is_empty() {
return Err(NoSolution);
} else {
Ok(self.bail_with_ambiguity(responses))
}

let Certainty::Maybe(maybe_cause) =
responses.iter().fold(Certainty::AMBIGUOUS, |certainty, response| {
certainty.unify_with(response.value.certainty)
})
else {
panic!("expected flounder response to be ambiguous")
};

Ok(self.make_ambiguous_response_no_constraints(maybe_cause))
}

/// Normalize a type for when it is structurally matched on.
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ where
/// returns `NoSolution`.
#[instrument(level = "trace", skip(self), ret)]
fn normalize_at_least_one_step(&mut self, goal: Goal<I, NormalizesTo<I>>) -> QueryResult<I> {
match goal.predicate.alias.kind(self.cx()) {
let cx = self.cx();
match goal.predicate.alias.kind(cx) {
ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
let candidates = self.assemble_and_evaluate_candidates(goal);
self.merge_candidates(candidates)
let (_, proven_via) =
self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
let trait_goal: Goal<I, ty::TraitPredicate<I>> =
goal.with(cx, goal.predicate.alias.trait_ref(cx));
ecx.compute_trait_goal(trait_goal)
})?;
self.merge_candidates(proven_via, candidates)
}
ty::AliasTermKind::InherentTy => self.normalize_inherent_associated_type(goal),
ty::AliasTermKind::OpaqueTy => self.normalize_opaque_type(goal),
Expand Down
78 changes: 76 additions & 2 deletions compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_type_ir::data_structures::IndexSet;
use rustc_type_ir::fast_reject::DeepRejectCtxt;
use rustc_type_ir::inherent::*;
use rustc_type_ir::lang_items::TraitSolverLangItem;
use rustc_type_ir::solve::CanonicalResponse;
use rustc_type_ir::visit::TypeVisitableExt as _;
use rustc_type_ir::{self as ty, Interner, TraitPredicate, TypingMode, Upcast as _, elaborate};
use tracing::{instrument, trace};
Expand Down Expand Up @@ -1139,13 +1140,86 @@ where
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
})
}
}

/// How we've proven this trait goal.
#[derive(Debug, Clone, Copy)]
pub(super) enum ProvenVia {
Impl,
ParamEnv,
AliasBound,
}

impl<D, I> EvalCtxt<'_, D>
where
D: SolverDelegate<Interner = I>,
I: Interner,
{
pub(super) fn merge_trait_candidates(
&mut self,
goal: Goal<I, TraitPredicate<I>>,
candidates: Vec<Candidate<I>>,
) -> Result<(CanonicalResponse<I>, Option<ProvenVia>), NoSolution> {
if let TypingMode::Coherence = self.typing_mode() {
let all_candidates: Vec<_> = candidates.into_iter().map(|c| c.result).collect();
return if let Some(response) = self.try_merge_responses(&all_candidates) {
Ok((response, Some(ProvenVia::Impl)))
} else {
self.flounder(&all_candidates).map(|r| (r, None))
};
}
// FIXME: prefer trivial builtin impls

// If there are non-global where-bounds, prefer where-bounds
// (including global ones) over everything else.
let has_non_global_where_bounds = candidates.iter().any(|c| match c.source {
CandidateSource::ParamEnv(idx) => {
let where_bound = goal.param_env.caller_bounds().get(idx);
where_bound.has_bound_vars() || !where_bound.is_global()
}
_ => false,
});
if has_non_global_where_bounds {
let where_bounds: Vec<_> = candidates
.iter()
.filter(|c| matches!(c.source, CandidateSource::ParamEnv(_)))
.map(|c| c.result)
.collect();

return if let Some(response) = self.try_merge_responses(&where_bounds) {
Ok((response, Some(ProvenVia::ParamEnv)))
} else {
Ok((self.bail_with_ambiguity(&where_bounds), None))
};
}

if candidates.iter().any(|c| matches!(c.source, CandidateSource::AliasBound)) {
let alias_bounds: Vec<_> = candidates
.iter()
.filter(|c| matches!(c.source, CandidateSource::AliasBound))
.map(|c| c.result)
.collect();
return if let Some(response) = self.try_merge_responses(&alias_bounds) {
Ok((response, Some(ProvenVia::AliasBound)))
} else {
Ok((self.bail_with_ambiguity(&alias_bounds), None))
};
}

let all_candidates: Vec<_> = candidates.into_iter().map(|c| c.result).collect();
if let Some(response) = self.try_merge_responses(&all_candidates) {
Ok((response, Some(ProvenVia::Impl)))
} else {
self.flounder(&all_candidates).map(|r| (r, None))
}
}

#[instrument(level = "trace", skip(self))]
pub(super) fn compute_trait_goal(
&mut self,
goal: Goal<I, TraitPredicate<I>>,
) -> QueryResult<I> {
) -> Result<(CanonicalResponse<I>, Option<ProvenVia>), NoSolution> {
let candidates = self.assemble_and_evaluate_candidates(goal);
self.merge_candidates(candidates)
self.merge_trait_candidates(goal, candidates)
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_type_ir/src/inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ pub trait AdtDef<I: Interner>: Copy + Debug + Hash + Eq {
}

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

pub trait Features<I: Interner>: Copy {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error: item does not constrain `Foo::{opaque#0}`, but has it in its signature
--> $DIR/norm-before-method-resolution-opaque-type.rs:16:4
--> $DIR/norm-before-method-resolution-opaque-type.rs:17:4
|
LL | fn weird_bound<X>(x: &<X as Trait<'static>>::Out<Foo>) -> X
| ^^^^^^^^^^^
|
= note: consider moving the opaque type's declaration and defining uses into a separate module
note: this opaque type is in the signature
--> $DIR/norm-before-method-resolution-opaque-type.rs:13:12
--> $DIR/norm-before-method-resolution-opaque-type.rs:14:12
|
LL | type Foo = impl Sized;
| ^^^^^^^^^^

error: unconstrained opaque type
--> $DIR/norm-before-method-resolution-opaque-type.rs:13:12
--> $DIR/norm-before-method-resolution-opaque-type.rs:14:12
|
LL | type Foo = impl Sized;
| ^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@ revisions: old next
//@[next] compile-flags: -Znext-solver
//@[next] check-pass

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

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

0 comments on commit b21b116

Please sign in to comment.