From b1f4db16c961ad7fa2446280d7a91240fb39592b Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 29 May 2024 12:13:19 +0000 Subject: [PATCH 1/3] Stop emitting spans from proc macro compile time in quote expansion Before this commit if the proc_macro::quote!{} macro was used, the span of each token as written in the source of the proc macro itself would be saved in the crate metadata of the proc macro and then recovered at proc macro runtime to forward this to the macro expansion of the proc macro. This commit stops doing this and instead generates def-site spans for each token. This removes the only case where spans from the proc macro source have a semantic effect on the compilation of crates that use the proc macro. This makes it easier to stop requiring all dependencies of proc macros to be present when using the proc macro. And will make it easier to stop requiring a proc macro to be present when using a crate that used this proc macro internally but doesn't expose it as part of it's public api. The latter is necessary to be able to cross-compile tools that link against rustc internals without requiring to be built as part of rustc with the -Zdual-proc-macro hack. It may also enable using proc macros inside the standard library or it's dependencies without breaking cross-compilation. --- compiler/rustc_expand/src/base.rs | 6 +-- .../rustc_expand/src/proc_macro_server.rs | 43 ------------------- compiler/rustc_metadata/src/rmeta/decoder.rs | 9 ---- .../src/rmeta/decoder/cstore_impl.rs | 9 ---- compiler/rustc_metadata/src/rmeta/encoder.rs | 4 -- compiler/rustc_metadata/src/rmeta/mod.rs | 1 - compiler/rustc_resolve/src/macros.rs | 6 +-- compiler/rustc_session/src/parse.rs | 14 ------ library/proc_macro/src/bridge/mod.rs | 2 - library/proc_macro/src/lib.rs | 16 +------ library/proc_macro/src/quote.rs | 13 +----- 11 files changed, 5 insertions(+), 118 deletions(-) diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 6b3c7fe979327..beadb166748c2 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -25,7 +25,7 @@ use rustc_parse::MACRO_ARGUMENTS; use rustc_parse::parser::{AllowConstBlockItems, ForceCollect, Parser}; use rustc_session::Session; use rustc_session::parse::ParseSess; -use rustc_span::def_id::{CrateNum, DefId, LocalDefId}; +use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::edition::Edition; use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId, MacroKind}; use rustc_span::source_map::SourceMap; @@ -1152,10 +1152,6 @@ pub trait ResolverExpand { path: &ast::Path, ) -> Result; - /// Decodes the proc-macro quoted span in the specified crate, with the specified id. - /// No caching is performed. - fn get_proc_macro_quoted_span(&self, krate: CrateNum, id: usize) -> Span; - /// The order of items in the HIR is unrelated to the order of /// items in the AST. However, we generate proc macro harnesses /// based on the AST order, and later refer to these harnesses diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 947b8a6e3e5ee..91897f096007f 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -6,7 +6,6 @@ use rustc_ast::token; use rustc_ast::tokenstream::{self, DelimSpacing, Spacing, TokenStream}; use rustc_ast::util::literal::escape_byte_str_symbol; use rustc_ast_pretty::pprust; -use rustc_data_structures::fx::FxHashMap; use rustc_errors::{Diag, ErrorGuaranteed, MultiSpan}; use rustc_parse::lexer::{StripTokens, nfc_normalize}; use rustc_parse::parser::Parser; @@ -16,7 +15,6 @@ use rustc_proc_macro::bridge::{ }; use rustc_proc_macro::{Delimiter, Level}; use rustc_session::parse::ParseSess; -use rustc_span::def_id::CrateNum; use rustc_span::{BytePos, FileName, Pos, Span, Symbol, sym}; use smallvec::{SmallVec, smallvec}; @@ -436,8 +434,6 @@ pub(crate) struct Rustc<'a, 'b> { def_site: Span, call_site: Span, mixed_site: Span, - krate: CrateNum, - rebased_spans: FxHashMap, } impl<'a, 'b> Rustc<'a, 'b> { @@ -447,8 +443,6 @@ impl<'a, 'b> Rustc<'a, 'b> { def_site: ecx.with_def_site_ctxt(expn_data.def_site), call_site: ecx.with_call_site_ctxt(expn_data.call_site), mixed_site: ecx.with_mixed_site_ctxt(expn_data.call_site), - krate: expn_data.macro_def_id.unwrap().krate, - rebased_spans: FxHashMap::default(), ecx, } } @@ -812,43 +806,6 @@ impl server::Server for Rustc<'_, '_> { self.psess().source_map().span_to_snippet(span).ok() } - /// Saves the provided span into the metadata of - /// *the crate we are currently compiling*, which must - /// be a proc-macro crate. This id can be passed to - /// `recover_proc_macro_span` when our current crate - /// is *run* as a proc-macro. - /// - /// Let's suppose that we have two crates - `my_client` - /// and `my_proc_macro`. The `my_proc_macro` crate - /// contains a procedural macro `my_macro`, which - /// is implemented as: `quote! { "hello" }` - /// - /// When we *compile* `my_proc_macro`, we will execute - /// the `quote` proc-macro. This will save the span of - /// "hello" into the metadata of `my_proc_macro`. As a result, - /// the body of `my_proc_macro` (after expansion) will end - /// up containing a call that looks like this: - /// `proc_macro::Ident::new("hello", proc_macro::Span::recover_proc_macro_span(0))` - /// - /// where `0` is the id returned by this function. - /// When `my_proc_macro` *executes* (during the compilation of `my_client`), - /// the call to `recover_proc_macro_span` will load the corresponding - /// span from the metadata of `my_proc_macro` (which we have access to, - /// since we've loaded `my_proc_macro` from disk in order to execute it). - /// In this way, we have obtained a span pointing into `my_proc_macro` - fn span_save_span(&mut self, span: Self::Span) -> usize { - self.psess().save_proc_macro_span(span) - } - - fn span_recover_proc_macro_span(&mut self, id: usize) -> Self::Span { - let (resolver, krate, def_site) = (&*self.ecx.resolver, self.krate, self.def_site); - *self.rebased_spans.entry(id).or_insert_with(|| { - // FIXME: `SyntaxContext` for spans from proc macro crates is lost during encoding, - // replace it with a def-site context until we are encoding it properly. - resolver.get_proc_macro_quoted_span(krate, id).with_ctxt(def_site.ctxt()) - }) - } - fn symbol_normalize_and_validate_ident(&mut self, string: &str) -> Result { let sym = nfc_normalize(string); if rustc_lexer::is_ident(sym.as_str()) { Ok(sym) } else { Err(()) } diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index bd5b3893e4e8a..da7724bca7d70 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1455,15 +1455,6 @@ impl<'a> CrateMetadataRef<'a> { self.root.native_libraries.decode((self, tcx)) } - fn get_proc_macro_quoted_span(self, tcx: TyCtxt<'_>, index: usize) -> Span { - self.root - .tables - .proc_macro_quoted_spans - .get((self, tcx), index) - .unwrap_or_else(|| panic!("Missing proc macro quoted span: {index:?}")) - .decode((self, tcx)) - } - fn get_foreign_modules(self, tcx: TyCtxt<'_>) -> impl Iterator { self.root.foreign_modules.decode((self, tcx)) } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 6ea9a28528042..93183d19de7a9 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -624,15 +624,6 @@ impl CStore { self.get_crate_data(cnum).num_def_ids() } - pub fn get_proc_macro_quoted_span_untracked( - &self, - tcx: TyCtxt<'_>, - cnum: CrateNum, - id: usize, - ) -> Span { - self.get_crate_data(cnum).get_proc_macro_quoted_span(tcx, id) - } - pub fn set_used_recursively(&mut self, cnum: CrateNum) { let cmeta = self.get_crate_data_mut(cnum); if !cmeta.used { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 4988cafdd3637..a80b580dac4d7 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1983,10 +1983,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let stability = tcx.lookup_stability(CRATE_DEF_ID); let macros = self.lazy_array(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index)); - for (i, span) in self.tcx.sess.psess.proc_macro_quoted_spans() { - let span = self.lazy(span); - self.tables.proc_macro_quoted_spans.set_some(i, span); - } self.tables.def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod); record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id())); diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 408b50ae48df1..261c5f0acf91f 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -462,7 +462,6 @@ define_tables! { // `DefPathTable` up front, since we may only ever use a few // definitions from any given crate. def_keys: Table>, - proc_macro_quoted_spans: Table>, variant_data: Table>, assoc_container: Table>, macro_definition: Table>, diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index e0973271da52d..bb5056377a2a1 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -18,7 +18,7 @@ use rustc_expand::expand::{ use rustc_hir::StabilityLevel; use rustc_hir::attrs::{CfgEntry, StrippedCfgItem}; use rustc_hir::def::{self, DefKind, MacroKinds, Namespace, NonMacroAttrKind}; -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::middle::stability; use rustc_middle::ty::{RegisteredTools, TyCtxt}; use rustc_session::lint::builtin::{ @@ -490,10 +490,6 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { self.path_accessible(expn_id, path, &[MacroNS]) } - fn get_proc_macro_quoted_span(&self, krate: CrateNum, id: usize) -> Span { - self.cstore().get_proc_macro_quoted_span_untracked(self.tcx, krate, id) - } - fn declare_proc_macro(&mut self, id: NodeId) { self.proc_macros.push(self.local_def_id(id)) } diff --git a/compiler/rustc_session/src/parse.rs b/compiler/rustc_session/src/parse.rs index 62c23424f371f..e98a016bf6c92 100644 --- a/compiler/rustc_session/src/parse.rs +++ b/compiler/rustc_session/src/parse.rs @@ -271,9 +271,6 @@ pub struct ParseSess { pub file_depinfo: Lock>, /// Whether cfg(version) should treat the current release as incomplete pub assume_incomplete_release: bool, - /// Spans passed to `proc_macro::quote_span`. Each span has a numerical - /// identifier represented by its position in the vector. - proc_macro_quoted_spans: AppendOnlyVec, /// Used to generate new `AttrId`s. Every `AttrId` is unique. pub attr_id_generator: AttrIdGenerator, } @@ -308,7 +305,6 @@ impl ParseSess { env_depinfo: Default::default(), file_depinfo: Default::default(), assume_incomplete_release: false, - proc_macro_quoted_spans: Default::default(), attr_id_generator: AttrIdGenerator::new(), } } @@ -360,16 +356,6 @@ impl ParseSess { }); } - pub fn save_proc_macro_span(&self, span: Span) -> usize { - self.proc_macro_quoted_spans.push(span) - } - - pub fn proc_macro_quoted_spans(&self) -> impl Iterator { - // This is equivalent to `.iter().copied().enumerate()`, but that isn't possible for - // AppendOnlyVec, so we resort to this scheme. - self.proc_macro_quoted_spans.iter_enumerated() - } - pub fn dcx(&self) -> DiagCtxtHandle<'_> { self.dcx.handle() } diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index 6a9027046af00..56dad6068b9dc 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -75,8 +75,6 @@ macro_rules! with_api { fn span_subspan(span: $Span, start: Bound, end: Bound) -> Option<$Span>; fn span_resolved_at(span: $Span, at: $Span) -> $Span; fn span_source_text(span: $Span) -> Option; - fn span_save_span(span: $Span) -> usize; - fn span_recover_proc_macro_span(id: usize) -> $Span; fn symbol_normalize_and_validate_ident(string: &str) -> Result<$Symbol, ()>; } diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index e2f39c015bdd7..7fe8446076081 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -239,7 +239,7 @@ impl Default for TokenStream { } #[unstable(feature = "proc_macro_quote", issue = "54722")] -pub use quote::{HasIterator, RepInterp, ThereIsNoIteratorInRepetition, ext, quote, quote_span}; +pub use quote::{HasIterator, RepInterp, ThereIsNoIteratorInRepetition, ext, quote}; fn tree_to_bridge_tree( tree: TokenTree, @@ -619,20 +619,6 @@ impl Span { BridgeMethods::span_source_text(self.0) } - // Used by the implementation of `Span::quote` - #[doc(hidden)] - #[unstable(feature = "proc_macro_internals", issue = "27812")] - pub fn save_span(&self) -> usize { - BridgeMethods::span_save_span(self.0) - } - - // Used by the implementation of `Span::quote` - #[doc(hidden)] - #[unstable(feature = "proc_macro_internals", issue = "27812")] - pub fn recover_proc_macro_span(id: usize) -> Span { - Span(BridgeMethods::span_recover_proc_macro_span(id)) - } - diagnostic_method!(error, Level::Error); diagnostic_method!(warning, Level::Warning); diagnostic_method!(note, Level::Note); diff --git a/library/proc_macro/src/quote.rs b/library/proc_macro/src/quote.rs index dbb55cd9fb300..a7a78fb0dc000 100644 --- a/library/proc_macro/src/quote.rs +++ b/library/proc_macro/src/quote.rs @@ -280,7 +280,6 @@ pub fn quote(stream: TokenStream) -> TokenStream { if stream.is_empty() { return minimal_quote!(crate::TokenStream::new()); } - let proc_macro_crate = minimal_quote!(crate); let mut after_dollar = false; let mut tokens = crate::TokenStream::new(); @@ -415,7 +414,7 @@ pub fn quote(stream: TokenStream) -> TokenStream { }; minimal_quote!(crate::ToTokens::to_tokens(&crate::TokenTree::Ident((@ ctor)( (@ TokenTree::from(Literal::string(literal))), - (@ quote_span(proc_macro_crate.clone(), tt.span())), + crate::Span::def_site(), )), &mut ts);) } TokenTree::Literal(tt) => { @@ -427,7 +426,7 @@ pub fn quote(stream: TokenStream) -> TokenStream { if let (Some(crate::TokenTree::Literal(mut lit)), None) = (iter.next(), iter.next()) { - lit.set_span((@ quote_span(proc_macro_crate.clone(), tt.span()))); + lit.set_span(crate::Span::def_site()); lit } else { unreachable!() @@ -476,11 +475,3 @@ fn collect_meta_vars(content_stream: TokenStream) -> Vec { helper(content_stream, &mut vars); vars } - -/// Quote a `Span` into a `TokenStream`. -/// This is needed to implement a custom quoter. -#[unstable(feature = "proc_macro_quote", issue = "54722")] -pub fn quote_span(proc_macro_crate: TokenStream, span: Span) -> TokenStream { - let id = span.save_span(); - minimal_quote!((@ proc_macro_crate ) ::Span::recover_proc_macro_span((@ TokenTree::from(Literal::usize_unsuffixed(id))))) -} From 923693d67b3465a5b526bf3fa5670f043ee10d44 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 29 May 2024 13:55:51 +0000 Subject: [PATCH 2/3] Fix the proc-macro-srv --- .../src/server_impl/rust_analyzer_span.rs | 12 ------------ .../proc-macro-srv/src/server_impl/token_id.rs | 6 ------ 2 files changed, 18 deletions(-) diff --git a/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs b/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs index c114d52ec33c6..6a0de4f34e6b1 100644 --- a/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs +++ b/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs @@ -148,18 +148,6 @@ impl server::Server for RaSpanServer<'_> { fn span_local_file(&mut self, span: Self::Span) -> Option { self.callback.as_mut().and_then(|cb| cb.local_file(span.anchor.file_id.file_id())) } - fn span_save_span(&mut self, _span: Self::Span) -> usize { - // FIXME, quote is incompatible with third-party tools - // This is called by the quote proc-macro which is expanded when the proc-macro is compiled - // As such, r-a will never observe this - 0 - } - fn span_recover_proc_macro_span(&mut self, _id: usize) -> Self::Span { - // FIXME, quote is incompatible with third-party tools - // This is called by the expansion of quote!, r-a will observe this, but we don't have - // access to the spans that were encoded - self.call_site - } /// Recent feature, not yet in the proc_macro /// /// See PR: diff --git a/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs b/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs index 70484c4dc28fe..fc8fa25a4ca39 100644 --- a/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs +++ b/src/tools/rust-analyzer/crates/proc-macro-srv/src/server_impl/token_id.rs @@ -145,12 +145,6 @@ impl server::Server for SpanIdServer<'_> { fn span_local_file(&mut self, _span: Self::Span) -> Option { None } - fn span_save_span(&mut self, _span: Self::Span) -> usize { - 0 - } - fn span_recover_proc_macro_span(&mut self, _id: usize) -> Self::Span { - self.call_site - } /// Recent feature, not yet in the proc_macro /// /// See PR: From f0fa136d3a24455097aa15def8cbc4ca826c5185 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 29 May 2024 18:33:13 +0000 Subject: [PATCH 3/3] Fix tests --- tests/ui/proc-macro/auxiliary/custom-quote.rs | 29 ---------- .../auxiliary/span-from-proc-macro.rs | 9 ---- tests/ui/proc-macro/mixed-site-span.stderr | 14 ++--- tests/ui/proc-macro/quote/debug.stdout | 12 ++--- tests/ui/proc-macro/span-from-proc-macro.rs | 2 - .../ui/proc-macro/span-from-proc-macro.stderr | 54 +++++++------------ 6 files changed, 33 insertions(+), 87 deletions(-) delete mode 100644 tests/ui/proc-macro/auxiliary/custom-quote.rs diff --git a/tests/ui/proc-macro/auxiliary/custom-quote.rs b/tests/ui/proc-macro/auxiliary/custom-quote.rs deleted file mode 100644 index bccbed8a6b492..0000000000000 --- a/tests/ui/proc-macro/auxiliary/custom-quote.rs +++ /dev/null @@ -1,29 +0,0 @@ -// ignore-tidy-linelength - -#![feature(proc_macro_quote)] - -extern crate proc_macro; -use std::iter::FromIterator; -use std::str::FromStr; -use proc_macro::*; - -#[proc_macro] -pub fn custom_quote(input: TokenStream) -> TokenStream { - let mut tokens: Vec<_> = input.into_iter().collect(); - assert_eq!(tokens.len(), 1, "Unexpected input: {:?}", tokens); - match tokens.pop() { - Some(TokenTree::Ident(ident)) => { - assert_eq!(ident.to_string(), "my_ident"); - - let proc_macro_crate = TokenStream::from_str("::proc_macro").unwrap(); - let quoted_span = proc_macro::quote_span(proc_macro_crate, ident.span()); - let prefix = TokenStream::from_str(r#"let mut ident = proc_macro::Ident::new("my_ident", proc_macro::Span::call_site());"#).unwrap(); - let set_span_method = TokenStream::from_str("ident.set_span").unwrap(); - let set_span_arg = TokenStream::from(TokenTree::Group(Group::new(Delimiter::Parenthesis, quoted_span))); - let suffix = TokenStream::from_str(";proc_macro::TokenStream::from(proc_macro::TokenTree::Ident(ident))").unwrap(); - let full_stream = TokenStream::from_iter([prefix, set_span_method, set_span_arg, suffix]); - full_stream - } - _ => unreachable!() - } -} diff --git a/tests/ui/proc-macro/auxiliary/span-from-proc-macro.rs b/tests/ui/proc-macro/auxiliary/span-from-proc-macro.rs index 16ca5e3f9e2d1..98dd665984e26 100644 --- a/tests/ui/proc-macro/auxiliary/span-from-proc-macro.rs +++ b/tests/ui/proc-macro/auxiliary/span-from-proc-macro.rs @@ -1,8 +1,6 @@ #![feature(proc_macro_quote)] -#![feature(proc_macro_internals)] // FIXME - this shouldn't be necessary extern crate proc_macro; -extern crate custom_quote; use proc_macro::{quote, TokenStream}; @@ -19,13 +17,6 @@ pub fn error_from_bang(_input: TokenStream) -> TokenStream { expand_to_quote!() } -#[proc_macro] -pub fn other_error_from_bang(_input: TokenStream) -> TokenStream { - custom_quote::custom_quote! { - my_ident - } -} - #[proc_macro_attribute] pub fn error_from_attribute(_args: TokenStream, _input: TokenStream) -> TokenStream { quote! { diff --git a/tests/ui/proc-macro/mixed-site-span.stderr b/tests/ui/proc-macro/mixed-site-span.stderr index fd941f65b788c..f2fcfa21a3c36 100644 --- a/tests/ui/proc-macro/mixed-site-span.stderr +++ b/tests/ui/proc-macro/mixed-site-span.stderr @@ -130,10 +130,11 @@ LL | invoke_with_ident!{krate input TokenItem} | = note: this error originates in the macro `with_crate` which comes from the expansion of the macro `invoke_with_ident` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider importing this struct instead - --> $DIR/auxiliary/mixed-site-span.rs:60:34 + --> $DIR/auxiliary/mixed-site-span.rs:41:1 + | +LL - pub fn with_crate(input: TokenStream) -> TokenStream { +LL + token_site_span::TokenItem as _ { | -LL | quote!(use $krate::$ident as token_site_span::TokenItem as _;) - | +++++++++++++++++++++++++++++ error[E0432]: unresolved import `$crate::TokenItem` --> $DIR/mixed-site-span.rs:90:5 @@ -143,10 +144,11 @@ LL | with_crate!{krate input TokenItem} | = note: this error originates in the macro `with_crate` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider importing this struct instead - --> $DIR/auxiliary/mixed-site-span.rs:60:34 + --> $DIR/auxiliary/mixed-site-span.rs:41:1 + | +LL - pub fn with_crate(input: TokenStream) -> TokenStream { +LL + token_site_span::TokenItem as _ { | -LL | quote!(use $krate::$ident as token_site_span::TokenItem as _;) - | +++++++++++++++++++++++++++++ error[E0432]: unresolved import `$crate` --> $DIR/mixed-site-span.rs:91:5 diff --git a/tests/ui/proc-macro/quote/debug.stdout b/tests/ui/proc-macro/quote/debug.stdout index 896c809fedaba..c8f511d931835 100644 --- a/tests/ui/proc-macro/quote/debug.stdout +++ b/tests/ui/proc-macro/quote/debug.stdout @@ -22,9 +22,9 @@ fn main() { { let mut ts = crate::TokenStream::new(); crate::ToTokens::to_tokens(&crate::TokenTree::Ident(crate::Ident::new("let", - crate::Span::recover_proc_macro_span(0))), &mut ts); + crate::Span::def_site())), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Ident(crate::Ident::new("hello", - crate::Span::recover_proc_macro_span(1))), &mut ts); + crate::Span::def_site())), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new('=', crate::Spacing::Alone)), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Literal({ @@ -32,7 +32,7 @@ fn main() { "\"world\"".parse::().unwrap().into_iter(); if let (Some(crate::TokenTree::Literal(mut lit)), None) = (iter.next(), iter.next()) { - lit.set_span(crate::Span::recover_proc_macro_span(2)); + lit.set_span(crate::Span::def_site()); lit } else { ::core::panicking::panic("internal error: entered unreachable code") @@ -41,9 +41,9 @@ fn main() { crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';', crate::Spacing::Alone)), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Ident(crate::Ident::new("let", - crate::Span::recover_proc_macro_span(3))), &mut ts); + crate::Span::def_site())), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Ident(crate::Ident::new_raw("raw_ident", - crate::Span::recover_proc_macro_span(4))), &mut ts); + crate::Span::def_site())), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new('=', crate::Spacing::Alone)), &mut ts); crate::ToTokens::to_tokens(&crate::TokenTree::Literal({ @@ -51,7 +51,7 @@ fn main() { "r#\"raw\"literal\"#".parse::().unwrap().into_iter(); if let (Some(crate::TokenTree::Literal(mut lit)), None) = (iter.next(), iter.next()) { - lit.set_span(crate::Span::recover_proc_macro_span(5)); + lit.set_span(crate::Span::def_site()); lit } else { ::core::panicking::panic("internal error: entered unreachable code") diff --git a/tests/ui/proc-macro/span-from-proc-macro.rs b/tests/ui/proc-macro/span-from-proc-macro.rs index 24a28d53476c1..44403bccd3c74 100644 --- a/tests/ui/proc-macro/span-from-proc-macro.rs +++ b/tests/ui/proc-macro/span-from-proc-macro.rs @@ -1,4 +1,3 @@ -//@ proc-macro: custom-quote.rs //@ proc-macro: span-from-proc-macro.rs //@ compile-flags: -Z macro-backtrace //@ ignore-backends: gcc @@ -14,5 +13,4 @@ struct Kept; fn main() { error_from_bang!(); //~ ERROR mismatched types - other_error_from_bang!(); //~ ERROR cannot find value `my_ident` } diff --git a/tests/ui/proc-macro/span-from-proc-macro.stderr b/tests/ui/proc-macro/span-from-proc-macro.stderr index 0605c727733b2..9ef61f0c797b7 100644 --- a/tests/ui/proc-macro/span-from-proc-macro.stderr +++ b/tests/ui/proc-macro/span-from-proc-macro.stderr @@ -1,62 +1,46 @@ error[E0425]: cannot find type `MissingType` in this scope - --> $DIR/auxiliary/span-from-proc-macro.rs:33:20 + --> $DIR/auxiliary/span-from-proc-macro.rs:21:1 | LL | pub fn error_from_attribute(_args: TokenStream, _input: TokenStream) -> TokenStream { - | ----------------------------------------------------------------------------------- in this expansion of `#[error_from_attribute]` -... -LL | field: MissingType - | ^^^^^^^^^^^ not found in this scope + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | not found in this scope + | in this expansion of `#[error_from_attribute]` | - ::: $DIR/span-from-proc-macro.rs:9:1 + ::: $DIR/span-from-proc-macro.rs:8:1 | LL | #[error_from_attribute] | ----------------------- in this attribute macro expansion error[E0425]: cannot find type `OtherMissingType` in this scope - --> $DIR/auxiliary/span-from-proc-macro.rs:42:21 + --> $DIR/auxiliary/span-from-proc-macro.rs:30:1 | LL | pub fn error_from_derive(_input: TokenStream) -> TokenStream { - | ------------------------------------------------------------ in this expansion of `#[derive(ErrorFromDerive)]` -... -LL | Variant(OtherMissingType) - | ^^^^^^^^^^^^^^^^ not found in this scope + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | not found in this scope + | in this expansion of `#[derive(ErrorFromDerive)]` | - ::: $DIR/span-from-proc-macro.rs:12:10 + ::: $DIR/span-from-proc-macro.rs:11:10 | LL | #[derive(ErrorFromDerive)] | --------------- in this derive macro expansion -error[E0425]: cannot find value `my_ident` in this scope - --> $DIR/auxiliary/span-from-proc-macro.rs:25:9 - | -LL | pub fn other_error_from_bang(_input: TokenStream) -> TokenStream { - | ---------------------------------------------------------------- in this expansion of `other_error_from_bang!` -LL | custom_quote::custom_quote! { -LL | my_ident - | ^^^^^^^^ not found in this scope - | - ::: $DIR/span-from-proc-macro.rs:17:5 - | -LL | other_error_from_bang!(); - | ------------------------ in this macro invocation - error[E0308]: mismatched types - --> $DIR/auxiliary/span-from-proc-macro.rs:12:36 + --> $DIR/auxiliary/span-from-proc-macro.rs:16:1 | -LL | let bang_error: bool = 25; - | ---- ^^ expected `bool`, found integer - | | - | expected due to this -... LL | pub fn error_from_bang(_input: TokenStream) -> TokenStream { - | ---------------------------------------------------------- in this expansion of `error_from_bang!` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | expected `bool`, found integer + | in this expansion of `error_from_bang!` | - ::: $DIR/span-from-proc-macro.rs:16:5 + ::: $DIR/span-from-proc-macro.rs:15:5 | LL | error_from_bang!(); | ------------------ in this macro invocation -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`.