Remove the witness type from coroutine args

This commit is contained in:
Michael Goulet 2025-07-25 16:46:11 +00:00
parent d05bb98d6b
commit e9765781b2
18 changed files with 54 additions and 136 deletions

View file

@ -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,

View file

@ -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>",
][..],
};

View file

@ -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
@ -182,7 +180,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
resume_ty,
yield_ty,
return_ty: liberated_sig.output(),
witness: interior,
tupled_upvars_ty,
},
);
@ -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),
@ -243,7 +239,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
),
tupled_upvars_ty,
coroutine_captures_by_ref_ty,
coroutine_witness_ty: interior,
},
);

View file

@ -96,14 +96,12 @@ 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"),
}
@ -111,23 +109,16 @@ impl<'tcx> rustc_type_ir::inherent::GenericArgs<TyCtxt<'tcx>> for ty::GenericArg
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"),
}
}

View file

@ -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())
);
}
@ -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!("}}");

View file

@ -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
@ -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)
}

View file

@ -1166,9 +1166,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
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 {

View file

@ -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 {
@ -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![],

View file

@ -101,7 +101,6 @@ use crate::{self as ty, Interner};
/// `yield` inside the coroutine.
/// * `GR`: The "return type", which is the type of value returned upon
/// completion of the coroutine.
/// * `GW`: The "coroutine witness".
#[derive_where(Clone, Copy, PartialEq, Eq, Hash, Debug; I: Interner)]
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
pub struct ClosureArgs<I: Interner> {
@ -239,8 +238,6 @@ pub struct CoroutineClosureArgsParts<I: Interner> {
/// while the `tupled_upvars_ty`, representing the by-move version of the same
/// captures, will be `(String,)`.
pub coroutine_captures_by_ref_ty: I::Ty,
/// Witness type returned by the generator produced by this coroutine-closure.
pub coroutine_witness_ty: I::Ty,
}
impl<I: Interner> CoroutineClosureArgs<I> {
@ -251,7 +248,6 @@ impl<I: Interner> CoroutineClosureArgs<I> {
parts.signature_parts_ty.into(),
parts.tupled_upvars_ty.into(),
parts.coroutine_captures_by_ref_ty.into(),
parts.coroutine_witness_ty.into(),
])),
}
}
@ -292,7 +288,6 @@ impl<I: Interner> CoroutineClosureArgs<I> {
}
pub fn coroutine_closure_sig(self) -> ty::Binder<I, CoroutineClosureSignature<I>> {
let interior = self.coroutine_witness_ty();
let ty::FnPtr(sig_tys, hdr) = self.signature_parts_ty().kind() else { panic!() };
sig_tys.map_bound(|sig_tys| {
let [resume_ty, tupled_inputs_ty] = *sig_tys.inputs().as_slice() else {
@ -302,7 +297,6 @@ impl<I: Interner> CoroutineClosureArgs<I> {
panic!()
};
CoroutineClosureSignature {
interior,
tupled_inputs_ty,
resume_ty,
yield_ty,
@ -318,10 +312,6 @@ impl<I: Interner> CoroutineClosureArgs<I> {
self.split().coroutine_captures_by_ref_ty
}
pub fn coroutine_witness_ty(self) -> I::Ty {
self.split().coroutine_witness_ty
}
pub fn has_self_borrows(&self) -> bool {
match self.coroutine_captures_by_ref_ty().kind() {
ty::FnPtr(sig_tys, _) => sig_tys
@ -361,7 +351,6 @@ impl<I: Interner> TypeVisitor<I> for HasRegionsBoundAt {
#[derive_where(Clone, Copy, PartialEq, Eq, Hash, Debug; I: Interner)]
#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
pub struct CoroutineClosureSignature<I: Interner> {
pub interior: I::Ty,
pub tupled_inputs_ty: I::Ty,
pub resume_ty: I::Ty,
pub yield_ty: I::Ty,
@ -407,7 +396,6 @@ impl<I: Interner> CoroutineClosureSignature<I> {
resume_ty: self.resume_ty,
yield_ty: self.yield_ty,
return_ty: self.return_ty,
witness: self.interior,
tupled_upvars_ty,
},
);
@ -587,11 +575,6 @@ pub struct CoroutineArgsParts<I: Interner> {
pub yield_ty: I::Ty,
pub return_ty: I::Ty,
/// The interior type of the coroutine.
/// Represents all types that are stored in locals
/// in the coroutine's body.
pub witness: I::Ty,
/// The upvars captured by the closure. Remains an inference variable
/// until the upvar analysis, which happens late in HIR typeck.
pub tupled_upvars_ty: I::Ty,
@ -607,7 +590,6 @@ impl<I: Interner> CoroutineArgs<I> {
parts.resume_ty.into(),
parts.yield_ty.into(),
parts.return_ty.into(),
parts.witness.into(),
parts.tupled_upvars_ty.into(),
])),
}
@ -629,15 +611,6 @@ impl<I: Interner> CoroutineArgs<I> {
self.split().kind_ty
}
/// This describes the types that can be contained in a coroutine.
/// It will be a type variable initially and unified in the last stages of typeck of a body.
/// It contains a tuple of all the types that could end up on a coroutine frame.
/// The state transformation MIR pass may only produce layouts which mention types
/// in this tuple. Upvars are not counted here.
pub fn witness(self) -> I::Ty {
self.split().witness
}
/// Returns an iterator over the list of types of captured paths by the coroutine.
/// In case there was a type error in figuring out the types of the captured path, an
/// empty iterator is returned.

View file

@ -9,10 +9,6 @@
std::future::ResumeTy,
(),
(),
CoroutineWitness(
DefId(0:5 ~ async_await[ccf8]::a::{closure#0}),
[],
),
(),
],
),
@ -30,10 +26,6 @@
std::future::ResumeTy,
(),
(),
CoroutineWitness(
DefId(0:5 ~ async_await[ccf8]::a::{closure#0}),
[],
),
(),
],
),

View file

@ -5,11 +5,11 @@ LL | let x = async || {};
| -- the expected `async` closure body
LL |
LL | let () = x();
| ^^ --- this expression has type `{static main::{closure#0}::{closure#0}<?16t> upvar_tys=?15t resume_ty=ResumeTy yield_ty=() return_ty=() witness={main::{closure#0}::{closure#0}}}`
| ^^ --- this expression has type `{static main::{closure#0}::{closure#0}<?15t> upvar_tys=?14t resume_ty=ResumeTy yield_ty=() return_ty=()}`
| |
| expected `async` closure body, found `()`
|
= note: expected `async` closure body `{static main::{closure#0}::{closure#0}<?16t> upvar_tys=?15t resume_ty=ResumeTy yield_ty=() return_ty=() witness={main::{closure#0}::{closure#0}}}`
= note: expected `async` closure body `{static main::{closure#0}::{closure#0}<?15t> upvar_tys=?14t resume_ty=ResumeTy yield_ty=() return_ty=()}`
found unit type `()`
error: aborting due to 1 previous error

View file

@ -21,30 +21,6 @@ LL | Box::new(async { new(|| async { f().await }).await })
= help: consider pinning your async block and casting it to a trait object
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0308]: mismatched types
--> $DIR/higher-ranked-auto-trait-6.rs:16:5
|
LL | Box::new(async { new(|| async { f().await }).await })
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected `async` block `{async block@$DIR/higher-ranked-auto-trait-6.rs:16:29: 16:34}`
found `async` block `{async block@$DIR/higher-ranked-auto-trait-6.rs:16:29: 16:34}`
= note: no two async blocks, even if identical, have the same type
= help: consider pinning your async block and casting it to a trait object
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0308]: mismatched types
--> $DIR/higher-ranked-auto-trait-6.rs:16:5
|
LL | Box::new(async { new(|| async { f().await }).await })
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected `async` block `{async block@$DIR/higher-ranked-auto-trait-6.rs:16:29: 16:34}`
found `async` block `{async block@$DIR/higher-ranked-auto-trait-6.rs:16:29: 16:34}`
= note: no two async blocks, even if identical, have the same type
= help: consider pinning your async block and casting it to a trait object
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 4 previous errors
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.

View file

@ -9,7 +9,7 @@ LL | | drop(a);
LL | | });
| |______^ coroutine is not `Sync`
|
= help: within `{main::{closure#0} upvar_tys=() resume_ty=() yield_ty=() return_ty=() witness={main::{closure#0}}}`, the trait `Sync` is not implemented for `NotSync`
= help: within `{main::{closure#0} upvar_tys=() resume_ty=() yield_ty=() return_ty=()}`, the trait `Sync` is not implemented for `NotSync`
note: coroutine is not `Sync` as this value is used across a yield
--> $DIR/coroutine-print-verbose-2.rs:20:9
|
@ -34,7 +34,7 @@ LL | | drop(a);
LL | | });
| |______^ coroutine is not `Send`
|
= help: within `{main::{closure#1} upvar_tys=() resume_ty=() yield_ty=() return_ty=() witness={main::{closure#1}}}`, the trait `Send` is not implemented for `NotSend`
= help: within `{main::{closure#1} upvar_tys=() resume_ty=() yield_ty=() return_ty=()}`, the trait `Send` is not implemented for `NotSend`
note: coroutine is not `Send` as this value is used across a yield
--> $DIR/coroutine-print-verbose-2.rs:27:9
|

