summary refs log tree commit diff
path: root/bit_field
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@chromium.org>2018-11-15 15:09:57 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-11-21 05:52:45 -0800
commit1d0b445be2db1dbe9b92ff8b8fa646904ff60fea (patch)
treefe599f71ffa58330ec7b38f4947e7a4c7efccfff /bit_field
parente81a3e66cc266821fa5faec258d33c2eb3d8e102 (diff)
downloadcrosvm-1d0b445be2db1dbe9b92ff8b8fa646904ff60fea.tar
crosvm-1d0b445be2db1dbe9b92ff8b8fa646904ff60fea.tar.gz
crosvm-1d0b445be2db1dbe9b92ff8b8fa646904ff60fea.tar.bz2
crosvm-1d0b445be2db1dbe9b92ff8b8fa646904ff60fea.tar.lz
crosvm-1d0b445be2db1dbe9b92ff8b8fa646904ff60fea.tar.xz
crosvm-1d0b445be2db1dbe9b92ff8b8fa646904ff60fea.tar.zst
crosvm-1d0b445be2db1dbe9b92ff8b8fa646904ff60fea.zip
macros: Update syn to 0.15
This brings us onto the stable API surface area for procedural macros
that stabilized in Rust 1.30, rather than the string-based shim on
older compilers.

    https://blog.rust-lang.org/2018/10/25/Rust-1.30.0.html

Intervening release notes:

- https://github.com/dtolnay/syn/releases/tag/0.13.0
- https://github.com/dtolnay/syn/releases/tag/0.14.0
- https://github.com/dtolnay/syn/releases/tag/0.15.0

TEST=cargo check crosvm
TEST=cargo test each of the three proc-macro crates
TEST=build_packages
CQ-DEPEND=CL:1340766

Change-Id: Idcf14df0225ab41423b9a8639d0bba0a63513712
Reviewed-on: https://chromium-review.googlesource.com/1338507
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Diffstat (limited to 'bit_field')
-rw-r--r--bit_field/bit_field_derive/Cargo.toml6
-rw-r--r--bit_field/bit_field_derive/bit_field_derive.rs40
2 files changed, 26 insertions, 20 deletions
diff --git a/bit_field/bit_field_derive/Cargo.toml b/bit_field/bit_field_derive/Cargo.toml
index 4d11d2b..05d5a36 100644
--- a/bit_field/bit_field_derive/Cargo.toml
+++ b/bit_field/bit_field_derive/Cargo.toml
@@ -4,9 +4,9 @@ version = "0.1.0"
 authors = ["The Chromium OS Authors"]
 
 [dependencies]
-syn = "=0.12"
-quote = "=0.4"
-proc-macro2 = "=0.2"
+syn = "=0.15"
+quote = "=0.6"
+proc-macro2 = "=0.4"
 
 [lib]
 proc-macro = true
diff --git a/bit_field/bit_field_derive/bit_field_derive.rs b/bit_field/bit_field_derive/bit_field_derive.rs
index cc5c070..adb889d 100644
--- a/bit_field/bit_field_derive/bit_field_derive.rs
+++ b/bit_field/bit_field_derive/bit_field_derive.rs
@@ -9,26 +9,25 @@ extern crate proc_macro2;
 #[macro_use]
 extern crate quote;
 
-#[cfg_attr(test, macro_use)]
+#[macro_use]
 extern crate syn;
 
 use std::string::String;
 use std::vec::Vec;
 
-use proc_macro::TokenStream;
-use proc_macro2::{Span, TokenNode};
-use quote::Tokens;
+use proc_macro2::{Span, TokenStream, TokenTree};
 use syn::{Attribute, Data, DeriveInput, Fields, Ident};
 
 type Result<T> = std::result::Result<T, String>;
 
 /// The function that derives the actual implementation.
 #[proc_macro_derive(BitField, attributes(passthrough))]
