Skip to content

Commit 19c2286

Browse files
committed
parse const closures
1 parent 8c8aa38 commit 19c2286

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/closures.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};
2626

2727
pub(crate) fn rewrite_closure(
2828
binder: &ast::ClosureBinder,
29+
constness: ast::Const,
2930
capture: ast::CaptureBy,
3031
is_async: &ast::Async,
3132
movability: ast::Movability,
@@ -38,7 +39,7 @@ pub(crate) fn rewrite_closure(
3839
debug!("rewrite_closure {:?}", body);
3940

4041
let (prefix, extra_offset) = rewrite_closure_fn_decl(
41-
binder, capture, is_async, movability, fn_decl, body, span, context, shape,
42+
binder, constness, capture, is_async, movability, fn_decl, body, span, context, shape,
4243
)?;
4344
// 1 = space between `|...|` and body.
4445
let body_shape = shape.offset_left(extra_offset)?;
@@ -230,6 +231,7 @@ fn rewrite_closure_block(
230231
// Return type is (prefix, extra_offset)
231232
fn rewrite_closure_fn_decl(
232233
binder: &ast::ClosureBinder,
234+
constness: ast::Const,
233235
capture: ast::CaptureBy,
234236
asyncness: &ast::Async,
235237
movability: ast::Movability,
@@ -250,6 +252,12 @@ fn rewrite_closure_fn_decl(
250252
ast::ClosureBinder::NotPresent => "".to_owned(),
251253
};
252254

255+
let const_ = if matches!(constness, ast::Const::Yes(_)) {
256+
"const "
257+
} else {
258+
""
259+
};
260+
253261
let immovable = if movability == ast::Movability::Static {
254262
"static "
255263
} else {
@@ -264,7 +272,7 @@ fn rewrite_closure_fn_decl(
264272
// 4 = "|| {".len(), which is overconservative when the closure consists of
265273
// a single expression.
266274
let nested_shape = shape
267-
.shrink_left(binder.len() + immovable.len() + is_async.len() + mover.len())?
275+
.shrink_left(binder.len() + const_.len() + immovable.len() + is_async.len() + mover.len())?
268276
.sub_width(4)?;
269277

270278
// 1 = |
@@ -302,7 +310,10 @@ fn rewrite_closure_fn_decl(
302310
.tactic(tactic)
303311
.preserve_newline(true);
304312
let list_str = write_list(&item_vec, &fmt)?;
305-
let mut prefix = format!("{}{}{}{}|{}|", binder, immovable, is_async, mover, list_str);
313+
let mut prefix = format!(
314+
"{}{}{}{}{}|{}|",
315+
binder, const_, immovable, is_async, mover, list_str
316+
);
306317

307318
if !ret_str.is_empty() {
308319
if prefix.contains('\n') {
@@ -329,6 +340,7 @@ pub(crate) fn rewrite_last_closure(
329340
if let ast::ExprKind::Closure(ref closure) = expr.kind {
330341
let ast::Closure {
331342
ref binder,
343+
constness,
332344
capture_clause,
333345
ref asyncness,
334346
movability,
@@ -349,6 +361,7 @@ pub(crate) fn rewrite_last_closure(
349361
};
350362
let (prefix, extra_offset) = rewrite_closure_fn_decl(
351363
binder,
364+
constness,
352365
capture_clause,
353366
asyncness,
354367
movability,

src/expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ pub(crate) fn format_expr(
205205
}
206206
ast::ExprKind::Closure(ref cl) => closures::rewrite_closure(
207207
&cl.binder,
208+
cl.constness,
208209
cl.capture_clause,
209210
&cl.asyncness,
210211
cl.movability,

0 commit comments

Comments
 (0)