Skip to content

Remove the witness type from coroutine *args* (without actually removing the type) #144458

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

Merged
merged 3 commits into from
Aug 2, 2025
Merged
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
1 change: 0 additions & 1 deletion compiler/rustc_borrowck/src/type_check/input_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
// them with fresh ty vars.
resume_ty: next_ty_var(),
yield_ty: next_ty_var(),
witness: next_ty_var(),
},
)
.args,
Expand Down
12 changes: 3 additions & 9 deletions compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,20 +379,14 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
// for info on the usage of each of these fields.
let dummy_args = match kind {
ClosureKind::Closure => &["<closure_kind>", "<closure_signature>", "<upvars>"][..],
ClosureKind::Coroutine(_) => &[
"<coroutine_kind>",
"<resume_ty>",
"<yield_ty>",
"<return_ty>",
"<witness>",
"<upvars>",
][..],
ClosureKind::Coroutine(_) => {
&["<coroutine_kind>", "<resume_ty>", "<yield_ty>", "<return_ty>", "<upvars>"][..]
}
ClosureKind::CoroutineClosure(_) => &[
"<closure_kind>",
"<closure_signature_parts>",
"<upvars>",
"<bound_captures_by_ref>",
"<witness>",
][..],
};

Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ 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);

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
// later during upvar analysis. Regular coroutines always have the kind
Expand All @@ -182,7 +180,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
resume_ty,
yield_ty,
return_ty: liberated_sig.output(),
witness: interior,
tupled_upvars_ty,
},
);
Expand Down Expand Up @@ -210,7 +207,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
// Compute all of the variables that will be used to populate the coroutine.
let resume_ty = self.next_ty_var(expr_span);
let interior = self.next_ty_var(expr_span);

let closure_kind_ty = match expected_kind {
Some(kind) => Ty::from_closure_kind(tcx, kind),
Expand Down Expand Up @@ -243,7 +239,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
),
tupled_upvars_ty,
coroutine_captures_by_ref_ty,
coroutine_witness_ty: interior,
},
);

Expand Down
29 changes: 10 additions & 19 deletions compiler/rustc_middle/src/ty/generic_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,38 +96,29 @@ impl<'tcx> rustc_type_ir::inherent::GenericArgs<TyCtxt<'tcx>> for ty::GenericArg
signature_parts_ty,
tupled_upvars_ty,
coroutine_captures_by_ref_ty,
coroutine_witness_ty,
] => ty::CoroutineClosureArgsParts {
parent_args,
closure_kind_ty: closure_kind_ty.expect_ty(),
signature_parts_ty: signature_parts_ty.expect_ty(),
tupled_upvars_ty: tupled_upvars_ty.expect_ty(),
coroutine_captures_by_ref_ty: coroutine_captures_by_ref_ty.expect_ty(),
coroutine_witness_ty: coroutine_witness_ty.expect_ty(),
},
_ => bug!("closure args missing synthetics"),
}
}

fn split_coroutine_args(self) -> ty::CoroutineArgsParts<TyCtxt<'tcx>> {
match self[..] {
[
ref parent_args @ ..,
kind_ty,
resume_ty,
yield_ty,
return_ty,
witness,
tupled_upvars_ty,
] => ty::CoroutineArgsParts {
parent_args,
kind_ty: kind_ty.expect_ty(),
resume_ty: resume_ty.expect_ty(),
yield_ty: yield_ty.expect_ty(),
return_ty: return_ty.expect_ty(),
witness: witness.expect_ty(),
tupled_upvars_ty: tupled_upvars_ty.expect_ty(),
},
[ref parent_args @ .., kind_ty, resume_ty, yield_ty, return_ty, tupled_upvars_ty] => {
ty::CoroutineArgsParts {
parent_args,
kind_ty: kind_ty.expect_ty(),
resume_ty: resume_ty.expect_ty(),
yield_ty: yield_ty.expect_ty(),
return_ty: return_ty.expect_ty(),
tupled_upvars_ty: tupled_upvars_ty.expect_ty(),
}
}
_ => bug!("coroutine args missing synthetics"),
}
}
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,9 +913,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
" yield_ty=",
print(args.as_coroutine().yield_ty()),
" return_ty=",
print(args.as_coroutine().return_ty()),
" witness=",
print(args.as_coroutine().witness())
print(args.as_coroutine().return_ty())
);
}

Expand Down Expand Up @@ -1035,9 +1033,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
" upvar_tys=",
print(args.as_coroutine_closure().tupled_upvars_ty()),
" coroutine_captures_by_ref_ty=",
print(args.as_coroutine_closure().coroutine_captures_by_ref_ty()),
" coroutine_witness_ty=",
print(args.as_coroutine_closure().coroutine_witness_ty())
print(args.as_coroutine_closure().coroutine_captures_by_ref_ty())
);
}
p!("}}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,16 @@ where
Ok(ty::Binder::dummy(vec![args.as_coroutine_closure().tupled_upvars_ty()]))
}

