Skip to content

Commit 966a295

Browse files
committed
Add a test for Span::resolved_at and Span::located_at
1 parent df99de5 commit 966a295

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![feature(proc_macro_def_site)]
5+
#![feature(proc_macro_diagnostic)]
6+
#![feature(proc_macro_hygiene)]
7+
#![feature(proc_macro_quote)]
8+
#![crate_type = "proc-macro"]
9+
10+
extern crate proc_macro;
11+
use proc_macro::*;
12+
13+
#[proc_macro]
14+
pub fn resolve_located_at(input: TokenStream) -> TokenStream {
15+
match &*input.into_iter().collect::<Vec<_>>() {
16+
[a, b, ..] => {
17+
// The error is reported at input `a`.
18+
let mut diag = Diagnostic::new(Level::Error, "expected error");
19+
diag.set_spans(Span::def_site().located_at(a.span()));
20+
diag.emit();
21+
22+
// Resolves to `struct S;` at def site, but the error is reported at input `b`.
23+
let s = TokenTree::Ident(Ident::new("S", b.span().resolved_at(Span::def_site())));
24+
quote!({
25+
struct S;
26+
27+
$s
28+
})
29+
}
30+
_ => panic!("unexpected input"),
31+
}
32+
}

Diff for: src/test/ui/proc-macro/resolved-located-at.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// aux-build:resolved-located-at.rs
2+
3+
#![feature(proc_macro_hygiene)]
4+
5+
#[macro_use]
6+
extern crate resolved_located_at;
7+
8+
fn main() {
9+
resolve_located_at!(a b)
10+
//~^ ERROR expected error
11+
//~| ERROR mismatched types
12+
}

Diff for: src/test/ui/proc-macro/resolved-located-at.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: expected error
2+
--> $DIR/resolved-located-at.rs:9:25
3+
|
4+
LL | resolve_located_at!(a b)
5+
| ^
6+
|
7+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/resolved-located-at.rs:9:27
11+
|
12+
LL | fn main() {
13+
| - expected `()` because of default return type
14+
LL | resolve_located_at!(a b)
15+
| ^ expected `()`, found struct `main::S`
16+
|
17+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)