From ca78653d61b7f045ca6245bb39c48fb057651aaf Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Wed, 18 Sep 2019 22:37:08 +0900 Subject: [PATCH] Use Cell instead of RefCell (#3798) --- src/chains.rs | 2 +- src/macros.rs | 2 +- src/overflow.rs | 2 +- src/rewrite.rs | 20 ++++++++++---------- src/visitor.rs | 23 +++++++++-------------- 5 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index 81708625acb..56775c4b5ef 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -647,7 +647,7 @@ fn join_rewrites(&self, context: &RewriteContext<'_>, child_shape: Shape) -> Opt Cow::from("") } else { // Use new lines. - if *context.force_one_line_chain.borrow() { + if context.force_one_line_chain.get() { return None; } child_shape.to_string_with_newline(context.config) diff --git a/src/macros.rs b/src/macros.rs index 08f83b7e826..e92f02a1db1 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -528,7 +528,7 @@ pub(crate) fn rewrite_macro_def( Some(v) => Some(v), // if the rewrite returned None because a macro could not be rewritten, then return the // original body - None if *context.macro_rewrite_failure.borrow() => { + None if context.macro_rewrite_failure.get() => { Some(context.snippet(branch.body).trim().to_string()) } None => None, diff --git a/src/overflow.rs b/src/overflow.rs index fc0cf98ceba..7c3f9d1a5eb 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -465,7 +465,7 @@ fn try_overflow_last_item(&self, list_items: &mut Vec) -> DefinitiveLi // Replace the last item with its first line to see if it fits with // first arguments. let placeholder = if overflow_last { - let old_value = *self.context.force_one_line_chain.borrow(); + let old_value = self.context.force_one_line_chain.get(); match self.last_item() { Some(OverflowableItem::Expr(expr)) if !combine_arg_with_callee && is_method_call(expr) => diff --git a/src/rewrite.rs b/src/rewrite.rs index 19b749dc8e1..602512e87ce 100644 --- a/src/rewrite.rs +++ b/src/rewrite.rs @@ -1,6 +1,6 @@ // A generic trait to abstract the rewriting of an element (of the AST). -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::rc::Rc; use syntax::parse::ParseSess; @@ -29,17 +29,17 @@ pub(crate) struct RewriteContext<'a> { pub(crate) parse_session: &'a ParseSess, pub(crate) source_map: &'a SourceMap, pub(crate) config: &'a Config, - pub(crate) inside_macro: Rc>, + pub(crate) inside_macro: Rc>, // Force block indent style even if we are using visual indent style. - pub(crate) use_block: RefCell, + pub(crate) use_block: Cell, // When `is_if_else_block` is true, unindent the comment on top // of the `else` or `else if`. - pub(crate) is_if_else_block: RefCell, + pub(crate) is_if_else_block: Cell, // When rewriting chain, veto going multi line except the last element - pub(crate) force_one_line_chain: RefCell, + pub(crate) force_one_line_chain: Cell, pub(crate) snippet_provider: &'a SnippetProvider<'a>, // Used for `format_snippet` - pub(crate) macro_rewrite_failure: RefCell, + pub(crate) macro_rewrite_failure: Cell, pub(crate) report: FormatReport, pub(crate) skip_context: SkipContext, pub(crate) skipped_range: Rc>>, @@ -47,7 +47,7 @@ pub(crate) struct RewriteContext<'a> { pub(crate) struct InsideMacroGuard { is_nested_macro_context: bool, - inside_macro_ref: Rc>, + inside_macro_ref: Rc>, } impl InsideMacroGuard { @@ -69,7 +69,7 @@ pub(crate) fn snippet(&self, span: Span) -> &str { /// Returns `true` if we should use block indent style for rewriting function call. pub(crate) fn use_block_indent(&self) -> bool { - self.config.indent_style() == IndentStyle::Block || *self.use_block.borrow() + self.config.indent_style() == IndentStyle::Block || self.use_block.get() } pub(crate) fn budget(&self, used_width: usize) -> usize { @@ -77,7 +77,7 @@ pub(crate) fn budget(&self, used_width: usize) -> usize { } pub(crate) fn inside_macro(&self) -> bool { - *self.inside_macro.borrow() + self.inside_macro.get() } pub(crate) fn enter_macro(&self) -> InsideMacroGuard { @@ -93,6 +93,6 @@ pub(crate) fn leave_macro(&self) { } pub(crate) fn is_if_else_block(&self) -> bool { - *self.is_if_else_block.borrow() + self.is_if_else_block.get() } } diff --git a/src/visitor.rs b/src/visitor.rs index 831fd56819f..49197682c14 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -1,4 +1,4 @@ -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::rc::Rc; use syntax::parse::ParseSess; @@ -862,15 +862,10 @@ pub(crate) fn with_context(&mut self, f: F) -> Option where F: Fn(&RewriteContext<'_>) -> Option, { - // FIXME borrow checker fighting - can be simplified a lot with NLL. - let (result, mrf) = { - let context = self.get_context(); - let result = f(&context); - let mrf = &context.macro_rewrite_failure.borrow(); - (result, *std::ops::Deref::deref(mrf)) - }; + let context = self.get_context(); + let result = f(&context); - self.macro_rewrite_failure |= mrf; + self.macro_rewrite_failure |= context.macro_rewrite_failure.get(); result } @@ -879,12 +874,12 @@ pub(crate) fn get_context(&self) -> RewriteContext<'_> { parse_session: self.parse_session, source_map: self.source_map, config: self.config, - inside_macro: Rc::new(RefCell::new(false)), - use_block: RefCell::new(false), - is_if_else_block: RefCell::new(false), - force_one_line_chain: RefCell::new(false), + inside_macro: Rc::new(Cell::new(false)), + use_block: Cell::new(false), + is_if_else_block: Cell::new(false), + force_one_line_chain: Cell::new(false), snippet_provider: self.snippet_provider, - macro_rewrite_failure: RefCell::new(false), + macro_rewrite_failure: Cell::new(false), report: self.report.clone(), skip_context: self.skip_context.clone(), skipped_range: self.skipped_range.clone(),