Skip to content

Commit

Permalink
parse const closures
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed Jan 12, 2023
1 parent 8c8aa38 commit 19c2286
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};

pub(crate) fn rewrite_closure(
binder: &ast::ClosureBinder,
constness: ast::Const,
capture: ast::CaptureBy,
is_async: &ast::Async,
movability: ast::Movability,
Expand All @@ -38,7 +39,7 @@ pub(crate) fn rewrite_closure(
debug!("rewrite_closure {:?}", body);

let (prefix, extra_offset) = rewrite_closure_fn_decl(
binder, capture, is_async, movability, fn_decl, body, span, context, shape,
binder, constness, capture, is_async, movability, fn_decl, body, span, context, shape,
)?;
// 1 = space between `|...|` and body.
let body_shape = shape.offset_left(extra_offset)?;
Expand Down Expand Up @@ -230,6 +231,7 @@ fn rewrite_closure_block(
// Return type is (prefix, extra_offset)
fn rewrite_closure_fn_decl(
binder: &ast::ClosureBinder,
constness: ast::Const,
capture: ast::CaptureBy,
asyncness: &ast::Async,
movability: ast::Movability,
Expand All @@ -250,6 +252,12 @@ fn rewrite_closure_fn_decl(
ast::ClosureBinder::NotPresent => "".to_owned(),
};

let const_ = if matches!(constness, ast::Const::Yes(_)) {
"const "
} else {
""
};

let immovable = if movability == ast::Movability::Static {
"static "
} else {
Expand All @@ -264,7 +272,7 @@ fn rewrite_closure_fn_decl(
// 4 = "|| {".len(), which is overconservative when the closure consists of
// a single expression.
let nested_shape = shape
.shrink_left(binder.len() + immovable.len() + is_async.len() + mover.len())?
.shrink_left(binder.len() + const_.len() + immovable.len() + is_async.len() + mover.len())?
.sub_width(4)?;

// 1 = |
Expand Down Expand Up @@ -302,7 +310,10 @@ fn rewrite_closure_fn_decl(
.tactic(tactic)
.preserve_newline(true);
let list_str = write_list(&item_vec, &fmt)?;
let mut prefix = format!("{}{}{}{}|{}|", binder, immovable, is_async, mover, list_str);
let mut prefix = format!(
"{}{}{}{}{}|{}|",
binder, const_, immovable, is_async, mover, list_str
);

if !ret_str.is_empty() {
if prefix.contains('\n') {
Expand All @@ -329,6 +340,7 @@ pub(crate) fn rewrite_last_closure(
if let ast::ExprKind::Closure(ref closure) = expr.kind {
let ast::Closure {
ref binder,
constness,
capture_clause,
ref asyncness,
movability,
Expand All @@ -349,6 +361,7 @@ pub(crate) fn rewrite_last_closure(
};
let (prefix, extra_offset) = rewrite_closure_fn_decl(
binder,
constness,
capture_clause,
asyncness,
movability,
Expand Down
1 change: 1 addition & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ pub(crate) fn format_expr(
}
ast::ExprKind::Closure(ref cl) => closures::rewrite_closure(
&cl.binder,
cl.constness,
cl.capture_clause,
&cl.asyncness,
cl.movability,
Expand Down

0 comments on commit 19c2286

Please sign in to comment.