Skip to content

Commit 4fb10c0

Browse files
committed
parse const closures
1 parent 56ee65a commit 4fb10c0

File tree

16 files changed

+63
-12
lines changed

16 files changed

+63
-12
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,7 @@ impl Expr {
13071307
pub struct Closure {
13081308
pub binder: ClosureBinder,
13091309
pub capture_clause: CaptureBy,
1310+
pub constness: Const,
13101311
pub asyncness: Async,
13111312
pub movability: Movability,
13121313
pub fn_decl: P<FnDecl>,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
13621362
ExprKind::Closure(box Closure {
13631363
binder,
13641364
capture_clause: _,
1365+
constness,
13651366
asyncness,
13661367
movability: _,
13671368
fn_decl,
@@ -1370,6 +1371,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
13701371
fn_arg_span: _,
13711372
}) => {
13721373
vis.visit_closure_binder(binder);
1374+
visit_constness(constness, vis);
13731375
vis.visit_asyncness(asyncness);
13741376
vis.visit_fn_decl(fn_decl);
13751377
vis.visit_expr(body);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
836836
binder,
837837
capture_clause: _,
838838
asyncness: _,
839+
constness: _,
839840
movability: _,
840841
fn_decl,
841842
body,

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
209209
ExprKind::Closure(box Closure {
210210
binder,
211211
capture_clause,
212+
constness,
212213
asyncness,
213214
movability,
214215
fn_decl,
@@ -233,6 +234,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
233234
binder,
234235
*capture_clause,
235236
e.id,
237+
*constness,
236238
*movability,
237239
fn_decl,
238240
body,
@@ -651,6 +653,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
651653
fn_decl_span: self.lower_span(span),
652654
fn_arg_span: None,
653655
movability: Some(hir::Movability::Static),
656+
constness: hir::Constness::NotConst,
654657
});
655658

656659
hir::ExprKind::Closure(c)
@@ -890,6 +893,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
890893
binder: &ClosureBinder,
891894
capture_clause: CaptureBy,
892895
closure_id: NodeId,
896+
constness: Const,
893897
movability: Movability,
894898
decl: &FnDecl,
895899
body: &Expr,
@@ -927,6 +931,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
927931
fn_decl_span: self.lower_span(fn_decl_span),
928932
fn_arg_span: Some(self.lower_span(fn_arg_span)),
929933
movability: generator_option,
934+
constness: self.lower_constness(constness),
930935
});
931936

932937
hir::ExprKind::Closure(c)
@@ -1041,6 +1046,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10411046
fn_decl_span: self.lower_span(fn_decl_span),
10421047
fn_arg_span: Some(self.lower_span(fn_arg_span)),
10431048
movability: None,
1049+
constness: hir::Constness::NotConst,
10441050
});
10451051
hir::ExprKind::Closure(c)
10461052
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12391239
}
12401240
}
12411241

1242-
fn lower_constness(&mut self, c: Const) -> hir::Constness {
1242+
pub(super) fn lower_constness(&mut self, c: Const) -> hir::Constness {
12431243
match c {
12441244
Const::Yes(_) => hir::Constness::Const,
12451245
Const::No => hir::Constness::NotConst,

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ impl<'a> State<'a> {
399399
ast::ExprKind::Closure(box ast::Closure {
400400
binder,
401401
capture_clause,
402+
constness,
402403
asyncness,
403404
movability,
404405
fn_decl,
@@ -407,6 +408,7 @@ impl<'a> State<'a> {
407408
fn_arg_span: _,
408409
}) => {
409410
self.print_closure_binder(binder);
411+
self.print_constness(*constness);
410412
self.print_movability(*movability);
411413
self.print_asyncness(*asyncness);
412414
self.print_capture_clause(*capture_clause);

compiler/rustc_expand/src/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ impl<'a> ExtCtxt<'a> {
533533
ast::ExprKind::Closure(Box::new(ast::Closure {
534534
binder: ast::ClosureBinder::NotPresent,
535535
capture_clause: ast::CaptureBy::Ref,
536+
constness: ast::Const::No,
536537
asyncness: ast::Async::No,
537538
movability: ast::Movability::Movable,
538539
fn_decl,

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ pub struct Crate<'hir> {
938938
pub struct Closure<'hir> {
939939
pub def_id: LocalDefId,
940940
pub binder: ClosureBinder,
941+
pub constness: Constness,
941942
pub capture_clause: CaptureBy,
942943
pub bound_generic_params: &'hir [GenericParam<'hir>],
943944
pub fn_decl: &'hir FnDecl<'hir>,

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
742742
fn_decl_span: _,
743743
fn_arg_span: _,
744744
movability: _,
745+
constness: _,
745746
}) => {
746747
walk_list!(visitor, visit_generic_param, bound_generic_params);
747748
visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, expression.hir_id)

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,7 @@ impl<'a> State<'a> {
14641464
}
14651465
hir::ExprKind::Closure(&hir::Closure {
14661466
binder,
1467+
constness,
14671468
capture_clause,
14681469
bound_generic_params,
14691470
fn_decl,
@@ -1474,6 +1475,7 @@ impl<'a> State<'a> {
14741475
def_id: _,
14751476
}) => {
14761477
self.print_closure_binder(binder, bound_generic_params);
1478+
self.print_constness(constness);
14771479
self.print_capture_clause(capture_clause);
14781480

14791481
self.print_closure_params(fn_decl, body);
@@ -2272,10 +2274,7 @@ impl<'a> State<'a> {
22722274
}
22732275

22742276
pub fn print_fn_header_info(&mut self, header: hir::FnHeader) {
2275-
match header.constness {
2276-
hir::Constness::NotConst => {}
2277-
hir::Constness::Const => self.word_nbsp("const"),
2278-
}
2277+
self.print_constness(header.constness);
22792278

22802279
match header.asyncness {
22812280
hir::IsAsync::NotAsync => {}
@@ -2292,6 +2291,13 @@ impl<'a> State<'a> {
22922291
self.word("fn")
22932292
}
22942293

2294+
pub fn print_constness(&mut self, s: hir::Constness) {
2295+
match s {
2296+
hir::Constness::NotConst => {}
2297+
hir::Constness::Const => self.word_nbsp("const"),
2298+
}
2299+
}
2300+
22952301
pub fn print_unsafety(&mut self, s: hir::Unsafety) {
22962302
match s {
22972303
hir::Unsafety::Normal => {}

0 commit comments

Comments
 (0)