From 669a8bcf8296b0a72659a85402caa20ebda4cfac Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sun, 1 Jul 2018 09:59:45 +0900 Subject: [PATCH 1/2] Add a test for #2721 --- tests/source/macro_rules.rs | 32 ++++++++++++++++++++++++++++++++ tests/target/macro_rules.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/tests/source/macro_rules.rs b/tests/source/macro_rules.rs index 2556a55d5a3..b6ff049430d 100644 --- a/tests/source/macro_rules.rs +++ b/tests/source/macro_rules.rs @@ -230,3 +230,35 @@ macro_rules! save_regs { :::: "intel", "volatile"); }; } + +// #2721 +macro_rules! impl_as_byte_slice_arrays { + ($n:expr,) => {}; + ($n:expr, $N:ident, $($NN:ident,)*) => { + impl_as_byte_slice_arrays!($n - 1, $($NN,)*); + + impl AsByteSliceMut for [T; $n] where [T]: AsByteSliceMut { + fn as_byte_slice_mut(&mut self) -> &mut [u8] { + self[..].as_byte_slice_mut() + } + + fn to_le(&mut self) { + self[..].to_le() + } + } + }; + (!div $n:expr,) => {}; + (!div $n:expr, $N:ident, $($NN:ident,)*) => { + impl_as_byte_slice_arrays!(!div $n / 2, $($NN,)*); + + impl AsByteSliceMut for [T; $n] where [T]: AsByteSliceMut { + fn as_byte_slice_mut(&mut self) -> &mut [u8] { + self[..].as_byte_slice_mut() + } + + fn to_le(&mut self) { + self[..].to_le() + } + } + }; +} diff --git a/tests/target/macro_rules.rs b/tests/target/macro_rules.rs index d1a9340b72e..41bc1420476 100644 --- a/tests/target/macro_rules.rs +++ b/tests/target/macro_rules.rs @@ -271,3 +271,35 @@ macro_rules! save_regs { :::: "intel", "volatile"); }; } + +// #2721 +macro_rules! impl_as_byte_slice_arrays { + ($n:expr,) => {}; + ($n:expr, $N:ident, $($NN:ident,)*) => { + impl_as_byte_slice_arrays!($n - 1, $($NN,)*); + + impl AsByteSliceMut for [T; $n] where [T]: AsByteSliceMut { + fn as_byte_slice_mut(&mut self) -> &mut [u8] { + self[..].as_byte_slice_mut() + } + + fn to_le(&mut self) { + self[..].to_le() + } + } + }; + (!div $n:expr,) => {}; + (!div $n:expr, $N:ident, $($NN:ident,)*) => { + impl_as_byte_slice_arrays!(!div $n / 2, $($NN,)*); + + impl AsByteSliceMut for [T; $n] where [T]: AsByteSliceMut { + fn as_byte_slice_mut(&mut self) -> &mut [u8] { + self[..].as_byte_slice_mut() + } + + fn to_le(&mut self) { + self[..].to_le() + } + } + }; +} From 5a3640da699ac243d601683802899d897b364e60 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sun, 1 Jul 2018 09:59:53 +0900 Subject: [PATCH 2/2] Return the trimmed original snippet when formatting macro def failed --- src/macros.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index e3539d3b699..8468dbf1591 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -411,7 +411,10 @@ pub fn rewrite_macro_def( result += &arm_shape.indent.to_string_with_newline(context.config); } - result += write_list(&branch_items, &fmt)?.as_str(); + match write_list(&branch_items, &fmt) { + Some(ref s) => result += s, + None => return snippet, + } if multi_branch_style { result += &indent.to_string_with_newline(context.config);