Skip to content

Commit 50ef800

Browse files
committed
Address code review feedback
1 parent 26f9954 commit 50ef800

File tree

10 files changed

+53
-36
lines changed

10 files changed

+53
-36
lines changed

compiler/rustc_ast/src/mut_visit.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,9 @@ pub fn noop_visit_closure_binder<T: MutVisitor>(binder: &mut ClosureBinder, vis:
873873

874874
pub fn noop_visit_coro_kind<T: MutVisitor>(coro_kind: &mut CoroutineKind, vis: &mut T) {
875875
match coro_kind {
876-
CoroutineKind::Async { span: _, closure_id, return_impl_trait_id }
877-
| CoroutineKind::Gen { span: _, closure_id, return_impl_trait_id } => {
876+
CoroutineKind::Async { span, closure_id, return_impl_trait_id }
877+
| CoroutineKind::Gen { span, closure_id, return_impl_trait_id } => {
878+
vis.visit_span(span);
878879
vis.visit_id(closure_id);
879880
vis.visit_id(return_impl_trait_id);
880881
}

compiler/rustc_ast_lowering/src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19221922
span,
19231923
opaque_ty_span,
19241924
|this| {
1925-
let future_bound = this.lower_coroutine_fn_output_type_to_future_bound(
1925+
let bound = this.lower_coroutine_fn_output_type_to_bound(
19261926
output,
19271927
coro,
19281928
span,
@@ -1931,7 +1931,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19311931
fn_kind,
19321932
},
19331933
);
1934-
arena_vec![this; future_bound]
1934+
arena_vec![this; bound]
19351935
},
19361936
);
19371937

@@ -1940,7 +1940,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19401940
}
19411941

19421942
/// Transforms `-> T` into `Future<Output = T>`.
1943-
fn lower_coroutine_fn_output_type_to_future_bound(
1943+
fn lower_coroutine_fn_output_type_to_bound(
19441944
&mut self,
19451945
output: &FnRetTy,
19461946
coro: CoroutineKind,
@@ -1958,21 +1958,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19581958
FnRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
19591959
};
19601960

1961-
// "<Output|Item = T>"
1962-
let (symbol, lang_item) = match coro {
1961+
// "<$assoc_ty_name = T>"
1962+
let (assoc_ty_name, trait_lang_item) = match coro {
19631963
CoroutineKind::Async { .. } => (hir::FN_OUTPUT_NAME, hir::LangItem::Future),
19641964
CoroutineKind::Gen { .. } => (hir::ITERATOR_ITEM_NAME, hir::LangItem::Iterator),
19651965
};
19661966

19671967
let future_args = self.arena.alloc(hir::GenericArgs {
19681968
args: &[],
1969-
bindings: arena_vec![self; self.assoc_ty_binding(symbol, span, output_ty)],
1969+
bindings: arena_vec![self; self.assoc_ty_binding(assoc_ty_name, span, output_ty)],
19701970
parenthesized: hir::GenericArgsParentheses::No,
19711971
span_ext: DUMMY_SP,
19721972
});
19731973

19741974
hir::GenericBound::LangItemTrait(
1975-
lang_item,
1975+
trait_lang_item,
19761976
self.lower_span(span),
19771977
self.next_id(),
19781978
future_args,

compiler/rustc_ast_lowering/src/path.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
401401
)
402402
}
403403

404-
/// An associated type binding `$symbol = $ty`.
404+
/// An associated type binding `$assoc_ty_name = $ty`.
405405
pub(crate) fn assoc_ty_binding(
406406
&mut self,
407-
symbol: rustc_span::Symbol,
407+
assoc_ty_name: rustc_span::Symbol,
408408
span: Span,
409409
ty: &'hir hir::Ty<'hir>,
410410
) -> hir::TypeBinding<'hir> {
411-
let ident = Ident::with_dummy_span(symbol);
411+
let ident = Ident::with_dummy_span(assoc_ty_name);
412412
let kind = hir::TypeBindingKind::Equality { term: ty.into() };
413413
let args = arena_vec![self;];
414414
let bindings = arena_vec![self;];

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12791279
..
12801280
}) = fk.header()
12811281
{
1282-
// FIXME(eholk): Report a different error for `const gen`
1282+
// FIXME(gen_blocks): Report a different error for `const gen`
12831283
self.err_handler().emit_err(errors::ConstAndAsync {
12841284
spans: vec![cspan, aspan],
12851285
cspan,

compiler/rustc_builtin_macros/src/test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,10 @@ fn check_test_signature(
545545
return Err(sd.emit_err(errors::TestBadFn { span: i.span, cause: span, kind: "async" }));
546546
}
547547

548+
if let Some(ast::CoroutineKind::Gen { span, .. }) = f.sig.header.coro_kind {
549+
return Err(sd.emit_err(errors::TestBadFn { span: i.span, cause: span, kind: "gen" }));
550+
}
551+
548552
// If the termination trait is active, the compiler will check that the output
549553
// type implements the `Termination` trait as `libtest` enforces that.
550554
let has_output = match &f.sig.decl.output {

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,7 @@ impl<'a> Parser<'a> {
25442544
}
25452545
}
25462546

2547-
// FIXME(eholk): add keyword recovery logic for genness
2547+
// FIXME(gen_blocks): add keyword recovery logic for genness
25482548

25492549
if wrong_kw.is_some()
25502550
&& self.may_recover()

compiler/rustc_parse/src/parser/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ impl<'a> Parser<'a> {
612612
if let Some(ast::CoroutineKind::Async { span, .. }) = coro_kind {
613613
self.sess.emit_err(FnPointerCannotBeAsync { span: whole_span, qualifier: span });
614614
}
615-
// FIXME(eholk): emit a similar error for `gen fn()`
615+
// FIXME(gen_blocks): emit a similar error for `gen fn()`
616616
let decl_span = span_start.to(self.token.span);
617617
Ok(TyKind::BareFn(P(BareFnTy { ext, unsafety, generic_params: params, decl, decl_span })))
618618
}

compiler/rustc_resolve/src/late.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -916,10 +916,10 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
916916
&sig.decl.output,
917917
);
918918

919-
if let Some((async_node_id, _)) =
919+
if let Some((coro_node_id, _)) =
920920
sig.header.coro_kind.map(|coro_kind| coro_kind.return_id())
921921
{
922-
this.record_lifetime_params_for_impl_trait(async_node_id);
922+
this.record_lifetime_params_for_impl_trait(coro_node_id);
923923
}
924924
},
925925
);
@@ -942,13 +942,13 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
942942
this.visit_generics(generics);
943943

944944
let declaration = &sig.decl;
945-
let async_node_id =
945+
let coro_node_id =
946946
sig.header.coro_kind.map(|coro_kind| coro_kind.return_id());
947947

948948
this.with_lifetime_rib(
949949
LifetimeRibKind::AnonymousCreateParameter {
950950
binder: fn_id,
951-
report_in_path: async_node_id.is_some(),
951+
report_in_path: coro_node_id.is_some(),
952952
},
953953
|this| {
954954
this.resolve_fn_signature(
@@ -961,7 +961,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
961961
&declaration.output,
962962
);
963963

964-
if let Some((async_node_id, _)) = async_node_id {
964+
if let Some((async_node_id, _)) = coro_node_id {
965965
this.record_lifetime_params_for_impl_trait(async_node_id);
966966
}
967967
},
@@ -4291,8 +4291,10 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
42914291
// `async |x| ...` gets desugared to `|x| async {...}`, so we need to
42924292
// resolve the arguments within the proper scopes so that usages of them inside the
42934293
// closure are detected as upvars rather than normal closure arg usages.
4294+
//
4295+
// Similarly, `gen |x| ...` gets desugared to `|x| gen {...}`, so we handle that too.
42944296
ExprKind::Closure(box ast::Closure {
4295-
coro_kind: Some(CoroutineKind::Async { .. }),
4297+
coro_kind: Some(_),
42964298
ref fn_decl,
42974299
ref body,
42984300
..

src/tools/rustfmt/src/closures.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,10 @@ fn rewrite_closure_fn_decl(
263263
} else {
264264
""
265265
};
266-
let (is_async, is_gen) = if let Some(coro_kind) = coro_kind {
267-
let is_async = if coro_kind.is_async() { "async " } else { "" };
268-
let is_gen = if coro_kind.is_gen() { "gen " } else { "" };
269-
(is_async, is_gen)
270-
} else {
271-
("", "")
266+
let coro = match coro_kind {
267+
Some(ast::CoroutineKind::Async { .. }) => "async ",
268+
Some(ast::CoroutineKind::Gen { .. }) => "gen ",
269+
None => "",
272270
};
273271
let mover = if matches!(capture, ast::CaptureBy::Value { .. }) {
274272
"move "
@@ -278,14 +276,7 @@ fn rewrite_closure_fn_decl(
278276
// 4 = "|| {".len(), which is overconservative when the closure consists of
279277
// a single expression.
280278
let nested_shape = shape
281-
.shrink_left(
282-
binder.len()
283-
+ const_.len()
284-
+ immovable.len()
285-
+ is_async.len()
286-
+ is_gen.len()
287-
+ mover.len(),
288-
)?
279+
.shrink_left(binder.len() + const_.len() + immovable.len() + coro.len() + mover.len())?
289280
.sub_width(4)?;
290281

291282
// 1 = |
@@ -323,7 +314,7 @@ fn rewrite_closure_fn_decl(
323314
.tactic(tactic)
324315
.preserve_newline(true);
325316
let list_str = write_list(&item_vec, &fmt)?;
326-
let mut prefix = format!("{binder}{const_}{immovable}{is_async}{is_gen}{mover}|{list_str}|");
317+
let mut prefix = format!("{binder}{const_}{immovable}{coro}{mover}|{list_str}|");
327318

328319
if !ret_str.is_empty() {
329320
if prefix.contains('\n') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// edition: 2024
2+
// compile-flags: -Zunstable-options
3+
// check-pass
4+
#![feature(gen_blocks)]
5+
6+
// make sure gen fn captures lifetimes in its signature
7+
8+
gen fn foo<'a, 'b>(x: &'a i32, y: &'b i32, z: &'b i32) -> &'b i32 {
9+
yield y;
10+
yield z;
11+
}
12+
13+
fn main() {
14+
let z = 3;
15+
let mut iter = foo(&1, &2, &z);
16+
assert_eq!(iter.next(), Some(&2));
17+
assert_eq!(iter.next(), Some(&3));
18+
assert_eq!(iter.next(), None);
19+
}

0 commit comments

Comments
 (0)