Skip to content

Commit 87e373e

Browse files
authored
Rollup merge of #98110 - cjgillot:closure-brace, r=Aaron1011
Make `ExprKind::Closure` a struct variant. Simple refactor since we both need it to introduce additional fields in `ExprKind::Closure`. r? ``@Aaron1011``
2 parents 40912e1 + 3039cfe commit 87e373e

File tree

75 files changed

+251
-216
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+251
-216
lines changed

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
577577
};
578578

579579
// The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`.
580-
let decl = self.arena.alloc(hir::FnDecl {
580+
let fn_decl = self.arena.alloc(hir::FnDecl {
581581
inputs: arena_vec![self; input_ty],
582582
output,
583583
c_variadic: false,
@@ -598,7 +598,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
598598
};
599599
let params = arena_vec![self; param];
600600

601-
let body_id = self.lower_body(move |this| {
601+
let body = self.lower_body(move |this| {
602602
this.generator_kind = Some(hir::GeneratorKind::Async(async_gen_kind));
603603

604604
let old_ctx = this.task_context;
@@ -609,13 +609,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
609609
});
610610

611611
// `static |_task_context| -> <ret_ty> { body }`:
612-
let generator_kind = hir::ExprKind::Closure(
612+
let generator_kind = hir::ExprKind::Closure {
613613
capture_clause,
614-
decl,
615-
body_id,
616-
self.lower_span(span),
617-
Some(hir::Movability::Static),
618-
);
614+
fn_decl,
615+
body,
616+
fn_decl_span: self.lower_span(span),
617+
movability: Some(hir::Movability::Static),
618+
};
619619
let generator = hir::Expr {
620620
hir_id: self.lower_node_id(closure_node_id),
621621
kind: generator_kind,
@@ -840,7 +840,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
840840
body: &Expr,
841841
fn_decl_span: Span,
842842
) -> hir::ExprKind<'hir> {
843-
let (body_id, generator_option) = self.with_new_scopes(move |this| {
843+
let (body, generator_option) = self.with_new_scopes(move |this| {
844844
let prev = this.current_item;
845845
this.current_item = Some(fn_decl_span);
846846
let mut generator_kind = None;
@@ -858,13 +858,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
858858
// Lower outside new scope to preserve `is_in_loop_condition`.
859859
let fn_decl = self.lower_fn_decl(decl, None, FnDeclKind::Closure, None);
860860

861-
hir::ExprKind::Closure(
861+
hir::ExprKind::Closure {
862862
capture_clause,
863863
fn_decl,
864-
body_id,
865-
self.lower_span(fn_decl_span),
866-
generator_option,
867-
)
864+
body,
865+
fn_decl_span: self.lower_span(fn_decl_span),
866+
movability: generator_option,
867+
}
868868
}
869869

870870
fn generator_movability_for_fn(
@@ -911,7 +911,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
911911
let outer_decl =
912912
FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
913913

914-
let body_id = self.with_new_scopes(|this| {
914+
let body = self.with_new_scopes(|this| {
915915
// FIXME(cramertj): allow `async` non-`move` closures with arguments.
916916
if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
917917
struct_span_err!(
@@ -950,13 +950,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
950950
// closure argument types.
951951
let fn_decl = self.lower_fn_decl(&outer_decl, None, FnDeclKind::Closure, None);
952952

953-
hir::ExprKind::Closure(
953+
hir::ExprKind::Closure {
954954
capture_clause,
955955
fn_decl,
956-
body_id,
957-
self.lower_span(fn_decl_span),
958-
None,
959-
)
956+
body,
957+
fn_decl_span: self.lower_span(fn_decl_span),
958+
movability: None,
959+
}
960960
}
961961

962962
/// Destructure the LHS of complex assignments.

Diff for: compiler/rustc_borrowck/src/diagnostics/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
896896
let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(local_did);
897897
let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind;
898898
debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
899-
if let hir::ExprKind::Closure(.., body_id, args_span, _) = expr {
899+
if let hir::ExprKind::Closure { body, fn_decl_span, .. } = expr {
900900
for (captured_place, place) in self
901901
.infcx
902902
.tcx
@@ -909,11 +909,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
909909
if target_place == place.as_ref() =>
910910
{
911911
debug!("closure_span: found captured local {:?}", place);
912-
let body = self.infcx.tcx.hir().body(*body_id);
912+
let body = self.infcx.tcx.hir().body(*body);
913913
let generator_kind = body.generator_kind();
914914

915915
return Some((
916-
*args_span,
916+
*fn_decl_span,
917917
generator_kind,
918918
captured_place.get_capture_kind_span(self.infcx.tcx),
919919
captured_place.get_path_span(self.infcx.tcx),

Diff for: compiler/rustc_borrowck/src/diagnostics/region_name.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
311311
// Can't have BrEnv in functions, constants or generators.
312312
bug!("BrEnv outside of closure.");
313313
};
314-
let hir::ExprKind::Closure(_, _, _, args_span, _) =
315-
tcx.hir().expect_expr(self.mir_hir_id()).kind else {
314+
let hir::ExprKind::Closure { fn_decl_span, .. }
315+
= tcx.hir().expect_expr(self.mir_hir_id()).kind
316+
else {
316317
bug!("Closure is not defined by a closure expr");
317318
};
318319
let region_name = self.synthesize_region_name();
@@ -336,7 +337,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
336337
Some(RegionName {
337338
name: region_name,
338339
source: RegionNameSource::SynthesizedFreeEnvRegion(
339-
args_span,
340+
fn_decl_span,
340341
note.to_string(),
341342
),
342343
})
@@ -683,16 +684,16 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
683684

684685
let (return_span, mir_description, hir_ty) = match hir.get(mir_hir_id) {
685686
hir::Node::Expr(hir::Expr {
686-
kind: hir::ExprKind::Closure(_, return_ty, body_id, span, _),
687+
kind: hir::ExprKind::Closure { fn_decl, body, fn_decl_span, .. },
687688
..
688689
}) => {
689-
let (mut span, mut hir_ty) = match return_ty.output {
690+
let (mut span, mut hir_ty) = match fn_decl.output {
690691
hir::FnRetTy::DefaultReturn(_) => {
691-
(tcx.sess.source_map().end_point(*span), None)
692+
(tcx.sess.source_map().end_point(*fn_decl_span), None)
692693
}
693-
hir::FnRetTy::Return(hir_ty) => (return_ty.output.span(), Some(hir_ty)),
694+
hir::FnRetTy::Return(hir_ty) => (fn_decl.output.span(), Some(hir_ty)),
694695
};
695-
let mir_description = match hir.body(*body_id).generator_kind {
696+
let mir_description = match hir.body(*body).generator_kind {
696697
Some(hir::GeneratorKind::Async(gen)) => match gen {
697698
hir::AsyncGeneratorKind::Block => " of async block",
698699
hir::AsyncGeneratorKind::Closure => " of async closure",
@@ -822,8 +823,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
822823

823824
let yield_span = match tcx.hir().get(self.mir_hir_id()) {
824825
hir::Node::Expr(hir::Expr {
825-
kind: hir::ExprKind::Closure(_, _, _, span, _), ..
826-
}) => (tcx.sess.source_map().end_point(*span)),
826+
kind: hir::ExprKind::Closure { fn_decl_span, .. },
827+
..
828+
}) => (tcx.sess.source_map().end_point(*fn_decl_span)),
827829
_ => self.body.span,
828830
};
829831

Diff for: compiler/rustc_hir/src/hir.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,7 @@ impl Expr<'_> {
16521652
ExprKind::Let(..) => ExprPrecedence::Let,
16531653
ExprKind::Loop(..) => ExprPrecedence::Loop,
16541654
ExprKind::Match(..) => ExprPrecedence::Match,
1655-
ExprKind::Closure(..) => ExprPrecedence::Closure,
1655+
ExprKind::Closure { .. } => ExprPrecedence::Closure,
16561656
ExprKind::Block(..) => ExprPrecedence::Block,
16571657
ExprKind::Assign(..) => ExprPrecedence::Assign,
16581658
ExprKind::AssignOp(..) => ExprPrecedence::AssignOp,
@@ -1712,7 +1712,7 @@ impl Expr<'_> {
17121712
| ExprKind::Tup(..)
17131713
| ExprKind::If(..)
17141714
| ExprKind::Match(..)
1715-
| ExprKind::Closure(..)
1715+
| ExprKind::Closure { .. }
17161716
| ExprKind::Block(..)
17171717
| ExprKind::Repeat(..)
17181718
| ExprKind::Array(..)
@@ -1795,7 +1795,7 @@ impl Expr<'_> {
17951795
| ExprKind::Match(..)
17961796
| ExprKind::MethodCall(..)
17971797
| ExprKind::Call(..)
1798-
| ExprKind::Closure(..)
1798+
| ExprKind::Closure { .. }
17991799
| ExprKind::Block(..)
18001800
| ExprKind::Repeat(..)
18011801
| ExprKind::Break(..)
@@ -1930,7 +1930,13 @@ pub enum ExprKind<'hir> {
19301930
///
19311931
/// This may also be a generator literal or an `async block` as indicated by the
19321932
/// `Option<Movability>`.
1933-
Closure(CaptureBy, &'hir FnDecl<'hir>, BodyId, Span, Option<Movability>),
1933+
Closure {
1934+
capture_clause: CaptureBy,
1935+
fn_decl: &'hir FnDecl<'hir>,
1936+
body: BodyId,
1937+
fn_decl_span: Span,
1938+
movability: Option<Movability>,
1939+
},
19341940
/// A block (e.g., `'label: { ... }`).
19351941
Block(&'hir Block<'hir>, Option<Label>),
19361942

@@ -3456,7 +3462,7 @@ impl<'hir> Node<'hir> {
34563462
_ => None,
34573463
},
34583464
Node::Expr(e) => match e.kind {
3459-
ExprKind::Closure(..) => Some(FnKind::Closure),
3465+
ExprKind::Closure { .. } => Some(FnKind::Closure),
34603466
_ => None,
34613467
},
34623468
_ => None,

Diff for: compiler/rustc_hir/src/intravisit.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1168,14 +1168,13 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
11681168
visitor.visit_expr(subexpression);
11691169
walk_list!(visitor, visit_arm, arms);
11701170
}
1171-
ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => visitor
1172-
.visit_fn(
1173-
FnKind::Closure,
1174-
function_declaration,
1175-
body,
1176-
expression.span,
1177-
expression.hir_id,
1178-
),
1171+
ExprKind::Closure {
1172+
ref fn_decl,
1173+
body,
1174+
capture_clause: _,
1175+
fn_decl_span: _,
1176+
movability: _,
1177+
} => visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, expression.hir_id),
11791178
ExprKind::Block(ref block, ref opt_label) => {
11801179
walk_list!(visitor, visit_label, opt_label);
11811180
visitor.visit_block(block);

Diff for: compiler/rustc_hir_pretty/src/lib.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,9 @@ impl<'a> State<'a> {
10781078
// parses as the erroneous construct `if (return {})`, not `if (return) {}`.
10791079
fn cond_needs_par(expr: &hir::Expr<'_>) -> bool {
10801080
match expr.kind {
1081-
hir::ExprKind::Break(..) | hir::ExprKind::Closure(..) | hir::ExprKind::Ret(..) => true,
1081+
hir::ExprKind::Break(..) | hir::ExprKind::Closure { .. } | hir::ExprKind::Ret(..) => {
1082+
true
1083+
}
10821084
_ => contains_exterior_struct_lit(expr),
10831085
}
10841086
}
@@ -1455,10 +1457,16 @@ impl<'a> State<'a> {
14551457
}
14561458
self.bclose(expr.span);
14571459
}
1458-
hir::ExprKind::Closure(capture_clause, ref decl, body, _fn_decl_span, _gen) => {
1460+
hir::ExprKind::Closure {
1461+
capture_clause,
1462+
ref fn_decl,
1463+
body,
1464+
fn_decl_span: _,
1465+
movability: _,
1466+
} => {
14591467
self.print_capture_clause(capture_clause);
14601468

1461-
self.print_closure_params(&decl, body);
1469+
self.print_closure_params(&fn_decl, body);
14621470
self.space();
14631471

14641472
// This is a bare expression.

Diff for: compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -988,22 +988,24 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
988988
}
989989

990990
if let Some(node_ty) = self.opt_node_type(expr.hir_id) {
991-
if let (&ExprKind::Closure(_, decl, body_id, span, _), ty::Closure(_, substs)) =
992-
(&expr.kind, node_ty.kind())
991+
if let (
992+
&ExprKind::Closure { fn_decl, body, fn_decl_span, .. },
993+
ty::Closure(_, substs),
994+
) = (&expr.kind, node_ty.kind())
993995
{
994996
let output = substs.as_closure().sig().output().skip_binder();
995997
if self.generic_arg_contains_target(output.into()) {
996-
let body = self.infcx.tcx.hir().body(body_id);
998+
let body = self.infcx.tcx.hir().body(body);
997999
let should_wrap_expr = if matches!(body.value.kind, ExprKind::Block(..)) {
9981000
None
9991001
} else {
10001002
Some(body.value.span.shrink_to_hi())
10011003
};
10021004
self.update_infer_source(InferSource {
1003-
span,
1005+
span: fn_decl_span,
10041006
kind: InferSourceKind::ClosureReturn {
10051007
ty: output,
1006-
data: &decl.output,
1008+
data: &fn_decl.output,
10071009
should_wrap_expr,
10081010
},
10091011
})

Diff for: compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn find_param_with_region<'tcx>(
5555

5656
// Don't perform this on closures
5757
match hir.get(hir_id) {
58-
hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {
58+
hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
5959
return None;
6060
}
6161
_ => {}

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2027,7 +2027,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20272027
}
20282028

20292029
fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) {
2030-
if let hir::ExprKind::Closure(..) = expr.kind {
2030+
if let hir::ExprKind::Closure { .. } = expr.kind {
20312031
self.encode_info_for_closure(expr.hir_id);
20322032
}
20332033
}

Diff for: compiler/rustc_middle/src/hir/map/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn fn_decl<'hir>(node: Node<'hir>) -> Option<&'hir FnDecl<'hir>> {
2222
Node::Item(Item { kind: ItemKind::Fn(sig, _, _), .. })
2323
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), .. })
2424
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, _), .. }) => Some(&sig.decl),
25-
Node::Expr(Expr { kind: ExprKind::Closure(_, fn_decl, ..), .. })
25+
Node::Expr(Expr { kind: ExprKind::Closure { fn_decl, .. }, .. })
2626
| Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(fn_decl, ..), .. }) => {
2727
Some(fn_decl)
2828
}
@@ -54,7 +54,7 @@ pub fn associated_body<'hir>(node: Node<'hir>) -> Option<BodyId> {
5454
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
5555
..
5656
})
57-
| Node::Expr(Expr { kind: ExprKind::Closure(.., body, _, _), .. }) => Some(*body),
57+
| Node::Expr(Expr { kind: ExprKind::Closure { body, .. }, .. }) => Some(*body),
5858

5959
Node::AnonConst(constant) => Some(constant.body),
6060

@@ -285,8 +285,8 @@ impl<'hir> Map<'hir> {
285285
}
286286
Node::Field(_) => DefKind::Field,
287287
Node::Expr(expr) => match expr.kind {
288-
ExprKind::Closure(.., None) => DefKind::Closure,
289-
ExprKind::Closure(.., Some(_)) => DefKind::Generator,
288+
ExprKind::Closure { movability: None, .. } => DefKind::Closure,
289+
ExprKind::Closure { movability: Some(_), .. } => DefKind::Generator,
290290
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
291291
},
292292
Node::GenericParam(param) => match param.kind {
@@ -758,7 +758,7 @@ impl<'hir> Map<'hir> {
758758
Node::Item(_)
759759
| Node::ForeignItem(_)
760760
| Node::TraitItem(_)
761-
| Node::Expr(Expr { kind: ExprKind::Closure(..), .. })
761+
| Node::Expr(Expr { kind: ExprKind::Closure { .. }, .. })
762762
| Node::ImplItem(_) => return Some(hir_id),
763763
// Ignore `return`s on the first iteration
764764
Node::Expr(Expr { kind: ExprKind::Loop(..) | ExprKind::Ret(..), .. })

Diff for: compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ impl<'tcx> TyCtxt<'tcx> {
15501550
Node::Item(&hir::Item { kind: ItemKind::Fn(..), .. }) => {}
15511551
Node::TraitItem(&hir::TraitItem { kind: TraitItemKind::Fn(..), .. }) => {}
15521552
Node::ImplItem(&hir::ImplItem { kind: ImplItemKind::Fn(..), .. }) => {}
1553-
Node::Expr(&hir::Expr { kind: ExprKind::Closure(..), .. }) => {}
1553+
Node::Expr(&hir::Expr { kind: ExprKind::Closure { .. }, .. }) => {}
15541554
_ => return None,
15551555
}
15561556

Diff for: compiler/rustc_mir_build/src/build/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
6868

6969
// Figure out what primary body this item has.
7070
let (body_id, return_ty_span, span_with_body) = match tcx.hir().get(id) {
71-
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_, decl, body_id, _, _), .. }) => {
72-
(*body_id, decl.output.span(), None)
71+
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure { fn_decl, body, .. }, .. }) => {
72+
(*body, fn_decl.output.span(), None)
7373
}
7474
Node::Item(hir::Item {
7575
kind: hir::ItemKind::Fn(hir::FnSig { decl, .. }, _, body_id),

Diff for: compiler/rustc_mir_build/src/thir/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ impl<'tcx> Cx<'tcx> {
418418
}
419419
},
420420

421-
hir::ExprKind::Closure(..) => {
421+
hir::ExprKind::Closure { .. } => {
422422
let closure_ty = self.typeck_results().expr_ty(expr);
423423
let (def_id, substs, movability) = match *closure_ty.kind() {
424424
ty::Closure(def_id, substs) => (def_id, UpvarSubsts::Closure(substs), None),

Diff for: compiler/rustc_passes/src/check_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2357,7 +2357,7 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
23572357

23582358
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
23592359
let target = match expr.kind {
2360-
hir::ExprKind::Closure(..) => Target::Closure,
2360+
hir::ExprKind::Closure { .. } => Target::Closure,
23612361
_ => Target::Expression,
23622362
};
23632363

0 commit comments

Comments
 (0)