Skip to content

[experimental] Make witnesses more eager #141762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Resume type defaults to `()` if the coroutine has no argument.
let resume_ty = liberated_sig.inputs().get(0).copied().unwrap_or(tcx.types.unit);

// In the new solver, we can just instantiate this eagerly
// with the witness. This will ensure that goals that don't need
// to stall on interior types will get processed eagerly.
let interior = if self.next_trait_solver() {
Ty::new_coroutine_witness(tcx, expr_def_id.to_def_id(), parent_args)
} else {
self.next_ty_var(expr_span)
};

self.deferred_coroutine_interiors.borrow_mut().push((expr_def_id, interior));
let interior = Ty::new_coroutine_witness(tcx, expr_def_id.to_def_id(), parent_args);

// Coroutines that come from coroutine closures have not yet determined
// their kind ty, so make a fresh infer var which will be constrained
Expand Down
51 changes: 12 additions & 39 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,50 +628,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// trigger query cycle ICEs, as doing so requires MIR.
self.select_obligations_where_possible(|_| {});

let coroutines = std::mem::take(&mut *self.deferred_coroutine_interiors.borrow_mut());
debug!(?coroutines);

let mut obligations = vec![];

if !self.next_trait_solver() {
for &(coroutine_def_id, interior) in coroutines.iter() {
debug!(?coroutine_def_id);

// Create the `CoroutineWitness` type that we will unify with `interior`.
let args = ty::GenericArgs::identity_for_item(
self.tcx,
self.tcx.typeck_root_def_id(coroutine_def_id.to_def_id()),
);
let witness =
Ty::new_coroutine_witness(self.tcx, coroutine_def_id.to_def_id(), args);

// Unify `interior` with `witness` and collect all the resulting obligations.
let span = self.tcx.hir_body_owned_by(coroutine_def_id).value.span;
let ty::Infer(ty::InferTy::TyVar(_)) = interior.kind() else {
span_bug!(span, "coroutine interior witness not infer: {:?}", interior.kind())
};
let ok = self
.at(&self.misc(span), self.param_env)
// Will never define opaque types, as all we do is instantiate a type variable.
.eq(DefineOpaqueTypes::Yes, interior, witness)
.expect("Failed to unify coroutine interior type");

obligations.extend(ok.obligations);
}
}
let ty::TypingMode::Analysis { defining_opaque_types_and_generators } = self.typing_mode()
else {
bug!();
};

if !coroutines.is_empty() {
obligations.extend(
if defining_opaque_types_and_generators
.iter()
.any(|def_id| self.tcx.is_coroutine(def_id.to_def_id()))
{
self.typeck_results.borrow_mut().coroutine_stalled_predicates.extend(
self.fulfillment_cx
.borrow_mut()
.drain_stalled_obligations_for_coroutines(&self.infcx),
.drain_stalled_obligations_for_coroutines(&self.infcx)
.into_iter()
.map(|o| (o.predicate, o.cause)),
);
}

self.typeck_results
.borrow_mut()
.coroutine_stalled_predicates
.extend(obligations.into_iter().map(|o| (o.predicate, o.cause)));
}

#[instrument(skip(self), level = "debug")]
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_hir_typeck/src/typeck_root_ctxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ pub(crate) struct TypeckRootCtxt<'tcx> {

pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, HirId)>>,

