Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,6 @@ fn return_macro_parse_failure_fallback(
Some(context.snippet(span).to_owned())
}

struct InsideMacroGuard<'a> {
context: &'a RewriteContext<'a>,
is_nested: bool,
}

impl<'a> InsideMacroGuard<'a> {
fn inside_macro_context(context: &'a RewriteContext<'_>) -> InsideMacroGuard<'a> {
let is_nested = context.inside_macro.replace(true);
InsideMacroGuard { context, is_nested }
}
}

impl<'a> Drop for InsideMacroGuard<'a> {
fn drop(&mut self) {
self.context.inside_macro.replace(self.is_nested);
}
}

pub(crate) fn rewrite_macro(
mac: &ast::Mac,
extra_ident: Option<ast::Ident>,
Expand All @@ -216,9 +198,16 @@ pub(crate) fn rewrite_macro(
if should_skip {
None
} else {
let guard = InsideMacroGuard::inside_macro_context(context);
let guard = context.enter_macro();
let result = catch_unwind(AssertUnwindSafe(|| {
rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested)
rewrite_macro_inner(
mac,
extra_ident,
context,
shape,
position,
guard.is_nested(),
)
}));
match result {
Err(..) | Ok(None) => {
Expand Down Expand Up @@ -258,7 +247,7 @@ fn rewrite_macro_inner(
) -> Option<String> {
if context.config.use_try_shorthand() {
if let Some(expr) = convert_try_mac(mac, context) {
context.inside_macro.replace(false);
context.leave_macro();
return expr.rewrite(context, shape);
}
}
Expand Down Expand Up @@ -407,7 +396,7 @@ fn rewrite_macro_inner(
Some(SeparatorTactic::Never)
};
if FORCED_BRACKET_MACROS.contains(macro_name) && !is_nested_macro {
context.inside_macro.replace(false);
context.leave_macro();
if context.use_block_indent() {
force_trailing_comma = Some(SeparatorTactic::Vertical);
};
Expand Down
12 changes: 5 additions & 7 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::source_map::SpanUtils;
use crate::spanned::Spanned;
use crate::utils::{
contains_skip, extra_offset, first_line_width, inner_attributes, last_line_extendable, mk_sp,
ptr_vec_to_ref_vec, semicolon_for_expr, trimmed_last_line_width,
ptr_vec_to_ref_vec, semicolon_for_expr, trimmed_last_line_width, unicode_str_width,
};

/// A simple wrapper type against `ast::Arm`. Used inside `write_list()`.
Expand Down Expand Up @@ -452,7 +452,9 @@ fn rewrite_match_body(

match rewrite {
Some(ref body_str)
if is_block || (!body_str.contains('\n') && body_str.len() <= body_shape.width) =>
if is_block
|| (!body_str.contains('\n')
&& unicode_str_width(body_str) <= body_shape.width) =>
{
return combine_orig_body(body_str);
}
Expand All @@ -470,11 +472,6 @@ fn rewrite_match_body(
next_line_body_shape.width,
);
match (orig_body, next_line_body) {
(Some(ref orig_str), Some(ref next_line_str))
if orig_str == next_line_str || context.inside_macro() =>
{
combine_orig_body(orig_str)
}
(Some(ref orig_str), Some(ref next_line_str))
if prefer_next_line(orig_str, next_line_str, RhsTactics::Default) =>
{
Expand Down Expand Up @@ -575,6 +572,7 @@ fn can_flatten_block_around_this(body: &ast::Expr) -> bool {
| ast::ExprKind::Box(ref expr)
| ast::ExprKind::Try(ref expr)
| ast::ExprKind::Unary(_, ref expr)
| ast::ExprKind::Index(ref expr, _)
| ast::ExprKind::Cast(ref expr, _) => can_flatten_block_around_this(expr),
_ => false,
}
Expand Down
32 changes: 31 additions & 1 deletion src/rewrite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// A generic trait to abstract the rewriting of an element (of the AST).

use std::cell::RefCell;
use std::rc::Rc;

use syntax::parse::ParseSess;
use syntax::ptr;
Expand Down Expand Up @@ -28,7 +29,7 @@ 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: RefCell<bool>,
pub(crate) inside_macro: Rc<RefCell<bool>>,
// Force block indent style even if we are using visual indent style.
pub(crate) use_block: RefCell<bool>,
// When `is_if_else_block` is true, unindent the comment on top
Expand All @@ -43,6 +44,23 @@ pub(crate) struct RewriteContext<'a> {
pub(crate) skip_context: SkipContext,
}

pub(crate) struct InsideMacroGuard {
is_nested_macro_context: bool,
inside_macro_ref: Rc<RefCell<bool>>,
}

impl InsideMacroGuard {
pub(crate) fn is_nested(&self) -> bool {
self.is_nested_macro_context
}
}

impl Drop for InsideMacroGuard {
fn drop(&mut self) {
self.inside_macro_ref.replace(self.is_nested_macro_context);
}
}

impl<'a> RewriteContext<'a> {
pub(crate) fn snippet(&self, span: Span) -> &str {
self.snippet_provider.span_to_snippet(span).unwrap()
Expand All @@ -61,6 +79,18 @@ impl<'a> RewriteContext<'a> {
*self.inside_macro.borrow()
}

pub(crate) fn enter_macro(&self) -> InsideMacroGuard {
let is_nested_macro_context = self.inside_macro.replace(true);
InsideMacroGuard {
is_nested_macro_context,
inside_macro_ref: self.inside_macro.clone(),
}
}

pub(crate) fn leave_macro(&self) {
self.inside_macro.replace(false);
}

pub(crate) fn is_if_else_block(&self) -> bool {
*self.is_if_else_block.borrow()
}
Expand Down
3 changes: 2 additions & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::rc::Rc;

use syntax::parse::ParseSess;
use syntax::source_map::{self, BytePos, Pos, SourceMap, Span};
Expand Down Expand Up @@ -869,7 +870,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
parse_session: self.parse_session,
source_map: self.source_map,
config: self.config,
inside_macro: RefCell::new(false),
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),
Expand Down
19 changes: 19 additions & 0 deletions tests/source/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,22 @@ fn issue_3005() {
},
}
}

// #3774
fn issue_3774() {
{
{
{
match foo {
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => unreachab(),
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => unreacha!(),
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => unreachabl(),
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => unreachae!(),
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => unreachable(),
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => unreachable!(),
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => rrunreachable!(),
}
}
}
}
}
29 changes: 29 additions & 0 deletions tests/target/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,32 @@ fn issue_3005() {
}
}
}

// #3774
fn issue_3774() {
{
{
{
match foo {
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => unreachab(),
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => unreacha!(),
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => {
unreachabl()
}
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => {
unreachae!()
}
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => {
unreachable()
}
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => {
unreachable!()
}
Lam(_, _, _) | Pi(_, _, _) | Let(_, _, _, _) | Embed(_) | Var(_) => {
rrunreachable!()
}
}
}
}
}
}