From a50e1c20e96602191c2fe216764f3a229daf45b5 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 7 Jun 2020 14:41:07 +0900 Subject: [PATCH] Use underscore consts on Rust 1.37+ --- serde_derive/Cargo.toml | 2 +- serde_derive/build.rs | 30 +++++++++++++++++++ serde_derive/src/dummy.rs | 12 ++++---- test_suite/tests/expand/de_enum.expanded.rs | 4 +-- .../tests/expand/default_ty_param.expanded.rs | 4 +-- .../tests/expand/generic_enum.expanded.rs | 4 +-- .../tests/expand/generic_struct.expanded.rs | 8 ++--- .../expand/generic_tuple_struct.expanded.rs | 2 +- test_suite/tests/expand/lifetimes.expanded.rs | 4 +-- test_suite/tests/expand/named_map.expanded.rs | 4 +-- .../tests/expand/named_tuple.expanded.rs | 4 +-- .../tests/expand/named_unit.expanded.rs | 4 +-- test_suite/tests/expand/ser_enum.expanded.rs | 2 +- test_suite/tests/expand/void.expanded.rs | 4 +-- 14 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 serde_derive/build.rs diff --git a/serde_derive/Cargo.toml b/serde_derive/Cargo.toml index 879a65199..2e90c0b91 100644 --- a/serde_derive/Cargo.toml +++ b/serde_derive/Cargo.toml @@ -9,7 +9,7 @@ repository = "https://github.com/serde-rs/serde" documentation = "https://serde.rs/derive.html" keywords = ["serde", "serialization", "no_std"] readme = "crates-io.md" -include = ["src/**/*.rs", "crates-io.md", "README.md", "LICENSE-APACHE", "LICENSE-MIT"] +include = ["build.rs", "src/**/*.rs", "crates-io.md", "README.md", "LICENSE-APACHE", "LICENSE-MIT"] [features] default = [] diff --git a/serde_derive/build.rs b/serde_derive/build.rs new file mode 100644 index 000000000..a2c481a4a --- /dev/null +++ b/serde_derive/build.rs @@ -0,0 +1,30 @@ +use std::env; +use std::process::Command; +use std::str; + +// The rustc-cfg strings below are *not* public API. Please let us know by +// opening a GitHub issue if your build environment requires some way to enable +// these cfgs other than by executing our build script. +fn main() { + let minor = match rustc_minor_version() { + Some(minor) => minor, + None => return, + }; + + // Underscore const names stabilized in Rust 1.37: + // https://github.com/rust-lang/rust/pull/61347 + if minor >= 37 { + println!("cargo:rustc-cfg=underscore_consts"); + } +} + +fn rustc_minor_version() -> Option { + let rustc = env::var_os("RUSTC")?; + let output = Command::new(rustc).arg("--version").output().ok()?; + let version = str::from_utf8(&output.stdout).ok()?; + let mut pieces = version.split('.'); + if pieces.next() != Some("rustc 1") { + return None; + } + pieces.next()?.parse().ok() +} diff --git a/serde_derive/src/dummy.rs b/serde_derive/src/dummy.rs index d55baf29c..9a4e5f085 100644 --- a/serde_derive/src/dummy.rs +++ b/serde_derive/src/dummy.rs @@ -1,4 +1,5 @@ -use proc_macro2::{Ident, Span, TokenStream}; +use proc_macro2::{Ident, TokenStream}; +use quote::format_ident; use syn; use try; @@ -11,10 +12,11 @@ pub fn wrap_in_const( ) -> TokenStream { let try_replacement = try::replacement(); - let dummy_const = Ident::new( - &format!("_IMPL_{}_FOR_{}", trait_, unraw(ty)), - Span::call_site(), - ); + let dummy_const = if cfg!(underscore_consts) { + format_ident!("_") + } else { + format_ident!("_IMPL_{}_FOR_{}", trait_, unraw(ty)) + }; let use_serde = match serde_path { Some(path) => quote! { diff --git a/test_suite/tests/expand/de_enum.expanded.rs b/test_suite/tests/expand/de_enum.expanded.rs index 58d40459b..a4ba24911 100644 --- a/test_suite/tests/expand/de_enum.expanded.rs +++ b/test_suite/tests/expand/de_enum.expanded.rs @@ -9,7 +9,7 @@ enum DeEnum { } #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_DeEnum: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -261,7 +261,7 @@ const _IMPL_SERIALIZE_FOR_DeEnum: () = { }; #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_DeEnum: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] diff --git a/test_suite/tests/expand/default_ty_param.expanded.rs b/test_suite/tests/expand/default_ty_param.expanded.rs index 23c243bc0..8aaa3ab43 100644 --- a/test_suite/tests/expand/default_ty_param.expanded.rs +++ b/test_suite/tests/expand/default_ty_param.expanded.rs @@ -10,7 +10,7 @@ struct DefaultTyParam = i32> { } #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_DefaultTyParam: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -45,7 +45,7 @@ const _IMPL_SERIALIZE_FOR_DefaultTyParam: () = { }; #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_DefaultTyParam: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] diff --git a/test_suite/tests/expand/generic_enum.expanded.rs b/test_suite/tests/expand/generic_enum.expanded.rs index 899e4fea2..add7c510d 100644 --- a/test_suite/tests/expand/generic_enum.expanded.rs +++ b/test_suite/tests/expand/generic_enum.expanded.rs @@ -7,7 +7,7 @@ pub enum GenericEnum { } #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_GenericEnum: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -110,7 +110,7 @@ const _IMPL_SERIALIZE_FOR_GenericEnum: () = { }; #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_GenericEnum: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] diff --git a/test_suite/tests/expand/generic_struct.expanded.rs b/test_suite/tests/expand/generic_struct.expanded.rs index 1331e7e96..59d2e51d7 100644 --- a/test_suite/tests/expand/generic_struct.expanded.rs +++ b/test_suite/tests/expand/generic_struct.expanded.rs @@ -4,7 +4,7 @@ pub struct GenericStruct { } #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_GenericStruct: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -38,7 +38,7 @@ const _IMPL_SERIALIZE_FOR_GenericStruct: () = { }; #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_GenericStruct: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -400,7 +400,7 @@ const _IMPL_DESERIALIZE_FOR_GenericStruct: () = { pub struct GenericNewTypeStruct(T); #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_GenericNewTypeStruct: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -422,7 +422,7 @@ const _IMPL_SERIALIZE_FOR_GenericNewTypeStruct: () = { }; #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_GenericNewTypeStruct: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] diff --git a/test_suite/tests/expand/generic_tuple_struct.expanded.rs b/test_suite/tests/expand/generic_tuple_struct.expanded.rs index 508667387..8eaf0e7ef 100644 --- a/test_suite/tests/expand/generic_tuple_struct.expanded.rs +++ b/test_suite/tests/expand/generic_tuple_struct.expanded.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; pub struct GenericTupleStruct(T, U); #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_GenericTupleStruct: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] diff --git a/test_suite/tests/expand/lifetimes.expanded.rs b/test_suite/tests/expand/lifetimes.expanded.rs index 6916ac905..9840d4401 100644 --- a/test_suite/tests/expand/lifetimes.expanded.rs +++ b/test_suite/tests/expand/lifetimes.expanded.rs @@ -7,7 +7,7 @@ enum Lifetimes<'a> { } #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_Lifetimes: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -91,7 +91,7 @@ const _IMPL_SERIALIZE_FOR_Lifetimes: () = { }; #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_Lifetimes: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] diff --git a/test_suite/tests/expand/named_map.expanded.rs b/test_suite/tests/expand/named_map.expanded.rs index 5283993ec..ce43b6634 100644 --- a/test_suite/tests/expand/named_map.expanded.rs +++ b/test_suite/tests/expand/named_map.expanded.rs @@ -6,7 +6,7 @@ struct SerNamedMap<'a, 'b, A: 'a, B: 'b, C> { } #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_SerNamedMap: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -59,7 +59,7 @@ struct DeNamedMap { } #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_DeNamedMap: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] diff --git a/test_suite/tests/expand/named_tuple.expanded.rs b/test_suite/tests/expand/named_tuple.expanded.rs index 2966c295e..8b5e4eee0 100644 --- a/test_suite/tests/expand/named_tuple.expanded.rs +++ b/test_suite/tests/expand/named_tuple.expanded.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; struct SerNamedTuple<'a, 'b, A: 'a, B: 'b, C>(&'a A, &'b mut B, C); #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_SerNamedTuple: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -51,7 +51,7 @@ const _IMPL_SERIALIZE_FOR_SerNamedTuple: () = { struct DeNamedTuple(A, B, C); #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_DeNamedTuple: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] diff --git a/test_suite/tests/expand/named_unit.expanded.rs b/test_suite/tests/expand/named_unit.expanded.rs index f100688a2..dfe44ddd0 100644 --- a/test_suite/tests/expand/named_unit.expanded.rs +++ b/test_suite/tests/expand/named_unit.expanded.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; struct NamedUnit; #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_NamedUnit: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -17,7 +17,7 @@ const _IMPL_SERIALIZE_FOR_NamedUnit: () = { }; #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_NamedUnit: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] diff --git a/test_suite/tests/expand/ser_enum.expanded.rs b/test_suite/tests/expand/ser_enum.expanded.rs index cd2723fae..3f4db97d9 100644 --- a/test_suite/tests/expand/ser_enum.expanded.rs +++ b/test_suite/tests/expand/ser_enum.expanded.rs @@ -12,7 +12,7 @@ where } #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_SerEnum: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] diff --git a/test_suite/tests/expand/void.expanded.rs b/test_suite/tests/expand/void.expanded.rs index e7db4ac2f..fb98c321a 100644 --- a/test_suite/tests/expand/void.expanded.rs +++ b/test_suite/tests/expand/void.expanded.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; enum Void {} #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_SERIALIZE_FOR_Void: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived] @@ -17,7 +17,7 @@ const _IMPL_SERIALIZE_FOR_Void: () = { }; #[doc(hidden)] #[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] -const _IMPL_DESERIALIZE_FOR_Void: () = { +const _: () = { #[allow(rust_2018_idioms, clippy::useless_attribute)] extern crate serde as _serde; #[automatically_derived]