From e8c48c689575c5469cf41f693d80786dfa9df537 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 23 Jan 2026 11:19:21 +0000 Subject: [PATCH] Fix review comments --- .../rustc_expand/src/proc_macro_server.rs | 20 +++--- library/proc_macro/src/bridge/client.rs | 4 +- library/proc_macro/src/bridge/mod.rs | 20 +++--- library/proc_macro/src/lib.rs | 70 +++++++++---------- .../src/server_impl/rust_analyzer_span.rs | 20 +++--- .../src/server_impl/token_id.rs | 20 +++--- 6 files changed, 76 insertions(+), 78 deletions(-) diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 4719829d6d3a..a51aa90355bc 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -566,19 +566,19 @@ fn emit_diagnostic(&mut self, diagnostic: Diagnostic) { diag.emit(); } - fn tt_drop(&mut self, stream: Self::TokenStream) { + fn ts_drop(&mut self, stream: Self::TokenStream) { drop(stream); } - fn tt_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream { + fn ts_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream { stream.clone() } - fn tt_is_empty(&mut self, stream: &Self::TokenStream) -> bool { + fn ts_is_empty(&mut self, stream: &Self::TokenStream) -> bool { stream.is_empty() } - fn tt_from_str(&mut self, src: &str) -> Self::TokenStream { + fn ts_from_str(&mut self, src: &str) -> Self::TokenStream { unwrap_or_emit_fatal(source_str_to_stream( self.psess(), FileName::proc_macro_source_code(src), @@ -587,11 +587,11 @@ fn tt_from_str(&mut self, src: &str) -> Self::TokenStream { )) } - fn tt_to_string(&mut self, stream: &Self::TokenStream) -> String { + fn ts_to_string(&mut self, stream: &Self::TokenStream) -> String { pprust::tts_to_string(stream) } - fn tt_expand_expr(&mut self, stream: &Self::TokenStream) -> Result { + fn ts_expand_expr(&mut self, stream: &Self::TokenStream) -> Result { // Parse the expression from our tokenstream. let expr: PResult<'_, _> = try { let mut p = Parser::new(self.psess(), stream.clone(), Some("proc_macro expand expr")); @@ -652,14 +652,14 @@ fn tt_expand_expr(&mut self, stream: &Self::TokenStream) -> Result, ) -> Self::TokenStream { Self::TokenStream::new((tree, &mut *self).to_internal().into_iter().collect::>()) } - fn tt_concat_trees( + fn ts_concat_trees( &mut self, base: Option, trees: Vec>, @@ -673,7 +673,7 @@ fn tt_concat_trees( stream } - fn tt_concat_streams( + fn ts_concat_streams( &mut self, base: Option, streams: Vec, @@ -685,7 +685,7 @@ fn tt_concat_streams( stream } - fn tt_into_trees( + fn ts_into_trees( &mut self, stream: Self::TokenStream, ) -> Vec> { diff --git a/library/proc_macro/src/bridge/client.rs b/library/proc_macro/src/bridge/client.rs index 058a9420f6e0..0d87a727ae40 100644 --- a/library/proc_macro/src/bridge/client.rs +++ b/library/proc_macro/src/bridge/client.rs @@ -25,7 +25,7 @@ impl !Sync for TokenStream {} // Forward `Drop::drop` to the inherent `drop` method. impl Drop for TokenStream { fn drop(&mut self) { - Methods::tt_drop(TokenStream { handle: self.handle }); + Methods::ts_drop(TokenStream { handle: self.handle }); } } @@ -75,7 +75,7 @@ fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { impl Clone for TokenStream { fn clone(&self) -> Self { - Methods::tt_clone(self) + Methods::ts_clone(self) } } diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index 429335456316..6f7c8726f925 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -56,24 +56,24 @@ macro_rules! with_api { fn literal_from_str(s: &str) -> Result, ()>; fn emit_diagnostic(diagnostic: Diagnostic<$S::Span>); - fn tt_drop(stream: $S::TokenStream); - fn tt_clone(stream: &$S::TokenStream) -> $S::TokenStream; - fn tt_is_empty(stream: &$S::TokenStream) -> bool; - fn tt_expand_expr(stream: &$S::TokenStream) -> Result<$S::TokenStream, ()>; - fn tt_from_str(src: &str) -> $S::TokenStream; - fn tt_to_string(stream: &$S::TokenStream) -> String; - fn tt_from_token_tree( + fn ts_drop(stream: $S::TokenStream); + fn ts_clone(stream: &$S::TokenStream) -> $S::TokenStream; + fn ts_is_empty(stream: &$S::TokenStream) -> bool; + fn ts_expand_expr(stream: &$S::TokenStream) -> Result<$S::TokenStream, ()>; + fn ts_from_str(src: &str) -> $S::TokenStream; + fn ts_to_string(stream: &$S::TokenStream) -> String; + fn ts_from_token_tree( tree: TokenTree<$S::TokenStream, $S::Span, $S::Symbol>, ) -> $S::TokenStream; - fn tt_concat_trees( + fn ts_concat_trees( base: Option<$S::TokenStream>, trees: Vec>, ) -> $S::TokenStream; - fn tt_concat_streams( + fn ts_concat_streams( base: Option<$S::TokenStream>, streams: Vec<$S::TokenStream>, ) -> $S::TokenStream; - fn tt_into_trees( + fn ts_into_trees( stream: $S::TokenStream ) -> Vec>; diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index cc2de8d470c9..95a7ea7d7b3b 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -58,6 +58,7 @@ #[unstable(feature = "proc_macro_totokens", issue = "130977")] pub use to_tokens::ToTokens; +use crate::bridge::client::Methods as BridgeMethods; use crate::escape::{EscapeOptions, escape_bytes}; /// Errors returned when trying to retrieve a literal unescaped value. @@ -158,7 +159,7 @@ pub fn new() -> TokenStream { /// Checks if this `TokenStream` is empty. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn is_empty(&self) -> bool { - self.0.as_ref().map(|h| bridge::client::Methods::tt_is_empty(h)).unwrap_or(true) + self.0.as_ref().map(|h| BridgeMethods::ts_is_empty(h)).unwrap_or(true) } /// Parses this `TokenStream` as an expression and attempts to expand any @@ -174,7 +175,7 @@ pub fn is_empty(&self) -> bool { #[unstable(feature = "proc_macro_expand", issue = "90765")] pub fn expand_expr(&self) -> Result { let stream = self.0.as_ref().ok_or(ExpandError)?; - match bridge::client::Methods::tt_expand_expr(stream) { + match BridgeMethods::ts_expand_expr(stream) { Ok(stream) => Ok(TokenStream(Some(stream))), Err(_) => Err(ExpandError), } @@ -193,7 +194,7 @@ impl FromStr for TokenStream { type Err = LexError; fn from_str(src: &str) -> Result { - Ok(TokenStream(Some(bridge::client::Methods::tt_from_str(src)))) + Ok(TokenStream(Some(BridgeMethods::ts_from_str(src)))) } } @@ -212,7 +213,7 @@ fn from_str(src: &str) -> Result { impl fmt::Display for TokenStream { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match &self.0 { - Some(ts) => write!(f, "{}", bridge::client::Methods::tt_to_string(ts)), + Some(ts) => write!(f, "{}", BridgeMethods::ts_to_string(ts)), None => Ok(()), } } @@ -252,7 +253,7 @@ fn tree_to_bridge_tree( #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl From for TokenStream { fn from(tree: TokenTree) -> TokenStream { - TokenStream(Some(bridge::client::Methods::tt_from_token_tree(tree_to_bridge_tree(tree)))) + TokenStream(Some(BridgeMethods::ts_from_token_tree(tree_to_bridge_tree(tree)))) } } @@ -281,7 +282,7 @@ fn build(self) -> TokenStream { if self.trees.is_empty() { TokenStream(None) } else { - TokenStream(Some(bridge::client::Methods::tt_concat_trees(None, self.trees))) + TokenStream(Some(BridgeMethods::ts_concat_trees(None, self.trees))) } } @@ -289,7 +290,7 @@ fn append_to(self, stream: &mut TokenStream) { if self.trees.is_empty() { return; } - stream.0 = Some(bridge::client::Methods::tt_concat_trees(stream.0.take(), self.trees)) + stream.0 = Some(BridgeMethods::ts_concat_trees(stream.0.take(), self.trees)) } } @@ -314,7 +315,7 @@ fn build(mut self) -> TokenStream { if self.streams.len() <= 1 { TokenStream(self.streams.pop()) } else { - TokenStream(Some(bridge::client::Methods::tt_concat_streams(None, self.streams))) + TokenStream(Some(BridgeMethods::ts_concat_streams(None, self.streams))) } } @@ -326,7 +327,7 @@ fn append_to(mut self, stream: &mut TokenStream) { if base.is_none() && self.streams.len() == 1 { stream.0 = self.streams.pop(); } else { - stream.0 = Some(bridge::client::Methods::tt_concat_streams(base, self.streams)); + stream.0 = Some(BridgeMethods::ts_concat_streams(base, self.streams)); } } } @@ -377,7 +378,7 @@ fn extend>(&mut self, streams: I) { macro_rules! extend_items { ($($item:ident)*) => { $( - #[stable(feature = "token_stream_extend_tt_items", since = "1.92.0")] + #[stable(feature = "token_stream_extend_ts_items", since = "1.92.0")] impl Extend<$item> for TokenStream { fn extend>(&mut self, iter: T) { self.extend(iter.into_iter().map(TokenTree::$item)); @@ -392,7 +393,7 @@ fn extend>(&mut self, iter: T) { /// Public implementation details for the `TokenStream` type, such as iterators. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub mod token_stream { - use crate::{Group, Ident, Literal, Punct, TokenStream, TokenTree, bridge}; + use crate::{BridgeMethods, Group, Ident, Literal, Punct, TokenStream, TokenTree, bridge}; /// An iterator over `TokenStream`'s `TokenTree`s. /// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups, @@ -438,10 +439,7 @@ impl IntoIterator for TokenStream { fn into_iter(self) -> IntoIter { IntoIter( - self.0 - .map(|v| bridge::client::Methods::tt_into_trees(v)) - .unwrap_or_default() - .into_iter(), + self.0.map(|v| BridgeMethods::ts_into_trees(v)).unwrap_or_default().into_iter(), ) } } @@ -514,7 +512,7 @@ pub fn mixed_site() -> Span { /// `self` was generated from, if any. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn parent(&self) -> Option { - bridge::client::Methods::span_parent(self.0).map(Span) + BridgeMethods::span_parent(self.0).map(Span) } /// The span for the origin source code that `self` was generated from. If @@ -522,25 +520,25 @@ pub fn parent(&self) -> Option { /// value is the same as `*self`. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn source(&self) -> Span { - Span(bridge::client::Methods::span_source(self.0)) + Span(BridgeMethods::span_source(self.0)) } /// Returns the span's byte position range in the source file. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn byte_range(&self) -> Range { - bridge::client::Methods::span_byte_range(self.0) + BridgeMethods::span_byte_range(self.0) } /// Creates an empty span pointing to directly before this span. #[stable(feature = "proc_macro_span_location", since = "1.88.0")] pub fn start(&self) -> Span { - Span(bridge::client::Methods::span_start(self.0)) + Span(BridgeMethods::span_start(self.0)) } /// Creates an empty span pointing to directly after this span. #[stable(feature = "proc_macro_span_location", since = "1.88.0")] pub fn end(&self) -> Span { - Span(bridge::client::Methods::span_end(self.0)) + Span(BridgeMethods::span_end(self.0)) } /// The one-indexed line of the source file where the span starts. @@ -548,7 +546,7 @@ pub fn end(&self) -> Span { /// To obtain the line of the span's end, use `span.end().line()`. #[stable(feature = "proc_macro_span_location", since = "1.88.0")] pub fn line(&self) -> usize { - bridge::client::Methods::span_line(self.0) + BridgeMethods::span_line(self.0) } /// The one-indexed column of the source file where the span starts. @@ -556,7 +554,7 @@ pub fn line(&self) -> usize { /// To obtain the column of the span's end, use `span.end().column()`. #[stable(feature = "proc_macro_span_location", since = "1.88.0")] pub fn column(&self) -> usize { - bridge::client::Methods::span_column(self.0) + BridgeMethods::span_column(self.0) } /// The path to the source file in which this span occurs, for display purposes. @@ -565,7 +563,7 @@ pub fn column(&self) -> usize { /// It might be remapped (e.g. `"/src/lib.rs"`) or an artificial path (e.g. `""`). #[stable(feature = "proc_macro_span_file", since = "1.88.0")] pub fn file(&self) -> String { - bridge::client::Methods::span_file(self.0) + BridgeMethods::span_file(self.0) } /// The path to the source file in which this span occurs on the local file system. @@ -575,7 +573,7 @@ pub fn file(&self) -> String { /// This path should not be embedded in the output of the macro; prefer `file()` instead. #[stable(feature = "proc_macro_span_file", since = "1.88.0")] pub fn local_file(&self) -> Option { - bridge::client::Methods::span_local_file(self.0).map(PathBuf::from) + BridgeMethods::span_local_file(self.0).map(PathBuf::from) } /// Creates a new span encompassing `self` and `other`. @@ -583,14 +581,14 @@ pub fn local_file(&self) -> Option { /// Returns `None` if `self` and `other` are from different files. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn join(&self, other: Span) -> Option { - bridge::client::Methods::span_join(self.0, other.0).map(Span) + BridgeMethods::span_join(self.0, other.0).map(Span) } /// Creates a new span with the same line/column information as `self` but /// that resolves symbols as though it were at `other`. #[stable(feature = "proc_macro_span_resolved_at", since = "1.45.0")] pub fn resolved_at(&self, other: Span) -> Span { - Span(bridge::client::Methods::span_resolved_at(self.0, other.0)) + Span(BridgeMethods::span_resolved_at(self.0, other.0)) } /// Creates a new span with the same name resolution behavior as `self` but @@ -615,21 +613,21 @@ pub fn eq(&self, other: &Span) -> bool { /// be used for diagnostics only. #[stable(feature = "proc_macro_source_text", since = "1.66.0")] pub fn source_text(&self) -> Option { - bridge::client::Methods::span_source_text(self.0) + 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 { - bridge::client::Methods::span_save_span(self.0) + 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(bridge::client::Methods::span_recover_proc_macro_span(id)) + Span(BridgeMethods::span_recover_proc_macro_span(id)) } diagnostic_method!(error, Level::Error); @@ -1394,7 +1392,7 @@ pub fn set_span(&mut self, span: Span) { // was 'c' or whether it was '\u{63}'. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn subspan>(&self, range: R) -> Option { - bridge::client::Methods::span_subspan( + BridgeMethods::span_subspan( self.0.span, range.start_bound().cloned(), range.end_bound().cloned(), @@ -1569,7 +1567,7 @@ impl FromStr for Literal { type Err = LexError; fn from_str(src: &str) -> Result { - match bridge::client::Methods::literal_from_str(src) { + match BridgeMethods::literal_from_str(src) { Ok(literal) => Ok(Literal(literal)), Err(()) => Err(LexError), } @@ -1611,11 +1609,12 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { )] /// Functionality for adding environment state to the build dependency info. pub mod tracked { - use std::env::{self, VarError}; use std::ffi::OsStr; use std::path::Path; + use crate::BridgeMethods; + /// Retrieve an environment variable and add it to build dependency info. /// The build system executing the compiler will know that the variable was accessed during /// compilation, and will be able to rerun the build when the value of that variable changes. @@ -1624,9 +1623,8 @@ pub mod tracked { #[unstable(feature = "proc_macro_tracked_env", issue = "99515")] pub fn env_var + AsRef>(key: K) -> Result { let key: &str = key.as_ref(); - let value = - crate::bridge::client::Methods::injected_env_var(key).map_or_else(|| env::var(key), Ok); - crate::bridge::client::Methods::track_env_var(key, value.as_deref().ok()); + let value = BridgeMethods::injected_env_var(key).map_or_else(|| env::var(key), Ok); + BridgeMethods::track_env_var(key, value.as_deref().ok()); value } @@ -1636,6 +1634,6 @@ pub fn env_var + AsRef>(key: K) -> Result #[unstable(feature = "proc_macro_tracked_path", issue = "99515")] pub fn path>(path: P) { let path: &str = path.as_ref().to_str().unwrap(); - crate::bridge::client::Methods::track_path(path); + BridgeMethods::track_path(path); } } 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 4f50c04b2799..ec30630c10bb 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 @@ -72,18 +72,18 @@ fn emit_diagnostic(&mut self, _: Diagnostic) { // FIXME handle diagnostic } - fn tt_drop(&mut self, stream: Self::TokenStream) { + fn ts_drop(&mut self, stream: Self::TokenStream) { drop(stream); } - fn tt_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream { + fn ts_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream { stream.clone() } - fn tt_is_empty(&mut self, stream: &Self::TokenStream) -> bool { + fn ts_is_empty(&mut self, stream: &Self::TokenStream) -> bool { stream.is_empty() } - fn tt_from_str(&mut self, src: &str) -> Self::TokenStream { + fn ts_from_str(&mut self, src: &str) -> Self::TokenStream { Self::TokenStream::from_str(src, self.call_site).unwrap_or_else(|e| { Self::TokenStream::from_str( &format!("compile_error!(\"failed to parse str to token stream: {e}\")"), @@ -92,15 +92,15 @@ fn tt_from_str(&mut self, src: &str) -> Self::TokenStream { .unwrap() }) } - fn tt_to_string(&mut self, stream: &Self::TokenStream) -> String { + fn ts_to_string(&mut self, stream: &Self::TokenStream) -> String { stream.to_string() } - fn tt_from_token_tree(&mut self, tree: TokenTree) -> Self::TokenStream { + fn ts_from_token_tree(&mut self, tree: TokenTree) -> Self::TokenStream { Self::TokenStream::new(vec![tree]) } - fn tt_expand_expr(&mut self, self_: &Self::TokenStream) -> Result { + fn ts_expand_expr(&mut self, self_: &Self::TokenStream) -> Result { // FIXME: requires db, more importantly this requires name resolution so we would need to // eagerly expand this proc-macro, but we can't know that this proc-macro is eager until we // expand it ... @@ -109,7 +109,7 @@ fn tt_expand_expr(&mut self, self_: &Self::TokenStream) -> Result, trees: Vec>, @@ -125,7 +125,7 @@ fn tt_concat_trees( } } - fn tt_concat_streams( + fn ts_concat_streams( &mut self, base: Option, streams: Vec, @@ -137,7 +137,7 @@ fn tt_concat_streams( stream } - fn tt_into_trees(&mut self, stream: Self::TokenStream) -> Vec> { + fn ts_into_trees(&mut self, stream: Self::TokenStream) -> Vec> { (*stream.0).clone() } 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 cd5ba5909561..3bf07290c8c0 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 @@ -75,18 +75,18 @@ fn literal_from_str(&mut self, s: &str) -> Result, ()> { fn emit_diagnostic(&mut self, _: Diagnostic) {} - fn tt_drop(&mut self, stream: Self::TokenStream) { + fn ts_drop(&mut self, stream: Self::TokenStream) { drop(stream); } - fn tt_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream { + fn ts_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream { stream.clone() } - fn tt_is_empty(&mut self, stream: &Self::TokenStream) -> bool { + fn ts_is_empty(&mut self, stream: &Self::TokenStream) -> bool { stream.is_empty() } - fn tt_from_str(&mut self, src: &str) -> Self::TokenStream { + fn ts_from_str(&mut self, src: &str) -> Self::TokenStream { Self::TokenStream::from_str(src, self.call_site).unwrap_or_else(|e| { Self::TokenStream::from_str( &format!("compile_error!(\"failed to parse str to token stream: {e}\")"), @@ -95,18 +95,18 @@ fn tt_from_str(&mut self, src: &str) -> Self::TokenStream { .unwrap() }) } - fn tt_to_string(&mut self, stream: &Self::TokenStream) -> String { + fn ts_to_string(&mut self, stream: &Self::TokenStream) -> String { stream.to_string() } - fn tt_from_token_tree(&mut self, tree: TokenTree) -> Self::TokenStream { + fn ts_from_token_tree(&mut self, tree: TokenTree) -> Self::TokenStream { Self::TokenStream::new(vec![tree]) } - fn tt_expand_expr(&mut self, self_: &Self::TokenStream) -> Result { + fn ts_expand_expr(&mut self, self_: &Self::TokenStream) -> Result { Ok(self_.clone()) } - fn tt_concat_trees( + fn ts_concat_trees( &mut self, base: Option, trees: Vec>, @@ -122,7 +122,7 @@ fn tt_concat_trees( } } - fn tt_concat_streams( + fn ts_concat_streams( &mut self, base: Option, streams: Vec, @@ -134,7 +134,7 @@ fn tt_concat_streams( stream } - fn tt_into_trees(&mut self, stream: Self::TokenStream) -> Vec> { + fn ts_into_trees(&mut self, stream: Self::TokenStream) -> Vec> { (*stream.0).clone() }