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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ clone-impls = []
extra-traits = []

[dependencies]
quote = { version = "0.4", optional = true }
proc-macro2 = "0.2"
proc-macro2 = "0.3"
#quote = { version = "0.5", optional = true }
quote = { git = 'https://github.com/dtolnay/quote', optional = true }
unicode-xid = "0.1"

[dev-dependencies]
Expand Down
5 changes: 3 additions & 2 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ publish = false # this is an internal crate which should never be published

[dependencies]
syn = { path = "..", features = ["full", "extra-traits"] }
quote = "0.4"
#quote = "0.5"
quote = { git = 'https://github.com/dtolnay/quote' }
failure = "0.1"
inflections = "1.1"
proc-macro2 = "0.2"
proc-macro2 = "0.3"
3 changes: 2 additions & 1 deletion codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,8 @@ pub trait Fold {{
macro_rules! fold_span_only {{
($f:ident : $t:ident) => {{
pub fn $f<V: Fold + ?Sized>(_visitor: &mut V, mut _i: $t) -> $t {{
_i.span = _visitor.fold_span(_i.span);
let span = _visitor.fold_span(_i.span());
_i.set_span(span);
_i
}}
}}
Expand Down
3 changes: 2 additions & 1 deletion examples/heapsize/heapsize_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ proc-macro = true

[dependencies]
syn = { path = "../../.." }
quote = "0.4"
#quote = "0.5"
quote = { git = 'https://github.com/dtolnay/quote' }
5 changes: 3 additions & 2 deletions examples/heapsize/heapsize_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
extern crate proc_macro;

#[macro_use]
extern crate syn;

#[macro_use]
Expand Down Expand Up @@ -40,8 +42,7 @@ pub fn derive_heap_size(input: TokenStream) -> TokenStream {
fn add_trait_bounds(mut generics: Generics) -> Generics {
for param in &mut generics.params {
if let GenericParam::Type(ref mut type_param) = *param {
let bound = syn::parse_str("::heapsize::HeapSize").unwrap();
type_param.bounds.push(bound);
type_param.bounds.push(parse_quote!(::heapsize::HeapSize));
}
}
generics
Expand Down
16 changes: 2 additions & 14 deletions examples/heapsize2/example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#[macro_use]
extern crate heapsize_derive;

// Demonstrate that hygiene is working correctly by having no `extern crate
// heapsize` in scope here. The macro-generated impl is not for something like
// `::heapsize::HeapSize` like is common in Macros 1.1. It is for the `HeapSize`
// trait brought into scope by the procedural macro definition.
extern crate heapsize;

#[derive(HeapSize)]
struct Demo<'a, T: ?Sized> {
a: Box<T>,
Expand All @@ -13,22 +11,12 @@ struct Demo<'a, T: ?Sized> {
d: HeapSize,
}

// This derive is going to generate `impl HeapSize for HeapSize` which is fine
// because the two `HeapSize` tokens have different hygiene context. The first
// one resolves to the `HeapSize` trait brough into scope by the procedural
// macro definition, and the second one refers to this struct.
//
// Also demonstrate that even though both `impl HeapSize for Demo` and `impl
// HeapSize for HeapSize` spit out a `mod scope`, they do not conflict because
// of hygiene.
#[derive(HeapSize)]
struct HeapSize {
e: String,
}

fn main() {
extern crate heapsize;

let demo = Demo {
a: b"bytestring".to_vec().into_boxed_slice(),
b: 255,
Expand Down
5 changes: 3 additions & 2 deletions examples/heapsize2/heapsize_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ proc-macro = true

[dependencies]
syn = { path = "../../.." }
quote = "0.4"
proc-macro2 = { version = "0.2", features = ["nightly"] }
#quote = "0.5"
quote = { git = 'https://github.com/dtolnay/quote' }
proc-macro2 = { version = "0.3", features = ["nightly"] }
30 changes: 9 additions & 21 deletions examples/heapsize2/heapsize_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,10 @@ pub fn derive_heap_size(input: TokenStream) -> TokenStream {
let sum = heap_size_sum(&input.data, &var);

let expanded = quote! {
mod scope {
extern crate heapsize;
use self::heapsize::HeapSize;

// The generated impl. Okay to use `HeapSize` unqualified here, it
// is guaranteed to resolve to the import on the previous line. This
// works even in edge cases like the user's struct having the name
// `HeapSize` as demonstrated in main.rs, in which case the
// generated code looks like `impl HeapSize for HeapSize`.
impl #impl_generics HeapSize for #name #ty_generics #where_clause {
fn heap_size_of_children(&#var) -> usize {
#sum
}
// The generated impl.
impl #impl_generics ::heapsize::HeapSize for #name #ty_generics #where_clause {
fn heap_size_of_children(&#var) -> usize {
#sum
}
}
};
Expand All @@ -55,15 +46,14 @@ pub fn derive_heap_size(input: TokenStream) -> TokenStream {
fn add_trait_bounds(mut generics: Generics) -> Generics {
for param in &mut generics.params {
if let GenericParam::Type(ref mut type_param) = *param {
type_param.bounds.push(parse_quote!(HeapSize));
type_param.bounds.push(parse_quote!(::heapsize::HeapSize));
}
}
generics
}

// Generate an expression to sum up the heap size of each field.
fn heap_size_sum(data: &Data, var: &Tokens) -> Tokens {
let def_site = Span::def_site();
let call_site = Span::call_site();

match *data {
Expand All @@ -83,9 +73,8 @@ fn heap_size_sum(data: &Data, var: &Tokens) -> Tokens {
let recurse = fields.named.iter().map(|f| {
let name = f.ident;
let access = quote_spanned!(call_site=> #var.#name);
let span = f.span().resolved_at(def_site);
quote_spanned! {span=>
HeapSize::heap_size_of_children(&#access)
quote_spanned! {f.span()=>
::heapsize::HeapSize::heap_size_of_children(&#access)
}
});
quote! {
Expand All @@ -99,9 +88,8 @@ fn heap_size_sum(data: &Data, var: &Tokens) -> Tokens {
let recurse = fields.unnamed.iter().enumerate().map(|(i, f)| {
let index = Index { index: i as u32, span: call_site };
let access = quote_spanned!(call_site=> #var.#index);
let span = f.span().resolved_at(def_site);
quote_spanned! {span=>
HeapSize::heap_size_of_children(&#access)
quote_spanned! {f.span()=>
::heapsize::HeapSize::heap_size_of_children(&#access)
}
});
quote! {
Expand Down
5 changes: 3 additions & 2 deletions examples/lazy-static/lazy-static/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ proc-macro = true

[dependencies]
syn = { path = "../../../", features = ["full"] }
quote = "0.4"
proc-macro2 = { version = "0.2", features = ["nightly"] }
#quote = "0.5"
quote = { git = 'https://github.com/dtolnay/quote' }
proc-macro2 = { version = "0.3", features = ["nightly"] }
27 changes: 8 additions & 19 deletions examples/lazy-static/lazy-static/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ extern crate syn;
#[macro_use]
extern crate quote;
extern crate proc_macro;
extern crate proc_macro2;

use syn::{Visibility, Ident, Type, Expr};
use syn::synom::Synom;
use syn::spanned::Spanned;
use proc_macro::TokenStream;
use proc_macro2::Span;

/// Parses the following syntax, which aligns with the input of the real
/// `lazy_static` crate.
Expand Down Expand Up @@ -51,7 +49,6 @@ impl Synom for LazyStatic {
#[proc_macro]
pub fn lazy_static(input: TokenStream) -> TokenStream {
let LazyStatic { visibility, name, ty, init } = syn::parse(input).unwrap();
let def_site = Span::def_site();

// The warning looks like this.
//
Expand Down Expand Up @@ -84,20 +81,15 @@ pub fn lazy_static(input: TokenStream) -> TokenStream {

// Assert that the static type implements Sync. If not, user sees an error
// message like the following. We span this assertion with the field type's
// line/column so that the error message appears in the correct place, but
// resolve it at the def site so that we are guaranteed it is checking the
// correct Sync. If the `Sync` token were resolved at the call site, the
// user could circumvent the check by defining their own Sync trait that is
// implemented for their type.
// line/column so that the error message appears in the correct place.
//
// error[E0277]: the trait bound `*const (): std::marker::Sync` is not satisfied
// --> src/main.rs:10:21
// |
// 10 | static ref PTR: *const () = &();
// | ^^^^^^^^^ `*const ()` cannot be shared between threads safely
let ty_span = ty.span().resolved_at(def_site);
let assert_sync = quote_spanned! {ty_span=>
struct _AssertSync where #ty: Sync;
let assert_sync = quote_spanned! {ty.span()=>
struct _AssertSync where #ty: ::std::marker::Sync;
};

// Check for Sized. Not vital to check here, but the error message is less
Expand All @@ -109,28 +101,25 @@ pub fn lazy_static(input: TokenStream) -> TokenStream {
// |
// 10 | static ref A: str = "";
// | ^^^ `str` does not have a constant size known at compile-time
let assert_sized = quote_spanned! {ty_span=>
struct _AssertSized where #ty: Sized;
let assert_sized = quote_spanned! {ty.span()=>
struct _AssertSized where #ty: ::std::marker::Sized;
};

let init_span = init.span().resolved_at(def_site);
let init_ptr = quote_spanned! {init_span=>
let init_ptr = quote_spanned! {init.span()=>
Box::into_raw(Box::new(#init))
};

let expanded = quote! {
extern crate std;

#visibility struct #name;

impl std::ops::Deref for #name {
impl ::std::ops::Deref for #name {
type Target = #ty;

fn deref(&self) -> &#ty {
#assert_sync
#assert_sized

static ONCE: std::sync::Once = std::sync::ONCE_INIT;
static ONCE: ::std::sync::Once = ::std::sync::ONCE_INIT;
static mut VALUE: *mut #ty = 0 as *mut #ty;

unsafe {
Expand Down
5 changes: 3 additions & 2 deletions examples/trace-var/trace-var/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ proc-macro = true

[dependencies]
syn = { path = "../../../", features = ["full", "fold"] }
quote = "0.4"
proc-macro2 = { version = "0.2", features = ["nightly"] }
#quote = "0.5"
quote = { git = 'https://github.com/dtolnay/quote' }
proc-macro2 = { version = "0.3", features = ["nightly"] }
Loading