From 675d9406fb773815c208b097c9003a3372e91721 Mon Sep 17 00:00:00 2001 From: jhilton Date: Fri, 23 Feb 2024 20:41:22 -0500 Subject: [PATCH] Add simple formatting support for cilk keywords --- src/tools/rustfmt/src/expr.rs | 26 ++++++++++++++++++++++++++ src/tools/rustfmt/src/utils.rs | 4 +++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/tools/rustfmt/src/expr.rs b/src/tools/rustfmt/src/expr.rs index 4b86c2acdc538..d224e3bcf5367 100644 --- a/src/tools/rustfmt/src/expr.rs +++ b/src/tools/rustfmt/src/expr.rs @@ -242,6 +242,7 @@ pub(crate) fn format_expr( ast::ExprKind::Yeet(Some(ref expr)) => { rewrite_unary_prefix(context, "do yeet ", &**expr, shape) } + ast::ExprKind::CilkSync => Some("cilk_sync".to_owned()), ast::ExprKind::AddrOf(borrow_kind, mutability, ref expr) => { rewrite_expr_addrof(context, borrow_kind, mutability, expr, shape) } @@ -397,6 +398,31 @@ pub(crate) fn format_expr( )) } } + // TODO(jhilton): add a test for cilk_spawn once we have the rest of parsing working! + ast::ExprKind::CilkSpawn(ref block) => { + if let rw @ Some(_) = rewrite_single_line_block( + context, + "cilk_spawn", + block, + Some(&expr.attrs), + None, + shape, + ) { + rw + } else { + let budget = shape.width.saturating_sub(6); + Some(format!( + "cilk_spawn {}", + rewrite_block( + block, + Some(&expr.attrs), + None, + context, + Shape::legacy(budget, shape.indent) + )? + )) + } + } ast::ExprKind::Underscore => Some("_".to_owned()), ast::ExprKind::FormatArgs(..) | ast::ExprKind::IncludedBytes(..) diff --git a/src/tools/rustfmt/src/utils.rs b/src/tools/rustfmt/src/utils.rs index 642b6603b1e9a..25ecf0bda6cf5 100644 --- a/src/tools/rustfmt/src/utils.rs +++ b/src/tools/rustfmt/src/utils.rs @@ -478,6 +478,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr | ast::ExprKind::Loop(..) | ast::ExprKind::ForLoop { .. } | ast::ExprKind::TryBlock(..) + | ast::ExprKind::CilkSpawn(..) | ast::ExprKind::Match(..) => repr.contains('\n'), ast::ExprKind::Paren(ref expr) | ast::ExprKind::Binary(_, _, ref expr) @@ -512,7 +513,8 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr | ast::ExprKind::Tup(..) | ast::ExprKind::Type(..) | ast::ExprKind::Yield(None) - | ast::ExprKind::Underscore => false, + | ast::ExprKind::Underscore + | ast::ExprKind::CilkSync => false, } }