Skip to content

Commit 903de92

Browse files
committed
Avoid cloning RewriteContext
1 parent 2fbdedb commit 903de92

File tree

7 files changed

+42
-24
lines changed

7 files changed

+42
-24
lines changed

src/closures.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn rewrite_closure_expr(
170170

171171
// When rewriting closure's body without block, we require it to fit in a single line
172172
// unless it is a block-like expression or we are inside macro call.
173-
let veto_multiline = (!allow_multi_line(expr) && !context.inside_macro)
173+
let veto_multiline = (!allow_multi_line(expr) && !context.inside_macro())
174174
|| context.config.force_multiline_blocks();
175175
expr.rewrite(context, shape)
176176
.and_then(|rw| {
@@ -370,7 +370,7 @@ where
370370

371371
fn is_block_closure_forced(context: &RewriteContext, expr: &ast::Expr) -> bool {
372372
// If we are inside macro, we do not want to add or remove block from closure body.
373-
if context.inside_macro {
373+
if context.inside_macro() {
374374
false
375375
} else {
376376
is_block_closure_forced_inner(expr)

src/expr.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
474474
separator: ",",
475475
trailing_separator: if trailing_comma {
476476
SeparatorTactic::Always
477-
} else if context.inside_macro && !exprs.is_empty() {
477+
} else if context.inside_macro() && !exprs.is_empty() {
478478
let ends_with_bracket = context.snippet(span).ends_with(']');
479479
let bracket_offset = if ends_with_bracket { 1 } else { 0 };
480480
let snippet = context.snippet(mk_sp(span.lo(), span.hi() - BytePos(bracket_offset)));
@@ -656,7 +656,7 @@ pub fn rewrite_block_with_visitor(
656656

657657
let mut visitor = FmtVisitor::from_context(context);
658658
visitor.block_indent = shape.indent;
659-
visitor.is_if_else_block = context.is_if_else_block;
659+
visitor.is_if_else_block = context.is_if_else_block();
660660
match block.rules {
661661
ast::BlockCheckMode::Unsafe(..) => {
662662
let snippet = context.snippet(block.span);
@@ -1142,10 +1142,13 @@ impl<'a> Rewrite for ControlFlow<'a> {
11421142
width: block_width,
11431143
..shape
11441144
};
1145-
let mut block_context = context.clone();
1146-
block_context.is_if_else_block = self.else_block.is_some();
1147-
let block_str =
1148-
rewrite_block_with_visitor(&block_context, "", self.block, None, block_shape, true)?;
1145+
let block_str = {
1146+
let old_val = context.is_if_else_block.replace(self.else_block.is_some());
1147+
let result =
1148+
rewrite_block_with_visitor(context, "", self.block, None, block_shape, true);
1149+
context.is_if_else_block.replace(old_val);
1150+
result?
1151+
};
11491152

11501153
let mut result = format!("{}{}", cond_str, block_str);
11511154

@@ -1456,7 +1459,7 @@ pub fn rewrite_call(
14561459
shape,
14571460
span,
14581461
context.config.width_heuristics().fn_call_width,
1459-
if context.inside_macro {
1462+
if context.inside_macro() {
14601463
if span_ends_with_comma(context, span) {
14611464
Some(SeparatorTactic::Always)
14621465
} else {
@@ -1768,7 +1771,7 @@ fn rewrite_struct_lit<'a>(
17681771
let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
17691772

17701773
let ends_with_comma = span_ends_with_comma(context, span);
1771-
let force_no_trailing_comma = if context.inside_macro && !ends_with_comma {
1774+
let force_no_trailing_comma = if context.inside_macro() && !ends_with_comma {
17721775
true
17731776
} else {
17741777
false
@@ -1947,7 +1950,7 @@ where
19471950
debug!("rewrite_tuple {:?}", shape);
19481951
if context.use_block_indent() {
19491952
// We use the same rule as function calls for rewriting tuples.
1950-
let force_tactic = if context.inside_macro {
1953+
let force_tactic = if context.inside_macro() {
19511954
if span_ends_with_comma(context, span) {
19521955
Some(SeparatorTactic::Always)
19531956
} else {

src/macros.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,22 @@ pub fn rewrite_macro(
152152
shape: Shape,
153153
position: MacroPosition,
154154
) -> Option<String> {
155-
let context = &mut context.clone();
156-
context.inside_macro = true;
155+
context.inside_macro.replace(true);
156+
let result = rewrite_macro_inner(mac, extra_ident, context, shape, position);
157+
context.inside_macro.replace(false);
158+
result
159+
}
160+
161+
pub fn rewrite_macro_inner(
162+
mac: &ast::Mac,
163+
extra_ident: Option<ast::Ident>,
164+
context: &RewriteContext,
165+
shape: Shape,
166+
position: MacroPosition,
167+
) -> Option<String> {
157168
if context.config.use_try_shorthand() {
158169
if let Some(expr) = convert_try_mac(mac, context) {
159-
context.inside_macro = false;
170+
context.inside_macro.replace(false);
160171
return expr.rewrite(context, shape);
161172
}
162173
}
@@ -295,7 +306,7 @@ pub fn rewrite_macro(
295306
// then we can rewrite this as an usual array literal.
296307
// Otherwise, we must preserve the original existence of trailing comma.
297308
if FORCED_BRACKET_MACROS.contains(&macro_name.as_str()) {
298-
context.inside_macro = false;
309+
context.inside_macro.replace(false);
299310
trailing_comma = false;
300311
}
301312
// Convert `MacroArg` into `ast::Expr`, as `rewrite_array` only accepts the latter.

src/overflow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
384384
result.push_str(self.ident);
385385
result.push_str(self.prefix);
386386
if !self.context.use_block_indent()
387-
|| (self.context.inside_macro && !items_str.contains('\n') && fits_one_line)
387+
|| (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
388388
|| (is_extendable && extend_width <= shape.width)
389389
{
390390
if self.context.config.spaces_within_parens_and_brackets() && !items_str.is_empty() {

src/patterns.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,6 @@ fn rewrite_tuple_pat(
360360

361361
// add comma if `(x,)`
362362
let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none();
363-
let mut context = context.clone();
364-
if let Some(&TuplePatField::Dotdot(..)) = pat_vec.last() {
365-
context.inside_macro = true;
366-
}
367363
let path_str = path_str.unwrap_or_default();
368364
let mut pat_ref_vec = Vec::with_capacity(pat_vec.len());
369365
for pat in pat_vec {

src/rewrite.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ pub struct RewriteContext<'a> {
2929
pub parse_session: &'a ParseSess,
3030
pub codemap: &'a CodeMap,
3131
pub config: &'a Config,
32-
pub inside_macro: bool,
32+
pub inside_macro: RefCell<bool>,
3333
// Force block indent style even if we are using visual indent style.
3434
pub use_block: RefCell<bool>,
3535
// When `format_if_else_cond_comment` is true, unindent the comment on top
3636
// of the `else` or `else if`.
37-
pub is_if_else_block: bool,
37+
pub is_if_else_block: RefCell<bool>,
3838
// When rewriting chain, veto going multi line except the last element
3939
pub force_one_line_chain: RefCell<bool>,
4040
pub snippet_provider: &'a SnippetProvider<'a>,
@@ -53,4 +53,12 @@ impl<'a> RewriteContext<'a> {
5353
pub fn budget(&self, used_width: usize) -> usize {
5454
self.config.max_width().checked_sub(used_width).unwrap_or(0)
5555
}
56+
57+
pub fn inside_macro(&self) -> bool {
58+
*self.inside_macro.borrow()
59+
}
60+
61+
pub fn is_if_else_block(&self) -> bool {
62+
*self.is_if_else_block.borrow()
63+
}
5664
}

src/visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
696696
parse_session: self.parse_session,
697697
codemap: self.codemap,
698698
config: self.config,
699-
inside_macro: false,
699+
inside_macro: RefCell::new(false),
700700
use_block: RefCell::new(false),
701-
is_if_else_block: false,
701+
is_if_else_block: RefCell::new(false),
702702
force_one_line_chain: RefCell::new(false),
703703
snippet_provider: self.snippet_provider,
704704
}

0 commit comments

Comments
 (0)