TypingMode 🤔

This commit is contained in:
lcnr 2024-10-18 00:28:43 +02:00
parent 2dece5bb62
commit f51ec110a7
75 changed files with 513 additions and 506 deletions

View file

@ -1,7 +1,7 @@
use std::ops::Deref;
use rustc_type_ir::fold::TypeFoldable;
use rustc_type_ir::solve::{Certainty, Goal, NoSolution, SolverMode};
use rustc_type_ir::solve::{Certainty, Goal, NoSolution};
use rustc_type_ir::{self as ty, InferCtxtLike, Interner};
pub trait SolverDelegate: Deref<Target = <Self as SolverDelegate>::Infcx> + Sized {
@ -15,7 +15,6 @@ pub trait SolverDelegate: Deref<Target = <Self as SolverDelegate>::Infcx> + Size
fn build_with_canonical<V>(
cx: Self::Interner,
solver_mode: SolverMode,
canonical: &ty::CanonicalQueryInput<Self::Interner, V>,
) -> (Self, V, ty::CanonicalVarValues<Self::Interner>)
where
@ -93,7 +92,6 @@ pub trait SolverDelegate: Deref<Target = <Self as SolverDelegate>::Infcx> + Size
fn fetch_eligible_assoc_item(
&self,
param_env: <Self::Interner as Interner>::ParamEnv,
goal_trait_ref: ty::TraitRef<Self::Interner>,
trait_assoc_def_id: <Self::Interner as Interner>::DefId,
impl_def_id: <Self::Interner as Interner>::DefId,

View file

@ -8,14 +8,14 @@ use rustc_type_ir::inherent::*;
use rustc_type_ir::lang_items::TraitSolverLangItem;
use rustc_type_ir::solve::inspect;
use rustc_type_ir::visit::TypeVisitableExt as _;
use rustc_type_ir::{self as ty, Interner, Upcast as _, elaborate};
use rustc_type_ir::{self as ty, Interner, TypingMode, Upcast as _, elaborate};
use tracing::{debug, instrument};
use crate::delegate::SolverDelegate;
use crate::solve::inspect::ProbeKind;
use crate::solve::{
BuiltinImplSource, CandidateSource, CanonicalResponse, Certainty, EvalCtxt, Goal, GoalSource,
MaybeCause, NoSolution, QueryResult, SolverMode,
MaybeCause, NoSolution, QueryResult,
};
/// A candidate is a possible way to prove a goal.
@ -328,11 +328,12 @@ where
let mut candidates = vec![];
if self.solver_mode() == SolverMode::Coherence {
if let TypingMode::Coherence = self.typing_mode(goal.param_env) {
if let Ok(candidate) = self.consider_coherence_unknowable_candidate(goal) {
return vec![candidate];
}
}
self.assemble_impl_candidates(goal, &mut candidates);
self.assemble_builtin_impl_candidates(goal, &mut candidates);
@ -343,8 +344,11 @@ where
self.assemble_param_env_candidates(goal, &mut candidates);
if self.solver_mode() == SolverMode::Normal {
self.discard_impls_shadowed_by_env(goal, &mut candidates);
match self.typing_mode(goal.param_env) {
TypingMode::Coherence => {}
TypingMode::Analysis { .. } | TypingMode::PostAnalysis => {
self.discard_impls_shadowed_by_env(goal, &mut candidates);
}
}
candidates

View file

@ -55,6 +55,7 @@ where
&self,
goal: Goal<I, T>,
) -> (Vec<I::GenericArg>, CanonicalInput<I, T>) {
let param_env_for_debug_assertion = goal.param_env;
let opaque_types = self.delegate.clone_opaque_types_for_query_response();
let (goal, opaque_types) =
(goal, opaque_types).fold_with(&mut EagerResolver::new(self.delegate));
@ -73,7 +74,7 @@ where
);
let query_input = ty::CanonicalQueryInput {
canonical,
defining_opaque_types: self.delegate.defining_opaque_types(),
typing_mode: self.typing_mode(param_env_for_debug_assertion),
};
(orig_values, query_input)
}

View file

@ -10,7 +10,7 @@ use rustc_type_ir::inherent::*;
use rustc_type_ir::relate::Relate;
use rustc_type_ir::relate::solver_relating::RelateExt;
use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
use rustc_type_ir::{self as ty, CanonicalVarValues, InferCtxtLike, Interner};
use rustc_type_ir::{self as ty, CanonicalVarValues, InferCtxtLike, Interner, TypingMode};
use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
use tracing::{instrument, trace};
@ -21,7 +21,6 @@ use crate::solve::search_graph::SearchGraph;
use crate::solve::{
CanonicalInput, Certainty, FIXPOINT_STEP_LIMIT, Goal, GoalEvaluationKind, GoalSource,
HasChanged, NestedNormalizationGoals, NoSolution, PredefinedOpaquesData, QueryResult,
SolverMode,
};
pub(super) mod canonical;
@ -215,8 +214,8 @@ where
D: SolverDelegate<Interner = I>,
I: Interner,
{
pub(super) fn solver_mode(&self) -> SolverMode {
self.search_graph.solver_mode()
pub(super) fn typing_mode(&self, param_env_for_debug_assertion: I::ParamEnv) -> TypingMode<I> {
self.delegate.typing_mode(param_env_for_debug_assertion)
}
pub(super) fn set_is_normalizes_to_goal(&mut self) {
@ -232,7 +231,7 @@ where
generate_proof_tree: GenerateProofTree,
f: impl FnOnce(&mut EvalCtxt<'_, D>) -> R,
) -> (R, Option<inspect::GoalEvaluation<I>>) {
let mut search_graph = SearchGraph::new(delegate.solver_mode(), root_depth);
let mut search_graph = SearchGraph::new(root_depth);
let mut ecx = EvalCtxt {
delegate,
@ -279,7 +278,7 @@ where
f: impl FnOnce(&mut EvalCtxt<'_, D>, Goal<I, I::Predicate>) -> R,
) -> R {
let (ref delegate, input, var_values) =
SolverDelegate::build_with_canonical(cx, search_graph.solver_mode(), &canonical_input);
SolverDelegate::build_with_canonical(cx, &canonical_input);
let mut ecx = EvalCtxt {
delegate,
@ -942,21 +941,11 @@ where
pub(super) fn fetch_eligible_assoc_item(
&self,
param_env: I::ParamEnv,
goal_trait_ref: ty::TraitRef<I>,
trait_assoc_def_id: I::DefId,
impl_def_id: I::DefId,
) -> Result<Option<I::DefId>, NoSolution> {
self.delegate.fetch_eligible_assoc_item(
param_env,
goal_trait_ref,
trait_assoc_def_id,
impl_def_id,
)
}
pub(super) fn can_define_opaque_ty(&self, def_id: I::LocalDefId) -> bool {
self.delegate.defining_opaque_types().contains(&def_id)
self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
}
pub(super) fn insert_hidden_type(

View file

@ -6,7 +6,7 @@ mod weak_types;
use rustc_type_ir::fast_reject::DeepRejectCtxt;
use rustc_type_ir::inherent::*;
use rustc_type_ir::lang_items::TraitSolverLangItem;
use rustc_type_ir::{self as ty, Interner, NormalizesTo, Upcast as _};
use rustc_type_ir::{self as ty, Interner, NormalizesTo, TypingMode, Upcast as _};
use tracing::instrument;
use crate::delegate::SolverDelegate;
@ -15,7 +15,7 @@ use crate::solve::assembly::{self, Candidate};
use crate::solve::inspect::ProbeKind;
use crate::solve::{
BuiltinImplSource, CandidateSource, Certainty, EvalCtxt, Goal, GoalSource, MaybeCause,
NoSolution, QueryResult, Reveal,
NoSolution, QueryResult,
};
impl<D, I> EvalCtxt<'_, D>
@ -71,21 +71,21 @@ where
Ok(())
}
ty::AliasTermKind::OpaqueTy => {
match param_env.reveal() {
// In user-facing mode, paques are only rigid if we may not define it.
Reveal::UserFacing => {
match self.typing_mode(param_env) {
// Opaques are never rigid outside of analysis mode.
TypingMode::Coherence | TypingMode::PostAnalysis => Err(NoSolution),
// During analysis, opaques are only rigid if we may not define it.
TypingMode::Analysis { defining_opaque_types } => {
if rigid_alias
.def_id
.as_local()
.is_some_and(|def_id| self.can_define_opaque_ty(def_id))
.is_some_and(|def_id| defining_opaque_types.contains(&def_id))
{
Err(NoSolution)
} else {
Ok(())
}
}
// Opaques are never rigid in reveal-all mode.
Reveal::All => Err(NoSolution),
}
}
// FIXME(generic_const_exprs): we would need to support generic consts here
@ -252,7 +252,6 @@ where
// return ambiguity this would otherwise be incomplete, resulting in
// unsoundness during coherence (#105782).
let Some(target_item_def_id) = ecx.fetch_eligible_assoc_item(
goal.param_env,
goal_trait_ref,
goal.predicate.def_id(),
impl_def_id,

View file

@ -4,12 +4,10 @@
use rustc_index::bit_set::GrowableBitSet;
use rustc_type_ir::inherent::*;
use rustc_type_ir::{self as ty, Interner};
use rustc_type_ir::{self as ty, Interner, TypingMode};
use crate::delegate::SolverDelegate;
use crate::solve::{
Certainty, EvalCtxt, Goal, NoSolution, QueryResult, Reveal, SolverMode, inspect,
};
use crate::solve::{Certainty, EvalCtxt, Goal, NoSolution, QueryResult, inspect};
impl<D, I> EvalCtxt<'_, D>
where
@ -24,17 +22,27 @@ where
let opaque_ty = goal.predicate.alias;
let expected = goal.predicate.term.as_type().expect("no such thing as an opaque const");
match (goal.param_env.reveal(), self.solver_mode()) {
(Reveal::UserFacing, SolverMode::Normal) => {
let Some(opaque_ty_def_id) = opaque_ty.def_id.as_local() else {
match self.typing_mode(goal.param_env) {
TypingMode::Coherence => {
// An impossible opaque type bound is the only way this goal will fail
// e.g. assigning `impl Copy := NotCopy`
self.add_item_bounds_for_hidden_type(
opaque_ty.def_id,
opaque_ty.args,
goal.param_env,
expected,
);
self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
}
TypingMode::Analysis { defining_opaque_types } => {
let Some(def_id) = opaque_ty.def_id.as_local() else {
return Err(NoSolution);
};
// FIXME: at some point we should call queries without defining
// new opaque types but having the existing opaque type definitions.
// This will require moving this below "Prefer opaques registered already".
if !self.can_define_opaque_ty(opaque_ty_def_id) {
if !defining_opaque_types.contains(&def_id) {
return Err(NoSolution);
}
// FIXME: This may have issues when the args contain aliases...
match uses_unique_placeholders_ignoring_regions(self.cx(), opaque_ty.args) {
Err(NotUniqueParam::NotParam(param)) if param.is_non_region_infer() => {
@ -48,8 +56,7 @@ where
Ok(()) => {}
}
// Prefer opaques registered already.
let opaque_type_key =
ty::OpaqueTypeKey { def_id: opaque_ty_def_id, args: opaque_ty.args };
let opaque_type_key = ty::OpaqueTypeKey { def_id, args: opaque_ty.args };
// FIXME: This also unifies the previous hidden type with the expected.
//
// If that fails, we insert `expected` as a new hidden type instead of
@ -69,7 +76,7 @@ where
}
ecx.eq(goal.param_env, candidate_ty, expected)?;
ecx.add_item_bounds_for_hidden_type(
candidate_key.def_id.into(),
def_id.into(),
candidate_key.args,
goal.param_env,
candidate_ty,
@ -82,25 +89,14 @@ where
// FIXME: should we use `inject_hidden_type_unchecked` here?
self.insert_hidden_type(opaque_type_key, goal.param_env, expected)?;
self.add_item_bounds_for_hidden_type(
opaque_ty.def_id,
def_id.into(),
opaque_ty.args,
goal.param_env,
expected,
);
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
}
(Reveal::UserFacing, SolverMode::Coherence) => {
// An impossible opaque type bound is the only way this goal will fail
// e.g. assigning `impl Copy := NotCopy`
self.add_item_bounds_for_hidden_type(
opaque_ty.def_id,
opaque_ty.args,
goal.param_env,
expected,
);
self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
}
(Reveal::All, _) => {
TypingMode::PostAnalysis => {
// FIXME: Add an assertion that opaque type storage is empty.
let actual = cx.type_of(opaque_ty.def_id).instantiate(cx, opaque_ty.args);
self.eq(goal.param_env, expected, actual)?;

View file

@ -6,7 +6,7 @@ use rustc_type_ir::fast_reject::DeepRejectCtxt;
use rustc_type_ir::inherent::*;
use rustc_type_ir::lang_items::TraitSolverLangItem;
use rustc_type_ir::visit::TypeVisitableExt as _;
use rustc_type_ir::{self as ty, Interner, TraitPredicate, Upcast as _, elaborate};
use rustc_type_ir::{self as ty, Interner, TraitPredicate, TypingMode, Upcast as _, elaborate};
use tracing::{instrument, trace};
use crate::delegate::SolverDelegate;
@ -15,7 +15,7 @@ use crate::solve::assembly::{self, Candidate};
use crate::solve::inspect::ProbeKind;
use crate::solve::{
BuiltinImplSource, CandidateSource, Certainty, EvalCtxt, Goal, GoalSource, MaybeCause,
NoSolution, QueryResult, Reveal, SolverMode,
NoSolution, QueryResult,
};
impl<D, I> assembly::GoalKind<D> for TraitPredicate<I>
@ -67,9 +67,9 @@ where
let maximal_certainty = match (impl_polarity, goal.predicate.polarity) {
// In intercrate mode, this is ambiguous. But outside of intercrate,
// it's not a real impl.
(ty::ImplPolarity::Reservation, _) => match ecx.solver_mode() {
SolverMode::Coherence => Certainty::AMBIGUOUS,
SolverMode::Normal => return Err(NoSolution),
(ty::ImplPolarity::Reservation, _) => match ecx.typing_mode(goal.param_env) {
TypingMode::Coherence => Certainty::AMBIGUOUS,
TypingMode::Analysis { .. } | TypingMode::PostAnalysis => return Err(NoSolution),
},
// Impl matches polarity
@ -167,33 +167,32 @@ where
return result;
}
// Don't call `type_of` on a local TAIT that's in the defining scope,
// since that may require calling `typeck` on the same item we're
// We only look into opaque types during analysis for opaque types
// outside of their defining scope. Doing so for opaques in the
// defining scope may require calling `typeck` on the same item we're
// currently type checking, which will result in a fatal cycle that
// ideally we want to avoid, since we can make progress on this goal
// via an alias bound or a locally-inferred hidden type instead.
//
// Also, don't call `type_of` on a TAIT in `Reveal::All` mode, since
// we already normalize the self type in
// `assemble_candidates_after_normalizing_self_ty`, and we'd
// just be registering an identical candidate here.
//
// We always return `Err(NoSolution)` here in `SolverMode::Coherence`
// since we'll always register an ambiguous candidate in
// `assemble_candidates_after_normalizing_self_ty` due to normalizing
// the TAIT.
if let ty::Alias(ty::Opaque, opaque_ty) = goal.predicate.self_ty().kind() {
if matches!(goal.param_env.reveal(), Reveal::All)
|| matches!(ecx.solver_mode(), SolverMode::Coherence)
|| opaque_ty
.def_id
.as_local()
.is_some_and(|def_id| ecx.can_define_opaque_ty(def_id))
{
return Err(NoSolution);
match ecx.typing_mode(goal.param_env) {
TypingMode::Coherence | TypingMode::PostAnalysis => {
unreachable!("rigid opaque outside of analysis: {goal:?}");
}
TypingMode::Analysis { defining_opaque_types } => {
if opaque_ty
.def_id
.as_local()
.is_some_and(|def_id| defining_opaque_types.contains(&def_id))
{
return Err(NoSolution);
}
}
}
}
// We want to make sure
debug_assert!(!matches!(ecx.typing_mode(), TypingMode::Coherence));
ecx.probe_and_evaluate_goal_for_constituent_tys(
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
goal,