pub(super) deferred_coroutine_interiors: RefCell<Vec<(LocalDefId, Ty<'tcx>)>>,

pub(super) deferred_repeat_expr_checks:
RefCell<Vec<(&'tcx hir::Expr<'tcx>, Ty<'tcx>, ty::Const<'tcx>)>>,

Expand Down Expand Up @@ -103,7 +101,6 @@ impl<'tcx> TypeckRootCtxt<'tcx> {
deferred_cast_checks: RefCell::new(Vec::new()),
deferred_transmute_checks: RefCell::new(Vec::new()),
deferred_asm_checks: RefCell::new(Vec::new()),
deferred_coroutine_interiors: RefCell::new(Vec::new()),
deferred_repeat_expr_checks: RefCell::new(Vec::new()),
diverging_type_vars: RefCell::new(Default::default()),
infer_var_info: RefCell::new(Default::default()),
Expand Down
18 changes: 7 additions & 11 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,17 +698,13 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
self,
defining_anchor: Self::LocalDefId,
) -> Self::LocalDefIds {
if self.next_trait_solver_globally() {
let coroutines_defined_by = self
.nested_bodies_within(defining_anchor)
.iter()
.filter(|def_id| self.is_coroutine(def_id.to_def_id()));
self.mk_local_def_ids_from_iter(
self.opaque_types_defined_by(defining_anchor).iter().chain(coroutines_defined_by),
)
} else {
self.opaque_types_defined_by(defining_anchor)
}
let coroutines_defined_by = self
.nested_bodies_within(defining_anchor)
.iter()
.filter(|def_id| self.is_coroutine(def_id.to_def_id()));
self.mk_local_def_ids_from_iter(
self.opaque_types_defined_by(defining_anchor).iter().chain(coroutines_defined_by),
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_trait_selection/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ impl<'tcx> InferCtxt<'tcx> {
let ty = self.resolve_vars_if_possible(ty);

// FIXME(#132279): This should be removed as it causes us to incorrectly
// handle opaques in their defining scope.
if !self.next_trait_solver() && !(param_env, ty).has_infer() {
// handle opaques in their defining scope, and stalled coroutines.
if !self.next_trait_solver() && !(param_env, ty).has_infer() && !ty.has_coroutines() {
return self.tcx.type_is_copy_modulo_regions(self.typing_env(param_env), ty);
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod normalize;
mod select;

pub(crate) use delegate::SolverDelegate;
pub use fulfill::{FulfillmentCtxt, NextSolverError};
pub use fulfill::{FulfillmentCtxt, NextSolverError, StalledOnCoroutines};
pub(crate) use normalize::deeply_normalize_for_diagnostics;
pub use normalize::{
deeply_normalize, deeply_normalize_with_skipped_universes,
Expand Down
27 changes: 15 additions & 12 deletions compiler/rustc_trait_selection/src/solve/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use rustc_infer::traits::{
FromSolverError, PredicateObligation, PredicateObligations, TraitEngine,
};
use rustc_middle::ty::{
self, DelayedSet, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor, TypingMode,
self, DelayedSet, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
TypingMode,
};
use rustc_next_trait_solver::delegate::SolverDelegate as _;
use rustc_next_trait_solver::solve::{
Expand Down Expand Up @@ -264,7 +265,7 @@ where
&mut self,
infcx: &InferCtxt<'tcx>,
) -> PredicateObligations<'tcx> {
let stalled_generators = match infcx.typing_mode() {
let stalled_coroutines = match infcx.typing_mode() {
TypingMode::Analysis { defining_opaque_types_and_generators } => {
defining_opaque_types_and_generators
}
Expand All @@ -274,7 +275,7 @@ where
| TypingMode::PostAnalysis => return Default::default(),
};

if stalled_generators.is_empty() {
if stalled_coroutines.is_empty() {
return Default::default();
}

Expand All @@ -285,7 +286,7 @@ where
.visit_proof_tree(
obl.as_goal(),
&mut StalledOnCoroutines {
stalled_generators,
stalled_coroutines,
span: obl.cause.span,
cache: Default::default(),
},
Expand All @@ -307,10 +308,10 @@ where
///
/// This function can be also return false positives, which will lead to poor diagnostics
/// so we want to keep this visitor *precise* too.
struct StalledOnCoroutines<'tcx> {
stalled_generators: &'tcx ty::List<LocalDefId>,
span: Span,
cache: DelayedSet<Ty<'tcx>>,
pub struct StalledOnCoroutines<'tcx> {
pub stalled_coroutines: &'tcx ty::List<LocalDefId>,
pub span: Span,
pub cache: DelayedSet<Ty<'tcx>>,
}

impl<'tcx> inspect::ProofTreeVisitor<'tcx> for StalledOnCoroutines<'tcx> {
Expand Down Expand Up @@ -340,12 +341,14 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for StalledOnCoroutines<'tcx> {
}

if let ty::CoroutineWitness(def_id, _) = *ty.kind()
&& def_id.as_local().is_some_and(|def_id| self.stalled_generators.contains(&def_id))
&& def_id.as_local().is_some_and(|def_id| self.stalled_coroutines.contains(&def_id))
{
return ControlFlow::Break(());
ControlFlow::Break(())
} else if ty.has_coroutines() {
ty.super_visit_with(self)
} else {
ControlFlow::Continue(())
}

ty.super_visit_with(self)
}
}

Expand Down
41 changes: 34 additions & 7 deletions compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::marker::PhantomData;
use rustc_data_structures::obligation_forest::{
Error, ForestObligation, ObligationForest, ObligationProcessor, Outcome, ProcessResult,
};
use rustc_hir::def_id::LocalDefId;
use rustc_infer::infer::DefineOpaqueTypes;
use rustc_infer::traits::{
FromSolverError, PolyTraitObligation, PredicateObligations, ProjectionCacheKey, SelectionError,
Expand All @@ -11,7 +12,10 @@ use rustc_infer::traits::{
use rustc_middle::bug;
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, Binder, Const, GenericArgsRef, TypeVisitableExt, TypingMode};
use rustc_middle::ty::{
self, Binder, Const, GenericArgsRef, TypeVisitable, TypeVisitableExt, TypingMode,
};
use rustc_span::DUMMY_SP;
use thin_vec::ThinVec;
use tracing::{debug, debug_span, instrument};

Expand All @@ -24,6 +28,7 @@ use super::{
};
use crate::error_reporting::InferCtxtErrorExt;
use crate::infer::{InferCtxt, TyOrConstInferVar};
use crate::solve::StalledOnCoroutines;
use crate::traits::normalize::normalize_with_depth_to;
use crate::traits::project::{PolyProjectionObligation, ProjectionCacheKeyExt as _};
use crate::traits::query::evaluate_obligation::InferCtxtExt;
Expand Down Expand Up @@ -166,15 +171,33 @@ where
&mut self,
infcx: &InferCtxt<'tcx>,
) -> PredicateObligations<'tcx> {
let mut processor =
DrainProcessor { removed_predicates: PredicateObligations::new(), infcx };
let stalled_coroutines = match infcx.typing_mode() {
TypingMode::Analysis { defining_opaque_types_and_generators } => {
defining_opaque_types_and_generators
}
TypingMode::Coherence
| TypingMode::Borrowck { defining_opaque_types: _ }
| TypingMode::PostBorrowckAnalysis { defined_opaque_types: _ }
| TypingMode::PostAnalysis => return Default::default(),
};

if stalled_coroutines.is_empty() {
return Default::default();
}

let mut processor = DrainProcessor {
infcx,
removed_predicates: PredicateObligations::new(),
stalled_coroutines,
};
let outcome: Outcome<_, _> = self.predicates.process_obligations(&mut processor);
assert!(outcome.errors.is_empty());
return processor.removed_predicates;

struct DrainProcessor<'a, 'tcx> {
infcx: &'a InferCtxt<'tcx>,
removed_predicates: PredicateObligations<'tcx>,
stalled_coroutines: &'tcx ty::List<LocalDefId>,
}

impl<'tcx> ObligationProcessor for DrainProcessor<'_, 'tcx> {
Expand All @@ -183,10 +206,14 @@ where
type OUT = Outcome<Self::Obligation, Self::Error>;

fn needs_process_obligation(&self, pending_obligation: &Self::Obligation) -> bool {
pending_obligation
.stalled_on
.iter()
.any(|&var| self.infcx.ty_or_const_infer_var_changed(var))
self.infcx
.resolve_vars_if_possible(pending_obligation.obligation.predicate)
.visit_with(&mut StalledOnCoroutines {
stalled_coroutines: self.stalled_coroutines,
span: DUMMY_SP,
cache: Default::default(),
})
.is_break()
}

fn process_obligation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
}

ty::CoroutineWitness(def_id, _) => {
if self.should_stall_coroutine_witness(def_id) {
candidates.ambiguous = true;
} else {
candidates.vec.push(AutoImplCandidate);
}
}

ty::Bool
| ty::Char
| ty::Int(_)
Expand All @@ -839,7 +847,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
| ty::Coroutine(..)
| ty::Never
| ty::Tuple(_)
| ty::CoroutineWitness(..)
| ty::UnsafeBinder(_) => {
// Only consider auto impls of unsafe traits when there are
// no unsafe fields.
Expand Down
37 changes: 29 additions & 8 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
defining_opaque_types_and_generators: defining_opaque_types,
}
| TypingMode::Borrowck { defining_opaque_types } => {
defining_opaque_types.is_empty() || !pred.has_opaque_types()
defining_opaque_types.is_empty()
|| (!pred.has_opaque_types() && !pred.has_coroutines())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like we prolly want to instead never erase the coroutines, only the defining opaques when creating a TypingEnv from the current infcx

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be expensive to erase on every call to TypingEnv; otherwise i'll keep this and write a comment explaining why it's unsound.

}
// The hidden types of `defined_opaque_types` is not local to the current
// inference context, so we can freely move this to the global cache.
Expand Down Expand Up @@ -2224,13 +2225,17 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
}

ty::CoroutineWitness(def_id, args) => {
let hidden_types = rebind_coroutine_witness_types(
self.infcx.tcx,
def_id,
args,
obligation.predicate.bound_vars(),
);
Where(hidden_types)
if self.should_stall_coroutine_witness(def_id) {
Ambiguous
} else {
let hidden_types = rebind_coroutine_witness_types(
self.infcx.tcx,
def_id,
args,
obligation.predicate.bound_vars(),
);
Where(hidden_types)
}
}

ty::Closure(_, args) => {
Expand Down Expand Up @@ -2856,6 +2861,22 @@ impl<'tcx> SelectionContext<'_, 'tcx> {

obligations
}

fn should_stall_coroutine_witness(&self, def_id: DefId) -> bool {
match self.infcx.typing_mode() {
TypingMode::Analysis { defining_opaque_types_and_generators: stalled_generators } => {
if def_id.as_local().is_some_and(|def_id| stalled_generators.contains(&def_id)) {
return true;
}
}
TypingMode::Coherence
| TypingMode::PostAnalysis
| TypingMode::Borrowck { defining_opaque_types: _ }
| TypingMode::PostBorrowckAnalysis { defined_opaque_types: _ } => {}
}

false
}
}

fn rebind_coroutine_witness_types<'tcx>(
Expand Down
Loading
Loading