Skip to content

Commit 0b23106

Browse files
Unstall obligations by looking for coroutines in old solver
1 parent 38081f2 commit 0b23106

File tree

3 files changed

+54
-54
lines changed

3 files changed

+54
-54
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -628,50 +628,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
628628
// trigger query cycle ICEs, as doing so requires MIR.
629629
self.select_obligations_where_possible(|_| {});
630630

631-
let coroutines = std::mem::take(&mut *self.deferred_coroutine_interiors.borrow_mut());
632-
debug!(?coroutines);
633-
634-
let mut obligations = vec![];
635-
636-
if !self.next_trait_solver() {
637-
for &(coroutine_def_id, interior) in coroutines.iter() {
638-
debug!(?coroutine_def_id);
639-
640-
// Create the `CoroutineWitness` type that we will unify with `interior`.
641-
let args = ty::GenericArgs::identity_for_item(
642-
self.tcx,
643-
self.tcx.typeck_root_def_id(coroutine_def_id.to_def_id()),
644-
);
645-
let witness =
646-
Ty::new_coroutine_witness(self.tcx, coroutine_def_id.to_def_id(), args);
647-
648-
// Unify `interior` with `witness` and collect all the resulting obligations.
649-
let span = self.tcx.hir_body_owned_by(coroutine_def_id).value.span;
650-
let ty::Infer(ty::InferTy::TyVar(_)) = interior.kind() else {
651-
span_bug!(span, "coroutine interior witness not infer: {:?}", interior.kind())
652-
};
653-
let ok = self
654-
.at(&self.misc(span), self.param_env)
655-
// Will never define opaque types, as all we do is instantiate a type variable.
656-
.eq(DefineOpaqueTypes::Yes, interior, witness)
657-
.expect("Failed to unify coroutine interior type");
658-
659-
obligations.extend(ok.obligations);
660-
}
661-
}
631+
let ty::TypingMode::Analysis { defining_opaque_types_and_generators } = self.typing_mode()
632+
else {
633+
bug!();
634+
};
662635

