Skip to content

Commit 7b7c63c

Browse files
authored
Rollup merge of rust-lang#69041 - petrochenkov:stabmodispan, r=Amanieu
proc_macro: Stabilize `Span::resolved_at` and `Span::located_at` Introduced in rust-lang#47149. Part of rust-lang#54725. Motivation: rust-lang#68716 (comment). Identifiers in proc macros may want to inherit span locations for diagnostics from one tokens (e.g. some tokens from the macro input), but resolve those identifiers from some different location (e.g. from the macro's definition site). This becomes especially important when multiple resolution locations become available with stabilization of [`Span::mixed_site`](rust-lang#68716). Why I think this is the right API for setting span's location and hygiene - rust-lang#69041 (comment). r? @dtolnay
2 parents b613c98 + 966a295 commit 7b7c63c

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

Diff for: src/libproc_macro/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,14 @@ impl Span {
352352

353353
/// Creates a new span with the same line/column information as `self` but
354354
/// that resolves symbols as though it were at `other`.
355-
#[unstable(feature = "proc_macro_span", issue = "54725")]
355+
#[stable(feature = "proc_macro_span_resolved_at", since = "1.43.0")]
356356
pub fn resolved_at(&self, other: Span) -> Span {
357357
Span(self.0.resolved_at(other.0))
358358
}
359359

360360
/// Creates a new span with the same name resolution behavior as `self` but
361361
/// with the line/column information of `other`.
362-
#[unstable(feature = "proc_macro_span", issue = "54725")]
362+
#[stable(feature = "proc_macro_span_located_at", since = "1.43.0")]
363363
pub fn located_at(&self, other: Span) -> Span {
364364
other.resolved_at(*self)
365365
}
+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)