Skip to content

Commit 733ff4f

Browse files
committed
Use proc_macro2::Span::call_site for all quotes
This avoids breakage when deriving `StructOpt` when `proc_macro2`'s nightly feature is enabled. See dtolnay/proc-macro2#67 for details.
1 parent f26ce8b commit 733ff4f

File tree

3 files changed

+72
-66
lines changed

3 files changed

+72
-66
lines changed

structopt-derive/src/attrs.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Attrs {
5050
Attrs {
5151
name: name,
5252
methods: vec![],
53-
parser: (Parser::TryFromStr, quote!(::std::str::FromStr::from_str)),
53+
parser: (Parser::TryFromStr, my_quote!(::std::str::FromStr::from_str)),
5454
has_custom_parser: false,
5555
is_subcommand: false,
5656
}
@@ -67,7 +67,7 @@ impl Attrs {
6767
("name", new_name) => self.name = new_name.into(),
6868
(name, arg) => self.methods.push(Method {
6969
name: name.to_string(),
70-
args: quote!(#arg),
70+
args: my_quote!(#arg),
7171
}),
7272
}
7373
}
@@ -79,21 +79,21 @@ impl Attrs {
7979
let iter = attrs.iter()
8080
.filter_map(|attr| {
8181
let path = &attr.path;
82-
match quote!(#path) == quote!(structopt) {
82+
match my_quote!(#path) == my_quote!(structopt) {
8383
true => Some(
8484
attr.interpret_meta()
85-
.expect(&format!("invalid structopt syntax: {}", quote!(attr)))
85+
.expect(&format!("invalid structopt syntax: {}", my_quote!(attr)))
8686
),
8787
false => None,
8888
}
8989
}).
9090
flat_map(|m| match m {
9191
List(l) => l.nested,
92-
tokens => panic!("unsupported syntax: {}", quote!(#tokens).to_string()),
92+
tokens => panic!("unsupported syntax: {}", my_quote!(#tokens).to_string()),
9393
})
9494
.map(|m| match m {
9595
Meta(m) => m,
96-
ref tokens => panic!("unsupported syntax: {}", quote!(#tokens).to_string()),
96+
ref tokens => panic!("unsupported syntax: {}", my_quote!(#tokens).to_string()),
9797
});
9898
for attr in iter {
9999
match attr {
@@ -102,7 +102,7 @@ impl Attrs {
102102
NameValue(MetaNameValue { ident, lit, .. }) => {
103103
self.methods.push(Method {
104104
name: ident.to_string(),
105-
args: quote!(#lit),
105+
args: my_quote!(#lit),
106106
})
107107
}
108108
List(MetaList { ident, ref nested, .. }) if ident == "parse" => {
@@ -114,51 +114,51 @@ impl Attrs {
114114
Meta(NameValue(MetaNameValue { ident, lit: Str(ref v), .. })) => {
115115
let function: syn::Path = v.parse().expect("parser function path");
116116
let parser = ident.as_ref().parse().unwrap();
117-
(parser, quote!(#function))
117+
(parser, my_quote!(#function))
118118
}
119119
Meta(Word(ref i)) => {
120120
use Parser::*;
121121
let parser = i.as_ref().parse().unwrap();
122122
let function = match parser {
123-
FromStr => quote!(::std::convert::From::from),
124-
TryFromStr => quote!(::std::str::FromStr::from_str),
125-
FromOsStr => quote!(::std::convert::From::from),
123+
FromStr => my_quote!(::std::convert::From::from),
124+
TryFromStr => my_quote!(::std::str::FromStr::from_str),
125+
FromOsStr => my_quote!(::std::convert::From::from),
126126
TryFromOsStr => panic!("cannot omit parser function name with `try_from_os_str`"),
127-
FromOccurrences => quote!({|v| v as _}),
127+
FromOccurrences => my_quote!({|v| v as _}),
128128
};
129129
(parser, function)
130130
}
131-
ref l @ _ => panic!("unknown value parser specification: {}", quote!(#l)),
131+
ref l @ _ => panic!("unknown value parser specification: {}", my_quote!(#l)),
132132
};
133133
}
134134
List(MetaList { ident, ref nested, .. }) if ident == "raw" => {
135135
for method in nested {
136136
match *method {
137137
Meta(NameValue(MetaNameValue { ident, lit: Str(ref v), .. })) =>
138138
self.push_raw_method(ident.as_ref(), v),
139-
ref mi @ _ => panic!("unsupported raw entry: {}", quote!(#mi)),
139+
ref mi @ _ => panic!("unsupported raw entry: {}", my_quote!(#mi)),
140140
}
141141
}
142142
}
143143
Word(ref w) if w == "subcommand" => self.is_subcommand = true,
144144
ref i @ List(..) | ref i @ Word(..) =>
145-
panic!("unsupported option: {}", quote!(#i)),
145+
panic!("unsupported option: {}", my_quote!(#i)),
146146
}
147147
}
148148
}
149149
fn push_raw_method(&mut self, name: &str, args: &LitStr) {
150150
let ts: ::proc_macro2::TokenStream = args.value().parse()
151-
.expect(&format!("bad parameter {} = {}: the parameter must be valid rust code", name, quote!(#args)));
151+
.expect(&format!("bad parameter {} = {}: the parameter must be valid rust code", name, my_quote!(#args)));
152152
self.methods.push(Method {
153153
name: name.to_string(),
154-
args: quote!(#(#ts)*),
154+
args: my_quote!(#(#ts)*),
155155
})
156156
}
157157
fn push_doc_comment(&mut self, attrs: &[Attribute], name: &str) {
158158
let doc_comments: Vec<_> = attrs.iter()
159159
.filter_map(|attr| {
160160
let path = &attr.path;
161-
match quote!(#path) == quote!(doc) {
161+
match my_quote!(#path) == my_quote!(doc) {
162162
true => attr.interpret_meta(),
163163
false => None,
164164
}
@@ -195,7 +195,7 @@ impl Attrs {
195195
.join("\n");
196196
self.methods.push(Method {
197197
name: name.to_string(),
198-
args: quote!(#arg),
198+
args: my_quote!(#arg),
199199
});
200200
}
201201
pub fn from_struct(attrs: &[Attribute], name: String) -> Attrs {
@@ -243,9 +243,9 @@ impl Attrs {
243243
pub fn methods(&self) -> Tokens {
244244
let methods = self.methods.iter().map(|&Method { ref name, ref args }| {
245245
let name: ::syn::Ident = name.as_str().into();
246-
quote!( .#name(#args) )
246+
my_quote!( .#name(#args) )
247247
});
248-
quote!( #(#methods)* )
248+
my_quote!( #(#methods)* )
249249
}
250250
pub fn name(&self) -> &str {
251251
&self.name

0 commit comments

Comments
 (0)