Rollup merge of #118762 - compiler-errors:gen-nits, r=eholk

Some more minor `async gen`-related nits

Tiny tweaks found after `async gen` pr landed

r? eholk
This commit is contained in:
Jubilee 2023-12-09 00:48:11 -08:00 committed by GitHub
commit 402cfb17f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 34 deletions

View File

@ -2450,6 +2450,14 @@ pub fn is_gen(self) -> bool {
matches!(self, CoroutineKind::Gen { .. }) matches!(self, CoroutineKind::Gen { .. })
} }
pub fn closure_id(self) -> NodeId {
match self {
CoroutineKind::Async { closure_id, .. }
| CoroutineKind::Gen { closure_id, .. }
| CoroutineKind::AsyncGen { closure_id, .. } => closure_id,
}
}
/// In this case this is an `async` or `gen` return, the `NodeId` for the generated `impl Trait` /// In this case this is an `async` or `gen` return, the `NodeId` for the generated `impl Trait`
/// item. /// item.
pub fn return_id(self) -> (NodeId, Span) { pub fn return_id(self) -> (NodeId, Span) {

View File

@ -1035,10 +1035,7 @@ fn lower_maybe_coroutine_body(
let (Some(coroutine_kind), Some(body)) = (coroutine_kind, body) else { let (Some(coroutine_kind), Some(body)) = (coroutine_kind, body) else {
return self.lower_fn_body_block(span, decl, body); return self.lower_fn_body_block(span, decl, body);
}; };
// FIXME(gen_blocks): Introduce `closure_id` method and remove ALL destructuring. let closure_id = coroutine_kind.closure_id();
let (CoroutineKind::Async { closure_id, .. }
| CoroutineKind::Gen { closure_id, .. }
| CoroutineKind::AsyncGen { closure_id, .. }) = coroutine_kind;
self.lower_body(|this| { self.lower_body(|this| {
let mut parameters: Vec<hir::Param<'_>> = Vec::new(); let mut parameters: Vec<hir::Param<'_>> = Vec::new();

View File

@ -177,7 +177,8 @@ fn new(tcx: TyCtxt<'hir>, resolver: &'a mut ResolverAstLowering) -> Self {
} else { } else {
[sym::gen_future].into() [sym::gen_future].into()
}, },
// FIXME(gen_blocks): how does `closure_track_caller` // FIXME(gen_blocks): how does `closure_track_caller`/`async_fn_track_caller`
// interact with `gen`/`async gen` blocks
allow_async_iterator: [sym::gen_future, sym::async_iterator].into(), allow_async_iterator: [sym::gen_future, sym::async_iterator].into(),
generics_def_id_map: Default::default(), generics_def_id_map: Default::default(),
host_param_id: None, host_param_id: None,

View File

@ -1271,11 +1271,11 @@ fn visit_fn(&mut self, fk: FnKind<'a>, span: Span, id: NodeId) {
// Functions cannot both be `const async` or `const gen` // Functions cannot both be `const async` or `const gen`
if let Some(&FnHeader { if let Some(&FnHeader {
constness: Const::Yes(cspan), constness: Const::Yes(cspan),
coroutine_kind: Some(coro_kind), coroutine_kind: Some(coroutine_kind),
.. ..
}) = fk.header() }) = fk.header()
{ {
let aspan = match coro_kind { let aspan = match coroutine_kind {
CoroutineKind::Async { span: aspan, .. } CoroutineKind::Async { span: aspan, .. }
| CoroutineKind::Gen { span: aspan, .. } | CoroutineKind::Gen { span: aspan, .. }
| CoroutineKind::AsyncGen { span: aspan, .. } => aspan, | CoroutineKind::AsyncGen { span: aspan, .. } => aspan,

View File

@ -541,8 +541,8 @@ fn check_test_signature(
return Err(sd.emit_err(errors::TestBadFn { span: i.span, cause: span, kind: "unsafe" })); return Err(sd.emit_err(errors::TestBadFn { span: i.span, cause: span, kind: "unsafe" }));
} }
if let Some(coro_kind) = f.sig.header.coroutine_kind { if let Some(coroutine_kind) = f.sig.header.coroutine_kind {
match coro_kind { match coroutine_kind {
ast::CoroutineKind::Async { span, .. } => { ast::CoroutineKind::Async { span, .. } => {
return Err(sd.emit_err(errors::TestBadFn { return Err(sd.emit_err(errors::TestBadFn {
span: i.span, span: i.span,

View File

@ -162,11 +162,8 @@ fn visit_fn(&mut self, fk: ast_visit::FnKind<'a>, span: Span, id: ast::NodeId) {
// Explicitly check for lints associated with 'closure_id', since // Explicitly check for lints associated with 'closure_id', since
// it does not have a corresponding AST node // it does not have a corresponding AST node
if let ast_visit::FnKind::Fn(_, _, sig, _, _, _) = fk { if let ast_visit::FnKind::Fn(_, _, sig, _, _, _) = fk {
if let Some(coro_kind) = sig.header.coroutine_kind { if let Some(coroutine_kind) = sig.header.coroutine_kind {
let (ast::CoroutineKind::Async { closure_id, .. } self.check_id(coroutine_kind.closure_id());
| ast::CoroutineKind::Gen { closure_id, .. }
| ast::CoroutineKind::AsyncGen { closure_id, .. }) = coro_kind;
self.check_id(closure_id);
} }
} }
} }
@ -226,12 +223,10 @@ fn visit_expr_post(&mut self, e: &'a ast::Expr) {
// it does not have a corresponding AST node // it does not have a corresponding AST node
match e.kind { match e.kind {
ast::ExprKind::Closure(box ast::Closure { ast::ExprKind::Closure(box ast::Closure {
coroutine_kind: Some(coro_kind), .. coroutine_kind: Some(coroutine_kind),
..
}) => { }) => {
let (ast::CoroutineKind::Async { closure_id, .. } self.check_id(coroutine_kind.closure_id());
| ast::CoroutineKind::Gen { closure_id, .. }
| ast::CoroutineKind::AsyncGen { closure_id, .. }) = coro_kind;
self.check_id(closure_id);
} }
_ => {} _ => {}
} }

View File

@ -157,11 +157,7 @@ fn visit_item(&mut self, i: &'a Item) {
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) { fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
if let FnKind::Fn(_, _, sig, _, generics, body) = fn_kind { if let FnKind::Fn(_, _, sig, _, generics, body) = fn_kind {
match sig.header.coroutine_kind { match sig.header.coroutine_kind {
Some( Some(coroutine_kind) => {
CoroutineKind::Async { closure_id, .. }
| CoroutineKind::Gen { closure_id, .. }
| CoroutineKind::AsyncGen { closure_id, .. },
) => {
self.visit_generics(generics); self.visit_generics(generics);
// For async functions, we need to create their inner defs inside of a // For async functions, we need to create their inner defs inside of a
@ -176,8 +172,12 @@ fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
// then the closure_def will never be used, and we should avoid generating a // then the closure_def will never be used, and we should avoid generating a
// def-id for it. // def-id for it.
if let Some(body) = body { if let Some(body) = body {
let closure_def = let closure_def = self.create_def(
self.create_def(closure_id, kw::Empty, DefKind::Closure, span); coroutine_kind.closure_id(),
kw::Empty,
DefKind::Closure,
span,
);
self.with_parent(closure_def, |this| this.visit_block(body)); self.with_parent(closure_def, |this| this.visit_block(body));
} }
return; return;
@ -289,11 +289,12 @@ fn visit_expr(&mut self, expr: &'a Expr) {
// we must create two defs. // we must create two defs.
let closure_def = self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span); let closure_def = self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span);
match closure.coroutine_kind { match closure.coroutine_kind {
Some( Some(coroutine_kind) => self.create_def(
CoroutineKind::Async { closure_id, .. } coroutine_kind.closure_id(),
| CoroutineKind::Gen { closure_id, .. } kw::Empty,
| CoroutineKind::AsyncGen { closure_id, .. }, DefKind::Closure,
) => self.create_def(closure_id, kw::Empty, DefKind::Closure, expr.span), expr.span,
),
None => closure_def, None => closure_def,
} }
} }

View File

@ -3144,10 +3144,9 @@ fn note_obligation_cause_code<T>(
let what = match self.tcx.coroutine_kind(coroutine_def_id) { let what = match self.tcx.coroutine_kind(coroutine_def_id) {
None None
| Some(hir::CoroutineKind::Coroutine) | Some(hir::CoroutineKind::Coroutine)
| Some(hir::CoroutineKind::Gen(_)) | Some(hir::CoroutineKind::Gen(_)) => "yield",
// FIXME(gen_blocks): This could be yield or await...
| Some(hir::CoroutineKind::AsyncGen(_)) => "yield",
Some(hir::CoroutineKind::Async(..)) => "await", Some(hir::CoroutineKind::Async(..)) => "await",
Some(hir::CoroutineKind::AsyncGen(_)) => "yield`/`await",
}; };
err.note(format!( err.note(format!(
"all values live across `{what}` must have a statically known size" "all values live across `{what}` must have a statically known size"