Skip to content

Commit b278496

Browse files
committed
Update examples/lazy-static to proc-macro2 0.3
1 parent 6e41e8e commit b278496

2 files changed

Lines changed: 11 additions & 21 deletions

File tree

examples/lazy-static/lazy-static/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ proc-macro = true
99

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

examples/lazy-static/lazy-static/src/lib.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ extern crate syn;
66
#[macro_use]
77
extern crate quote;
88
extern crate proc_macro;
9-
extern crate proc_macro2;
109

1110
use syn::{Visibility, Ident, Type, Expr};
1211
use syn::synom::Synom;
1312
use syn::spanned::Spanned;
1413
use proc_macro::TokenStream;
15-
use proc_macro2::Span;
1614

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

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

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

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

116-
let init_span = init.span().resolved_at(def_site);
117-
let init_ptr = quote_spanned! {init_span=>
108+
let init_ptr = quote_spanned! {init.span()=>
118109
Box::into_raw(Box::new(#init))
119110
};
120111

121112
let expanded = quote! {
122-
extern crate std;
123-
124113
#visibility struct #name;
125114

126-
impl std::ops::Deref for #name {
115+
impl ::std::ops::Deref for #name {
127116
type Target = #ty;
128117

129118
fn deref(&self) -> &#ty {
130119
#assert_sync
131120
#assert_sized
132121

133-
static ONCE: std::sync::Once = std::sync::ONCE_INIT;
122+
static ONCE: ::std::sync::Once = ::std::sync::ONCE_INIT;
134123
static mut VALUE: *mut #ty = 0 as *mut #ty;
135124

136125
unsafe {

0 commit comments

Comments
 (0)