Skip to content

Commit 3039cfe

Browse files
committed
Make ExprKind::Closure a struct variant.
1 parent fa68e73 commit 3039cfe

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
@@ -1651,7 +1651,7 @@ impl Expr<'_> {
16511651
ExprKind::Let(..) => ExprPrecedence::Let,
16521652
ExprKind::Loop(..) => ExprPrecedence::Loop,
16531653
ExprKind::Match(..) => ExprPrecedence::Match,
1654-
ExprKind::Closure(..) => ExprPrecedence::Closure,
1654+
ExprKind::Closure { .. } => ExprPrecedence::Closure,
16551655
ExprKind::Block(..) => ExprPrecedence::Block,
16561656
ExprKind::Assign(..) => ExprPrecedence::Assign,
16571657
ExprKind::AssignOp(..) => ExprPrecedence::AssignOp,
@@ -1711,7 +1711,7 @@ impl Expr<'_> {
17111711
| ExprKind::Tup(..)
17121712
| ExprKind::If(..)
17131713
| ExprKind::Match(..)
1714-
| ExprKind::Closure(..)
1714+
| ExprKind::Closure { .. }
17151715
| ExprKind::Block(..)
17161716
| ExprKind::Repeat(..)
17171717
| ExprKind::Array(..)
@@ -1794,7 +1794,7 @@ impl Expr<'_> {
17941794
| ExprKind::Match(..)
17951795
| ExprKind::MethodCall(..)
17961796
| ExprKind::Call(..)
1797-
| ExprKind::Closure(..)
1797+
| ExprKind::Closure { .. }
17981798
| ExprKind::Block(..)
17991799
| ExprKind::Repeat(..)
18001800
| ExprKind::Break(..)
@@ -1929,7 +1929,13 @@ pub enum ExprKind<'hir> {
19291929
///
19301930
/// This may also be a generator literal or an `async block` as indicated by the
19311931
/// `Option<Movability>`.
1932-
Closure(CaptureBy, &'hir FnDecl<'hir>, BodyId, Span, Option<Movability>),
1932+
Closure {
1933+
capture_clause: CaptureBy,
1934+
fn_decl: &'hir FnDecl<'hir>,
1935+
body: BodyId,
1936+
fn_decl_span: Span,
1937+
movability: Option<Movability>,
1938+
},
19331939
/// A block (e.g., `'label: { ... }`).
19341940
Block(&'hir Block<'hir>, Option<Label>),
19351941

@@ -3455,7 +3461,7 @@ impl<'hir> Node<'hir> {
34553461
_ => None,
34563462
},
34573463
Node::Expr(e) => match e.kind {
3458-
ExprKind::Closure(..) => Some(FnKind::Closure),
3464+
ExprKind::Closure { .. } => Some(FnKind::Closure),
34593465
_ => None,
34603466
},
34613467
_ => 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
@@ -991,22 +991,24 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
991991
}
992992

993993
if let Some(node_ty) = self.opt_node_type(expr.hir_id) {
994-
if let (&ExprKind::Closure(_, decl, body_id, span, _), ty::Closure(_, substs)) =
995-
(&expr.kind, node_ty.kind())
994+
if let (
995+
&ExprKind::Closure { fn_decl, body, fn_decl_span, .. },
996+
ty::Closure(_, substs),
997+
) = (&expr.kind, node_ty.kind())
996998
{
997999
let output = substs.as_closure().sig().output().skip_binder();
9981000
if self.generic_arg_contains_target(output.into()) {
999-
let body = self.infcx.tcx.hir().body(body_id);
1001+
let body = self.infcx.tcx.hir().body(body);
10001002
let should_wrap_expr = if matches!(body.value.kind, ExprKind::Block(..)) {
10011003
None
10021004
} else {
10031005
Some(body.value.span.shrink_to_hi())
10041006
};
10051007
self.update_infer_source(InferSource {
1006-
span,
1008+
span: fn_decl_span,
10071009
kind: InferSourceKind::ClosureReturn {
10081010
ty: output,
1009-
data: &decl.output,
1011+
data: &fn_decl.output,
10101012
should_wrap_expr,
10111013
},
10121014
})

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
@@ -2030,7 +2030,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20302030
}
20312031

20322032
fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) {
2033-
if let hir::ExprKind::Closure(..) = expr.kind {
2033+
if let hir::ExprKind::Closure { .. } = expr.kind {
20342034
self.encode_info_for_closure(expr.hir_id);
20352035
}
20362036
}

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
@@ -66,8 +66,8 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
6666

6767
// Figure out what primary body this item has.
6868
let (body_id, return_ty_span, span_with_body) = match tcx.hir().get(id) {
69-
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_, decl, body_id, _, _), .. }) => {
70-
(*body_id, decl.output.span(), None)
69+
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure { fn_decl, body, .. }, .. }) => {
70+
(*body, fn_decl.output.span(), None)
7171
}
7272
Node::Item(hir::Item {
7373
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
@@ -2340,7 +2340,7 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
23402340

23412341
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
23422342
let target = match expr.kind {
2343-
hir::ExprKind::Closure(..) => Target::Closure,
2343+
hir::ExprKind::Closure { .. } => Target::Closure,
23442344
_ => Target::Expression,
23452345
};
23462346

0 commit comments

Comments
 (0)