-pub fn bitfield(input: TokenStream) -> TokenStream {
-    bitfield_impl(syn::parse(input).unwrap()).into()
+pub fn bitfield(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
+    let derive_input = parse_macro_input!(input as DeriveInput);
+    bitfield_impl(derive_input).into()
 }
 
-fn bitfield_impl(ast: DeriveInput) -> Tokens {
+fn bitfield_impl(ast: DeriveInput) -> TokenStream {
     if !ast.generics.params.is_empty() {
         return quote! {
             compile_error!("derive(BitField) does not support generic parameters");
@@ -109,7 +108,7 @@ fn get_struct_name(schema_name: &str) -> Option<&str> {
 // error.
 // We only care about field names and types.
 // "myfield : BitField3" -> ("myfield", Token(BitField3))
-fn get_struct_fields(ast: DeriveInput) -> Result<Vec<(String, Tokens)>> {
+fn get_struct_fields(ast: DeriveInput) -> Result<Vec<(String, TokenStream)>> {
     let fields = match ast.data {
         Data::Struct(data_struct) => match data_struct.fields {
             Fields::Named(fields_named) => fields_named.named,
@@ -140,13 +139,16 @@ fn get_struct_fields(ast: DeriveInput) -> Result<Vec<(String, Tokens)>> {
 
 // We only support attributes of the form #[passthrough(derive(Clone))].
 // Any other attribute will cause undefined behavior.
-fn get_attrs(attrs: &[Attribute]) -> Result<Vec<Tokens>> {
+fn get_attrs(attrs: &[Attribute]) -> Result<Vec<TokenStream>> {
     let mut attr_tokens = Vec::new();
     for a in attrs {
         let tts = a.tts.clone();
         for t in tts {
-            match t.kind {
-                TokenNode::Group(_d, g) => attr_tokens.push(quote!(#[#g])),
+            match t {
+                TokenTree::Group(group) => {
+                    let nested = group.stream();
+                    attr_tokens.push(quote!(#[#nested]))
+                }
                 _ => return Err(format!("Unsupported attribute.")),
             }
         }
@@ -154,7 +156,11 @@ fn get_attrs(attrs: &[Attribute]) -> Result<Vec<Tokens>> {
     Ok(attr_tokens)
 }
 
-fn get_struct_def(vis: &Tokens, name: &Tokens, fields: &[(String, Tokens)]) -> Tokens {
+fn get_struct_def(
+    vis: &TokenStream,
+    name: &TokenStream,
+    fields: &[(String, TokenStream)],
+) -> TokenStream {
     let mut field_types = Vec::new();
     for &(ref _name, ref ty) in fields {
         field_types.push(ty.clone());
@@ -178,7 +184,7 @@ fn get_struct_def(vis: &Tokens, name: &Tokens, fields: &[(String, Tokens)]) -> T
 }
 
 // Implement setter and getter for all fields.
-fn get_fields_impl(fields: &[(String, Tokens)]) -> Vec<Tokens> {
+fn get_fields_impl(fields: &[(String, TokenStream)]) -> Vec<TokenStream> {
     let mut impls = Vec::new();
     // This vec keeps track of types before this field, used to generate the offset.
     let mut current_types = vec![quote!(BitField0)];
@@ -208,7 +214,7 @@ fn get_fields_impl(fields: &[(String, Tokens)]) -> Vec<Tokens> {
 }
 
 // Implement setter and getter for all fields.
-fn get_debug_fmt_impl(name: &Tokens, fields: &[(String, Tokens)]) -> Tokens {
+fn get_debug_fmt_impl(name: &TokenStream, fields: &[(String, TokenStream)]) -> TokenStream {
     // print fields:
     let mut impls = Vec::new();
     for &(ref name, ref _ty) in fields {
@@ -231,7 +237,7 @@ fn get_debug_fmt_impl(name: &Tokens, fields: &[(String, Tokens)]) -> Tokens {
 }
 
 // Implement test.
-fn get_tests_impl(struct_name: &Tokens, fields: &[(String, Tokens)]) -> Vec<Tokens> {
+fn get_tests_impl(struct_name: &TokenStream, fields: &[(String, TokenStream)]) -> Vec<TokenStream> {
     let mut field_types = Vec::new();
     for &(ref _name, ref ty) in fields {
         field_types.push(ty.clone());
@@ -280,7 +286,7 @@ fn get_tests_impl(struct_name: &Tokens, fields: &[(String, Tokens)]) -> Vec<Toke
     impls
 }
 
-fn get_bits_impl(name: &Tokens) -> Tokens {
+fn get_bits_impl(name: &TokenStream) -> TokenStream {
     quote!(
         impl #name {
             #[inline]
@@ -542,6 +548,6 @@ mod tests {
             }
         };
 
-        assert_eq!(bitfield_impl(input), expected);
+        assert_eq!(bitfield_impl(input).to_string(), expected.to_string());
     }
 }