Skip to content

Commit db907de

Browse files
authored
Rollup merge of rust-lang#52536 - alexcrichton:attr-spans, r=nikomatsakis
proc_macro: Preserve spans of attributes on functions This commit updates the tokenization of items which are subsequently passed to `proc_macro` to ensure that span information is preserved on attributes as much as possible. Previously this area of the code suffered from rust-lang#43081 where we haven't actually implemented converting an attribute to to a token tree yet, but a local fix was possible here. Closes rust-lang#47941
2 parents 7a0ffda + 5332375 commit db907de

File tree

5 files changed

+132
-5
lines changed

5 files changed

+132
-5
lines changed

src/libsyntax/parse/token.rs

+44-5
Original file line numberDiff line numberDiff line change
@@ -777,11 +777,50 @@ fn prepend_attrs(sess: &ParseSess,
777777
for attr in attrs {
778778
assert_eq!(attr.style, ast::AttrStyle::Outer,
779779
"inner attributes should prevent cached tokens from existing");
780-
// FIXME: Avoid this pretty-print + reparse hack as bove
781-
let name = FileName::MacroExpansion;
782-
let source = pprust::attr_to_string(attr);
783-
let stream = parse_stream_from_source_str(name, source, sess, Some(span));
784-
builder.push(stream);
780+
781+
if attr.is_sugared_doc {
782+
let stream = parse_stream_from_source_str(
783+
FileName::MacroExpansion,
784+
pprust::attr_to_string(attr),
785+
sess,
786+
Some(span),
787+
);
788+
builder.push(stream);
789+
continue
790+
}
791+
792+
// synthesize # [ $path $tokens ] manually here
793+
let mut brackets = tokenstream::TokenStreamBuilder::new();
794+
795+
// For simple paths, push the identifier directly
796+
if attr.path.segments.len() == 1 && attr.path.segments[0].args.is_none() {
797+
let ident = attr.path.segments[0].ident;
798+
let token = Ident(ident, ident.as_str().starts_with("r#"));
799+
brackets.push(tokenstream::TokenTree::Token(ident.span, token));
800+
801+
// ... and for more complicated paths, fall back to a reparse hack that
802+
// should eventually be removed.
803+
} else {
804+
let stream = parse_stream_from_source_str(
805+
FileName::MacroExpansion,
806+
pprust::path_to_string(&attr.path),
807+
sess,
808+
Some(span),
809+
);
810+
brackets.push(stream);
811+
}
812+
813+
brackets.push(attr.tokens.clone());
814+
815+
let tokens = tokenstream::Delimited {
816+
delim: DelimToken::Bracket,
817+
tts: brackets.build().into(),
818+
};
819+
// The span we list here for `#` and for `[ ... ]` are both wrong in
820+
// that it encompasses more than each token, but it hopefully is "good
821+
// enough" for now at least.
822+
builder.push(tokenstream::TokenTree::Token(attr.span, Pound));
823+
builder.push(tokenstream::TokenTree::Delimited(attr.span, tokens));
785824
}
786825
builder.push(tokens.clone());
787826
Some(builder.build())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:attribute-spans-preserved.rs
12+
13+
#![feature(use_extern_macros)]
14+
15+
extern crate attribute_spans_preserved as foo;
16+
17+
use foo::foo;
18+
19+
#[ foo ( let y: u32 = "z"; ) ] //~ ERROR: mismatched types
20+
#[ bar let x: u32 = "y"; ] //~ ERROR: mismatched types
21+
fn main() {
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/attribute-spans-preserved.rs:19:23
3+
|
4+
LL | #[ foo ( let y: u32 = "z"; ) ] //~ ERROR: mismatched types
5+
| ^^^ expected u32, found reference
6+
|
7+
= note: expected type `u32`
8+
found type `&'static str`
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/attribute-spans-preserved.rs:20:21
12+
|
13+
LL | #[ bar let x: u32 = "y"; ] //~ ERROR: mismatched types
14+
| ^^^ expected u32, found reference
15+
|
16+
= note: expected type `u32`
17+
found type `&'static str`
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main ( ) { let y : u32 = "z" ; let x : u32 = "y" ; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// no-prefer-dynamic
12+
13+
#![crate_type = "proc-macro"]
14+
15+
extern crate proc_macro;
16+
17+
use proc_macro::*;
18+
19+
#[proc_macro_attribute]
20+
pub fn foo(attr: TokenStream, f: TokenStream) -> TokenStream {
21+
let mut tokens = f.into_iter();
22+
assert_eq!(tokens.next().unwrap().to_string(), "#");
23+
let next_attr = match tokens.next().unwrap() {
24+
TokenTree::Group(g) => g,
25+
_ => panic!(),
26+
};
27+
28+
let fn_tok = tokens.next().unwrap();
29+
let ident_tok = tokens.next().unwrap();
30+
let args_tok = tokens.next().unwrap();
31+
let body = tokens.next().unwrap();
32+
33+
let new_body = attr.into_iter()
34+
.chain(next_attr.stream().into_iter().skip(1));
35+
36+
let tokens = vec![
37+
fn_tok,
38+
ident_tok,
39+
args_tok,
40+
Group::new(Delimiter::Brace, new_body.collect()).into(),
41+
].into_iter().collect::<TokenStream>();
42+
println!("{}", tokens);
43+
return tokens
44+
}

0 commit comments

Comments
 (0)