View file

@ -11,7 +11,7 @@ LL | | };
| |_____^ expected `()`, found coroutine
|
= note: expected unit type `()`
found coroutine `{main::{closure#0} upvar_tys=?4t resume_ty=() yield_ty=i32 return_ty=&'?1 str witness={main::{closure#0}}}`
found coroutine `{main::{closure#0} upvar_tys=?4t resume_ty=() yield_ty=i32 return_ty=&'?1 str}`
error: aborting due to 1 previous error

View file

@ -12,7 +12,6 @@ impl<S> Bar for S {
type E = impl std::marker::Send;
fn foo<T>() -> Self::E {
//~^ ERROR type parameter `T` is part of concrete type but not used in parameter list for the `impl Trait` type alias
//~| ERROR type parameter `T` is part of concrete type but not used in parameter list for the `impl Trait` type alias
async {}
}
}

View file

@ -4,13 +4,5 @@ error: type parameter `T` is part of concrete type but not used in parameter lis
LL | fn foo<T>() -> Self::E {
| ^^^^^^^
error: type parameter `T` is part of concrete type but not used in parameter list for the `impl Trait` type alias
--> $DIR/issue-55872-2.rs:13:20
|
LL | fn foo<T>() -> Self::E {
| ^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 2 previous errors
error: aborting due to 1 previous error

View file

@ -14,7 +14,6 @@ impl<S> Bar for S {
fn foo<T>() -> Self::E {
//~^ ERROR : Copy` is not satisfied [E0277]
//~| ERROR type parameter `T` is part of concrete type
//~| ERROR type parameter `T` is part of concrete type
async {}
}
}

View file

@ -4,20 +4,12 @@ error: type parameter `T` is part of concrete type but not used in parameter lis
LL | fn foo<T>() -> Self::E {
| ^^^^^^^
error: type parameter `T` is part of concrete type but not used in parameter list for the `impl Trait` type alias
error[E0277]: the trait bound `{async block@$DIR/issue-55872-3.rs:17:9: 17:14}: Copy` is not satisfied
--> $DIR/issue-55872-3.rs:14:20
|
LL | fn foo<T>() -> Self::E {
| ^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
| ^^^^^^^ the trait `Copy` is not implemented for `{async block@$DIR/issue-55872-3.rs:17:9: 17:14}`
error[E0277]: the trait bound `{async block@$DIR/issue-55872-3.rs:18:9: 18:14}: Copy` is not satisfied
--> $DIR/issue-55872-3.rs:14:20
|
LL | fn foo<T>() -> Self::E {
| ^^^^^^^ the trait `Copy` is not implemented for `{async block@$DIR/issue-55872-3.rs:18:9: 18:14}`
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.