From df99de522a17e7a0c4b75dfdad596862081127d5 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 10 Feb 2020 20:42:04 +0300 Subject: [PATCH 1/2] proc_macro: Stabilize `Span::resolved_at` and `Span::located_at` --- src/libproc_macro/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 3cbe852de7b5a..6e49d9829a2e1 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -352,14 +352,14 @@ impl Span { /// Creates a new span with the same line/column information as `self` but /// that resolves symbols as though it were at `other`. - #[unstable(feature = "proc_macro_span", issue = "54725")] + #[stable(feature = "proc_macro_span_resolved_at", since = "1.43.0")] pub fn resolved_at(&self, other: Span) -> Span { Span(self.0.resolved_at(other.0)) } /// Creates a new span with the same name resolution behavior as `self` but /// with the line/column information of `other`. - #[unstable(feature = "proc_macro_span", issue = "54725")] + #[stable(feature = "proc_macro_span_located_at", since = "1.43.0")] pub fn located_at(&self, other: Span) -> Span { other.resolved_at(*self) } From 966a295e8cc210d7b50185d16cb46a7ae1874531 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 18 Apr 2020 18:49:03 +0300 Subject: [PATCH 2/2] Add a test for `Span::resolved_at` and `Span::located_at` --- .../auxiliary/resolved-located-at.rs | 32 +++++++++++++++++++ src/test/ui/proc-macro/resolved-located-at.rs | 12 +++++++ .../ui/proc-macro/resolved-located-at.stderr | 21 ++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/test/ui/proc-macro/auxiliary/resolved-located-at.rs create mode 100644 src/test/ui/proc-macro/resolved-located-at.rs create mode 100644 src/test/ui/proc-macro/resolved-located-at.stderr diff --git a/src/test/ui/proc-macro/auxiliary/resolved-located-at.rs b/src/test/ui/proc-macro/auxiliary/resolved-located-at.rs new file mode 100644 index 0000000000000..9416c133e5661 --- /dev/null +++ b/src/test/ui/proc-macro/auxiliary/resolved-located-at.rs @@ -0,0 +1,32 @@ +// force-host +// no-prefer-dynamic + +#![feature(proc_macro_def_site)] +#![feature(proc_macro_diagnostic)] +#![feature(proc_macro_hygiene)] +#![feature(proc_macro_quote)] +#![crate_type = "proc-macro"] + +extern crate proc_macro; +use proc_macro::*; + +#[proc_macro] +pub fn resolve_located_at(input: TokenStream) -> TokenStream { + match &*input.into_iter().collect::>() { + [a, b, ..] => { + // The error is reported at input `a`. + let mut diag = Diagnostic::new(Level::Error, "expected error"); + diag.set_spans(Span::def_site().located_at(a.span())); + diag.emit(); + + // Resolves to `struct S;` at def site, but the error is reported at input `b`. + let s = TokenTree::Ident(Ident::new("S", b.span().resolved_at(Span::def_site()))); + quote!({ + struct S; + + $s + }) + } + _ => panic!("unexpected input"), + } +} diff --git a/src/test/ui/proc-macro/resolved-located-at.rs b/src/test/ui/proc-macro/resolved-located-at.rs new file mode 100644 index 0000000000000..9976284e222e6 --- /dev/null +++ b/src/test/ui/proc-macro/resolved-located-at.rs @@ -0,0 +1,12 @@ +// aux-build:resolved-located-at.rs + +#![feature(proc_macro_hygiene)] + +#[macro_use] +extern crate resolved_located_at; + +fn main() { + resolve_located_at!(a b) + //~^ ERROR expected error + //~| ERROR mismatched types +} diff --git a/src/test/ui/proc-macro/resolved-located-at.stderr b/src/test/ui/proc-macro/resolved-located-at.stderr new file mode 100644 index 0000000000000..0df7ced27a777 --- /dev/null +++ b/src/test/ui/proc-macro/resolved-located-at.stderr @@ -0,0 +1,21 @@ +error: expected error + --> $DIR/resolved-located-at.rs:9:25 + | +LL | resolve_located_at!(a b) + | ^ + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0308]: mismatched types + --> $DIR/resolved-located-at.rs:9:27 + | +LL | fn main() { + | - expected `()` because of default return type +LL | resolve_located_at!(a b) + | ^ expected `()`, found struct `main::S` + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`.