about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAaron Hill <aa1ronham@gmail.com>2022-02-18 19:14:51 -0500
committerAaron Hill <aa1ronham@gmail.com>2022-02-24 16:02:07 -0500
commit01efe6d5c25b73a311ce03ec80744628f1ec8978 (patch)
treee130e515cdcc7ae8926791c26c9519e985cb3b72
parente686aee48eed69c0dc4f6ed1bd31e0df352a7008 (diff)
downloadrust-01efe6d5c25b73a311ce03ec80744628f1ec8978.tar.gz
rust-01efe6d5c25b73a311ce03ec80744628f1ec8978.zip
Address review comments
-rw-r--r--compiler/rustc_macros/src/newtype.rs34
1 files changed, 10 insertions, 24 deletions
diff --git a/compiler/rustc_macros/src/newtype.rs b/compiler/rustc_macros/src/newtype.rs
index b59ec34ba7b..f284e5cdd5c 100644
--- a/compiler/rustc_macros/src/newtype.rs
+++ b/compiler/rustc_macros/src/newtype.rs
@@ -37,7 +37,7 @@ impl Parse for Newtype {
         braced!(body in input);
 
         // Any additional `#[derive]` macro paths to apply
-        let mut derive_paths: Option<Vec<Path>> = None;
+        let mut derive_paths: Vec<Path> = Vec::new();
         let mut debug_format: Option<DebugFormat> = None;
         let mut max = None;
         let mut consts = Vec::new();
@@ -62,28 +62,23 @@ impl Parse for Newtype {
                     let derives: Punctuated<Path, Token![,]> =
                         derives.parse_terminated(Path::parse)?;
                     try_comma()?;
-                    if let Some(old) = derive_paths.replace(derives.into_iter().collect()) {
-                        panic!("Specified multiple derives: {:?}", old);
-                    }
+                    derive_paths.extend(derives);
                     continue;
                 }
                 if body.lookahead1().peek(kw::DEBUG_FORMAT) {
                     body.parse::<kw::DEBUG_FORMAT>()?;
                     body.parse::<Token![=]>()?;
-                    if body.lookahead1().peek(kw::custom) {
+                    let new_debug_format = if body.lookahead1().peek(kw::custom) {
                         body.parse::<kw::custom>()?;
-                        if let Some(old) = debug_format.replace(DebugFormat::Custom) {
-                            panic!("Specified multiple debug format options: {:?}", old);
-                        }
+                        DebugFormat::Custom
                     } else {
                         let format_str: LitStr = body.parse()?;
-                        if let Some(old) =
-                            debug_format.replace(DebugFormat::Format(format_str.value()))
-                        {
-                            panic!("Specified multiple debug format options: {:?}", old);
-                        }
-                    }
+                        DebugFormat::Format(format_str.value())
+                    };
                     try_comma()?;
+                    if let Some(old) = debug_format.replace(new_debug_format) {
+                        panic!("Specified multiple debug format options: {:?}", old);
+                    }
                     continue;
                 }
                 if body.lookahead1().peek(kw::MAX) {
@@ -121,7 +116,6 @@ impl Parse for Newtype {
             }
         }
 
-        let derive_paths = derive_paths.unwrap_or_else(Vec::new);
         let debug_format = debug_format.unwrap_or(DebugFormat::Format("{}".to_string()));
         // shave off 256 indices at the end to allow space for packing these indices into enums
         let max = max.unwrap_or_else(|| Lit::Int(LitInt::new("0xFFFF_FF00", Span::call_site())));
@@ -158,7 +152,7 @@ impl Parse for Newtype {
 
         Ok(Self(quote! {
             #(#attrs)*
-            #[derive(Copy, PartialEq, Eq, Hash, PartialOrd, Ord, #(#derive_paths),*)]
+            #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, #(#derive_paths),*)]
             #[rustc_layout_scalar_valid_range_end(#max)]
             #vis struct #name {
                 private: u32,
@@ -166,13 +160,6 @@ impl Parse for Newtype {
 
             #(#consts)*
 
-            impl Clone for #name {
-                #[inline]
-                fn clone(&self) -> Self {
-                    *self
-                }
-            }
-
             impl #name {
                 /// Maximum value the index can take, as a `u32`.
                 #vis const MAX_AS_U32: u32  = #max;
@@ -313,7 +300,6 @@ impl Parse for Newtype {
 
             #encodable_impls
             #debug_impl
-
         }))
     }
 }