ty::Coroutine(_, args) => {
ty::Coroutine(def_id, args) => {
let coroutine_args = args.as_coroutine();
Ok(ty::Binder::dummy(vec![coroutine_args.tupled_upvars_ty(), coroutine_args.witness()]))
Ok(ty::Binder::dummy(vec![
coroutine_args.tupled_upvars_ty(),
Ty::new_coroutine_witness(
ecx.cx(),
def_id,
ecx.cx().mk_args(coroutine_args.parent_args().as_slice()),
),
]))
}

ty::CoroutineWitness(def_id, args) => Ok(ecx
Expand Down Expand Up @@ -245,7 +252,14 @@ where
Movability::Movable => {
if ecx.cx().features().coroutine_clone() {
let coroutine = args.as_coroutine();
Ok(ty::Binder::dummy(vec![coroutine.tupled_upvars_ty(), coroutine.witness()]))
Ok(ty::Binder::dummy(vec![
coroutine.tupled_upvars_ty(),
Ty::new_coroutine_witness(
ecx.cx(),
def_id,
ecx.cx().mk_args(coroutine.parent_args().as_slice()),
),
]))
} else {
Err(NoSolution)
}
Expand Down
11 changes: 4 additions & 7 deletions compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ where
}

// We need to make sure to stall any coroutines we are inferring to avoid query cycles.
if let Some(cand) = ecx.try_stall_coroutine_witness(goal.predicate.self_ty()) {
if let Some(cand) = ecx.try_stall_coroutine(goal.predicate.self_ty()) {
return cand;
}

Expand Down Expand Up @@ -294,7 +294,7 @@ where
}

// We need to make sure to stall any coroutines we are inferring to avoid query cycles.
if let Some(cand) = ecx.try_stall_coroutine_witness(goal.predicate.self_ty()) {
if let Some(cand) = ecx.try_stall_coroutine(goal.predicate.self_ty()) {
return cand;
}

Expand Down Expand Up @@ -1432,11 +1432,8 @@ where
self.merge_trait_candidates(candidates)
}

fn try_stall_coroutine_witness(
&mut self,
self_ty: I::Ty,
) -> Option<Result<Candidate<I>, NoSolution>> {
if let ty::CoroutineWitness(def_id, _) = self_ty.kind() {
fn try_stall_coroutine(&mut self, self_ty: I::Ty) -> Option<Result<Candidate<I>, NoSolution>> {
if let ty::Coroutine(def_id, _) = self_ty.kind() {
match self.typing_mode() {
TypingMode::Analysis {
defining_opaque_types_and_generators: stalled_generators,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/solve/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for StalledOnCoroutines<'tcx> {
return ControlFlow::Continue(());
}

if let ty::CoroutineWitness(def_id, _) = *ty.kind()
if let ty::Coroutine(def_id, _) = *ty.kind()
&& def_id.as_local().is_some_and(|def_id| self.stalled_coroutines.contains(&def_id))
{
ControlFlow::Break(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
})
}

ty::Coroutine(_, args) => {
ty::Coroutine(def_id, args) => {
// rust-lang/rust#49918: types can be constructed, stored
// in the interior, and sit idle when coroutine yields
// (and is subsequently dropped).
Expand Down Expand Up @@ -346,7 +346,10 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
// While we conservatively assume that all coroutines require drop
// to avoid query cycles during MIR building, we can check the actual
// witness during borrowck to avoid unnecessary liveness constraints.
if args.witness().needs_drop(tcx, tcx.erase_regions(typing_env)) {
let typing_env = tcx.erase_regions(typing_env);
if tcx.mir_coroutine_witnesses(def_id).is_some_and(|witness| {
witness.field_tys.iter().any(|field| field.ty.needs_drop(tcx, typing_env))
}) {
constraints.outlives.extend(args.upvar_tys().iter().map(ty::GenericArg::from));
constraints.outlives.push(args.resume_ty().into());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,18 +794,25 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// The auto impl might apply; we don't know.
candidates.ambiguous = true;
}
ty::Coroutine(coroutine_def_id, _)
if self.tcx().is_lang_item(def_id, LangItem::Unpin) =>
{
match self.tcx().coroutine_movability(coroutine_def_id) {
hir::Movability::Static => {
// Immovable coroutines are never `Unpin`, so
// suppress the normal auto-impl candidate for it.
ty::Coroutine(coroutine_def_id, _) => {
if self.tcx().is_lang_item(def_id, LangItem::Unpin) {
match self.tcx().coroutine_movability(coroutine_def_id) {
hir::Movability::Static => {
// Immovable coroutines are never `Unpin`, so
// suppress the normal auto-impl candidate for it.
}
hir::Movability::Movable => {
// Movable coroutines are always `Unpin`, so add an
// unconditional builtin candidate with no sub-obligations.
candidates.vec.push(BuiltinCandidate);
}
}
hir::Movability::Movable => {
// Movable coroutines are always `Unpin`, so add an
// unconditional builtin candidate.
candidates.vec.push(BuiltinCandidate);
} else {
if self.should_stall_coroutine(coroutine_def_id) {
candidates.ambiguous = true;
} else {
// Coroutines implement all other auto traits normally.
candidates.vec.push(AutoImplCandidate);
}
}
}
Expand Down Expand Up @@ -842,12 +849,8 @@ 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::CoroutineWitness(..) => {
candidates.vec.push(AutoImplCandidate);
}

ty::Bool
Expand All @@ -866,7 +869,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
| ty::FnPtr(..)
| ty::Closure(..)
| ty::CoroutineClosure(..)
| ty::Coroutine(..)
| ty::Never
| ty::Tuple(_)
| ty::UnsafeBinder(_) => {
Expand Down Expand Up @@ -1153,15 +1155,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
ty::Ref(_, _, hir::Mutability::Mut) => {}

ty::Coroutine(coroutine_def_id, args) => {
if self.should_stall_coroutine(coroutine_def_id) {
candidates.ambiguous = true;
return;
}

match self.tcx().coroutine_movability(coroutine_def_id) {
hir::Movability::Static => {}
hir::Movability::Movable => {
if self.tcx().features().coroutine_clone() {
let resolved_upvars =
self.infcx.shallow_resolve(args.as_coroutine().tupled_upvars_ty());
let resolved_witness =
self.infcx.shallow_resolve(args.as_coroutine().witness());
if resolved_upvars.is_ty_var() || resolved_witness.is_ty_var() {
if resolved_upvars.is_ty_var() {
// Not yet resolved.
candidates.ambiguous = true;
} else {
Expand Down Expand Up @@ -1194,12 +1199,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
}

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

// Fallback to whatever user-defined impls or param-env clauses exist in this case.
Expand Down Expand Up @@ -1238,7 +1239,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
| ty::RawPtr(..)
| ty::Char
| ty::Ref(..)
| ty::Coroutine(..)
| ty::Array(..)
| ty::Closure(..)
| ty::CoroutineClosure(..)
Expand All @@ -1247,14 +1247,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
candidates.vec.push(SizedCandidate);
}

ty::CoroutineWitness(coroutine_def_id, _) => {
if self.should_stall_coroutine_witness(coroutine_def_id) {
ty::Coroutine(coroutine_def_id, _) => {
if self.should_stall_coroutine(coroutine_def_id) {
candidates.ambiguous = true;
} else {
candidates.vec.push(SizedCandidate);
}
}

ty::CoroutineWitness(..) => {
candidates.vec.push(SizedCandidate);
}

// Conditionally `Sized`.
ty::Tuple(..) | ty::Pat(..) | ty::Adt(..) | ty::UnsafeBinder(_) => {
candidates.vec.push(SizedCandidate);
Expand Down
16 changes: 12 additions & 4 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,11 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
args.as_coroutine()
.upvar_tys()
.iter()
.chain([args.as_coroutine().witness()])
.chain([Ty::new_coroutine_witness(
self.tcx(),
coroutine_def_id,
self.tcx().mk_args(args.as_coroutine().parent_args()),
)])
.collect::<Vec<_>>(),
)
} else {
Expand Down Expand Up @@ -2327,9 +2331,13 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
ty::Binder::dummy(AutoImplConstituents { types: vec![ty], assumptions: vec![] })
}

ty::Coroutine(_, args) => {
ty::Coroutine(def_id, args) => {
let ty = self.infcx.shallow_resolve(args.as_coroutine().tupled_upvars_ty());
let witness = args.as_coroutine().witness();
let witness = Ty::new_coroutine_witness(
self.tcx(),
def_id,
self.tcx().mk_args(args.as_coroutine().parent_args()),
);
ty::Binder::dummy(AutoImplConstituents {
types: [ty].into_iter().chain(iter::once(witness)).collect(),
assumptions: vec![],
Expand Down Expand Up @@ -2841,7 +2849,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
obligations
}

fn should_stall_coroutine_witness(&self, def_id: DefId) -> bool {
fn should_stall_coroutine(&self, def_id: DefId) -> bool {
match self.infcx.typing_mode() {
TypingMode::Analysis { defining_opaque_types_and_generators: stalled_generators } => {
def_id.as_local().is_some_and(|def_id| stalled_generators.contains(&def_id))
Expand Down
Loading
Loading