663-
if !coroutines.is_empty() {
664-
obligations.extend(
636+
if defining_opaque_types_and_generators
637+
.iter()
638+
.any(|def_id| self.tcx.is_coroutine(def_id.to_def_id()))
639+
{
640+
self.typeck_results.borrow_mut().coroutine_stalled_predicates.extend(
665641
self.fulfillment_cx
666642
.borrow_mut()
667-
.drain_stalled_obligations_for_coroutines(&self.infcx),
643+
.drain_stalled_obligations_for_coroutines(&self.infcx)
644+
.into_iter()
645+
.map(|o| (o.predicate, o.cause)),
668646
);
669647
}
670-
671-
self.typeck_results
672-
.borrow_mut()
673-
.coroutine_stalled_predicates
674-
.extend(obligations.into_iter().map(|o| (o.predicate, o.cause)));
675648
}
676649

677650
#[instrument(skip(self), level = "debug")]

compiler/rustc_trait_selection/src/solve/fulfill.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ where
259259
&mut self,
260260
infcx: &InferCtxt<'tcx>,
261261
) -> PredicateObligations<'tcx> {
262-
let stalled_generators = match infcx.typing_mode() {
262+
let stalled_coroutines = match infcx.typing_mode() {
263263
TypingMode::Analysis { defining_opaque_types_and_generators } => {
264264
defining_opaque_types_and_generators
265265
}
@@ -269,7 +269,7 @@ where
269269
| TypingMode::PostAnalysis => return Default::default(),
270270
};
271271

272-
if stalled_generators.is_empty() {
272+
if stalled_coroutines.is_empty() {
273273
return Default::default();
274274
}
275275

@@ -280,7 +280,7 @@ where
280280
.visit_proof_tree(
281281
obl.as_goal(),
282282
&mut StalledOnCoroutines {
283-
stalled_generators,
283+
stalled_coroutines,
284284
span: obl.cause.span,
285285
cache: Default::default(),
286286
},
@@ -302,10 +302,10 @@ where
302302
///
303303
/// This function can be also return false positives, which will lead to poor diagnostics
304304
/// so we want to keep this visitor *precise* too.
305-
struct StalledOnCoroutines<'tcx> {
306-
stalled_generators: &'tcx ty::List<LocalDefId>,
307-
span: Span,
308-
cache: DelayedSet<Ty<'tcx>>,
305+
pub struct StalledOnCoroutines<'tcx> {
306+
pub stalled_coroutines: &'tcx ty::List<LocalDefId>,
307+
pub span: Span,
308+
pub cache: DelayedSet<Ty<'tcx>>,
309309
}
310310

311311
impl<'tcx> inspect::ProofTreeVisitor<'tcx> for StalledOnCoroutines<'tcx> {
@@ -335,7 +335,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for StalledOnCoroutines<'tcx> {
335335
}
336336

337337
if let ty::CoroutineWitness(def_id, _) = *ty.kind()
338-
&& def_id.as_local().is_some_and(|def_id| self.stalled_generators.contains(&def_id))
338+
&& def_id.as_local().is_some_and(|def_id| self.stalled_coroutines.contains(&def_id))
339339
{
340340
return ControlFlow::Break(());
341341
}

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::marker::PhantomData;
33
use rustc_data_structures::obligation_forest::{
44
Error, ForestObligation, ObligationForest, ObligationProcessor, Outcome, ProcessResult,
55
};
6+
use rustc_hir::def_id::LocalDefId;
67
use rustc_infer::infer::DefineOpaqueTypes;
78
use rustc_infer::traits::{
89
FromSolverError, PolyTraitObligation, PredicateObligations, ProjectionCacheKey, SelectionError,
@@ -11,7 +12,10 @@ use rustc_infer::traits::{
1112
use rustc_middle::bug;
1213
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
1314
use rustc_middle::ty::error::{ExpectedFound, TypeError};
14-
use rustc_middle::ty::{self, Binder, Const, GenericArgsRef, TypeVisitableExt, TypingMode};
15+
use rustc_middle::ty::{
16+
self, Binder, Const, GenericArgsRef, TypeVisitable, TypeVisitableExt, TypingMode,
17+
};
18+
use rustc_span::DUMMY_SP;
1519
use thin_vec::ThinVec;
1620
use tracing::{debug, debug_span, instrument};
1721

@@ -24,6 +28,7 @@ use super::{
2428
};
2529
use crate::error_reporting::InferCtxtErrorExt;
2630
use crate::infer::{InferCtxt, TyOrConstInferVar};
31+
use crate::solve::StalledOnCoroutines;
2732
use crate::traits::normalize::normalize_with_depth_to;
2833
use crate::traits::project::{PolyProjectionObligation, ProjectionCacheKeyExt as _};
2934
use crate::traits::query::evaluate_obligation::InferCtxtExt;
@@ -166,15 +171,33 @@ where
166171
&mut self,
167172
infcx: &InferCtxt<'tcx>,
168173
) -> PredicateObligations<'tcx> {
169-
let mut processor =
170-
DrainProcessor { removed_predicates: PredicateObligations::new(), infcx };
174+
let stalled_coroutines = match infcx.typing_mode() {
175+
TypingMode::Analysis { defining_opaque_types_and_generators } => {
176+
defining_opaque_types_and_generators
177+
}
178+
TypingMode::Coherence
179+
| TypingMode::Borrowck { defining_opaque_types: _ }
180+
| TypingMode::PostBorrowckAnalysis { defined_opaque_types: _ }
181+
| TypingMode::PostAnalysis => return Default::default(),
182+
};
183+
184+
if stalled_coroutines.is_empty() {
185+
return Default::default();
186+
}
187+
188+
let mut processor = DrainProcessor {
189+
infcx,
190+
removed_predicates: PredicateObligations::new(),
191+
stalled_coroutines,
192+
};
171193
let outcome: Outcome<_, _> = self.predicates.process_obligations(&mut processor);
172194
assert!(outcome.errors.is_empty());
173195
return processor.removed_predicates;
174196

175197
struct DrainProcessor<'a, 'tcx> {
176198
infcx: &'a InferCtxt<'tcx>,
177199
removed_predicates: PredicateObligations<'tcx>,
200+
stalled_coroutines: &'tcx ty::List<LocalDefId>,
178201
}
179202

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

185208
fn needs_process_obligation(&self, pending_obligation: &Self::Obligation) -> bool {
186-
pending_obligation
187-
.stalled_on
188-
.iter()
189-
.any(|&var| self.infcx.ty_or_const_infer_var_changed(var))
209+
self.infcx
210+
.resolve_vars_if_possible(pending_obligation.obligation.predicate)
211+
.visit_with(&mut StalledOnCoroutines {
212+
stalled_coroutines: self.stalled_coroutines,
213+
span: DUMMY_SP,
214+
cache: Default::default(),
215+
})
216+
.is_break()
190217
}
191218

192219
fn process_obligation(

0 commit comments

Comments
 (0)