From 4a992043b5c518ac301db251d1f923b9d8ff22af 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 | 48 ------------------- 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/session.rs | 18 +------ library/proc_macro/src/bridge/mod.rs | 2 - library/proc_macro/src/lib.rs | 16 +------ library/proc_macro/src/quote.rs | 13 +---- .../src/server_impl/rust_analyzer_span.rs | 12 ----- 12 files changed, 6 insertions(+), 138 deletions(-) diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 31c776f3fef00..7148f68af56c2 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -24,7 +24,7 @@ use rustc_parse::MACRO_ARGUMENTS; use rustc_parse::parser::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; @@ -1141,10 +1141,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 65af690611919..8a361692f171b 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; @@ -15,9 +14,7 @@ use rustc_proc_macro::bridge::{ DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree, server, }; use rustc_proc_macro::{Delimiter, Level}; -use rustc_session::Session; use rustc_session::parse::ParseSess; -use rustc_span::def_id::CrateNum; use rustc_span::{BytePos, FileName, Pos, Span, Symbol, sym}; use smallvec::{SmallVec, smallvec}; @@ -424,8 +421,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> { @@ -435,16 +430,10 @@ 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, } } - fn sess(&self) -> &Session { - &self.ecx.sess - } - fn psess(&self) -> &ParseSess { self.ecx.psess() } @@ -810,43 +799,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.sess().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 bd3b2445759e7..54ab72f2f0643 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1478,15 +1478,6 @@ impl CrateMetadata { 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, 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 dff8a4078da16..a299e899d75c2 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -635,15 +635,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 cdata = self.get_crate_data_mut(cnum); if !cdata.used { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 16a5a6c8877a5..2b7ab03d63bc2 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1987,10 +1987,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let tcx = self.tcx; let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index; let stability = tcx.lookup_stability(CRATE_DEF_ID); - for (i, span) in self.tcx.sess.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 90721f0f1fc11..8aceea07cb986 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -467,7 +467,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 595c2fdf011dd..3d4c55f7240f1 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::attrs::{AttributeKind, CfgEntry, StrippedCfgItem}; use rustc_hir::def::{DefKind, MacroKinds, Namespace, NonMacroAttrKind}; -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::{Attribute, StabilityLevel}; use rustc_middle::middle::stability; use rustc_middle::ty::{RegisteredTools, TyCtxt}; @@ -496,10 +496,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.owner_def_id(id)) } diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 986dababf770a..32cb5d4d33097 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -8,9 +8,7 @@ use std::{env, io}; use rustc_data_structures::flock; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef}; -use rustc_data_structures::sync::{ - AppendOnlyVec, DynSend, DynSync, Lock, MappedReadGuard, ReadGuard, RwLock, -}; +use rustc_data_structures::sync::{DynSend, DynSync, Lock, MappedReadGuard, ReadGuard, RwLock}; use rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitter; use rustc_errors::codes::*; use rustc_errors::emitter::{DynEmitter, HumanReadableErrorType, OutputTheme, stderr_destination}; @@ -96,9 +94,6 @@ pub struct Session { pub unstable_features: UnstableFeatures, pub config: Cfg, pub check_config: CheckCfg, - /// 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, /// Input, input file path and output file path to this compilation process. pub io: CompilerIO, @@ -310,16 +305,6 @@ impl Session { self.psess.source_map() } - 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 save_proc_macro_span(&self, span: Span) -> usize { - self.proc_macro_quoted_spans.push(span) - } - /// Returns `true` if internal lints should be added to the lint store - i.e. if /// `-Zunstable-options` is provided and this isn't rustdoc (internal lints can trigger errors /// to be emitted under rustdoc). @@ -1113,7 +1098,6 @@ pub fn build_session( unstable_features: UnstableFeatures::from_environment(None), config: Cfg::default(), check_config: CheckCfg::default(), - proc_macro_quoted_spans: Default::default(), io, incr_comp_session: RwLock::new(IncrCompSession::NotInitialized), prof, diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index 8b60043f5dc2a..bcfd287f73824 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -74,8 +74,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 2cba4b52fc276..1a63518eb15c8 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -373,7 +373,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, @@ -751,20 +751,6 @@ impl Span { BridgeMethods::span_source_text(self.0) } - // Used by the implementation of `Span::quote` - #[doc(hidden)] - #[unstable(feature = "proc_macro_internals", issue = "none")] - 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 = "none")] - 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))))) -} 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 bdac9c920c433..72a3b15afdb1d 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 @@ -139,18 +139,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: From 19881e08a411319ccb2b57d9315ac9454a3f03e6 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 --- .../crates/proc-macro-srv/src/server_impl/token_id.rs | 6 ------ 1 file changed, 6 deletions(-) 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 6c393b8befb15..c38f4109478d9 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 @@ -131,12 +131,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 0f2e288e09d0bccb83abb45e3af57131238fec75 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 10af6b428a2f3..3d5c2ac36f983 100644 --- a/tests/ui/proc-macro/mixed-site-span.stderr +++ b/tests/ui/proc-macro/mixed-site-span.stderr @@ -170,10 +170,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 @@ -185,10 +186,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 097cf75223bfb..504328d8cabec 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`.