Skip to content

Commit c7b34e4

Browse files
committed
fix: Strip unused token ids from eager macro input token maps
1 parent bf56246 commit c7b34e4

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

crates/hir-expand/src/eager.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//!
2020
//! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros>
2121
use base_db::CrateId;
22-
use rustc_hash::FxHashMap;
22+
use rustc_hash::{FxHashMap, FxHashSet};
2323
use syntax::{ted, Parse, SyntaxNode, TextRange, TextSize, WalkEvent};
2424
use triomphe::Arc;
2525

@@ -83,10 +83,11 @@ pub fn expand_eager_macro_input(
8383
mbe::syntax_node_to_token_tree(&expanded_eager_input);
8484

8585
let og_tmap = if let Some(tt) = macro_call.value.token_tree() {
86-
let og_tmap = mbe::syntax_node_to_token_map(tt.syntax());
86+
let mut ids_used = FxHashSet::default();
87+
let mut og_tmap = mbe::syntax_node_to_token_map(tt.syntax());
8788
// The tokenmap and ids of subtree point into the expanded syntax node, but that is inaccessible from the outside
8889
// so we need to remap them to the original input of the eager macro.
89-
subtree.visit_ids(&|id| {
90+
subtree.visit_ids(&mut |id| {
9091
// Note: we discard all token ids of braces and the like here, but that's not too bad and only a temporary fix
9192

9293
if let Some(range) = expanded_eager_input_token_map
@@ -97,13 +98,15 @@ pub fn expand_eager_macro_input(
9798
// remap from eager input expansion to original eager input
9899
if let Some(&og_range) = ws_mapping.get(og_range) {
99100
if let Some(og_token) = og_tmap.token_by_range(og_range) {
101+
ids_used.insert(id);
100102
return og_token;
101103
}
102104
}
103105
}
104106
}
105107
tt::TokenId::UNSPECIFIED
106108
});
109+
og_tmap.filter(|id| ids_used.contains(&id));
107110
og_tmap
108111
} else {
109112
Default::default()

crates/ide/src/syntax_highlighting/test_data/highlight_macros.html

+12-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,18 @@
9090
<span class="brace">}</span>
9191
<span class="brace">}</span>
9292

93+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute library">rustc_builtin_macro</span><span class="attribute_bracket attribute">]</span>
94+
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">concat</span> <span class="brace">{</span><span class="brace">}</span>
95+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute library">rustc_builtin_macro</span><span class="attribute_bracket attribute">]</span>
96+
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">include</span> <span class="brace">{</span><span class="brace">}</span>
97+
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute library">rustc_builtin_macro</span><span class="attribute_bracket attribute">]</span>
98+
<span class="keyword">macro_rules</span><span class="macro_bang">!</span> <span class="macro declaration">format_args</span> <span class="brace">{</span><span class="brace">}</span>
99+
100+
<span class="macro">include</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="none macro">concat</span><span class="punctuation macro">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"foo/"</span><span class="comma macro">,</span> <span class="string_literal macro">"foo.rs"</span><span class="parenthesis macro">)</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
101+
93102
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
94-
<span class="unresolved_reference">println</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"Hello, {}!"</span><span class="comma macro">,</span> <span class="numeric_literal macro">92</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
103+
<span class="macro">format_args</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"Hello, </span><span class="format_specifier">{</span><span class="format_specifier">}</span><span class="string_literal macro">!"</span><span class="comma macro">,</span> <span class="numeric_literal macro">92</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
95104
<span class="macro">dont_color_me_braces</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
96105
<span class="macro">noop</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="macro macro">noop</span><span class="macro_bang macro">!</span><span class="parenthesis macro">(</span><span class="numeric_literal macro">1</span><span class="parenthesis macro">)</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
97-
<span class="brace">}</span></code></pre>
106+
<span class="brace">}</span>
107+
</code></pre>

crates/ide/src/syntax_highlighting/tests.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fn macros() {
4848
check_highlighting(
4949
r#"
5050
//- proc_macros: mirror
51+
//- /lib.rs crate:lib
5152
proc_macros::mirror! {
5253
{
5354
,i32 :x pub
@@ -95,11 +96,23 @@ macro without_args {
9596
}
9697
}
9798
99+
#[rustc_builtin_macro]
100+
macro_rules! concat {}
101+
#[rustc_builtin_macro]
102+
macro_rules! include {}
103+
#[rustc_builtin_macro]
104+
macro_rules! format_args {}
105+
106+
include!(concat!("foo/", "foo.rs"));
107+
98108
fn main() {
99-
println!("Hello, {}!", 92);
109+
format_args!("Hello, {}!", 92);
100110
dont_color_me_braces!();
101111
noop!(noop!(1));
102112
}
113+
//- /foo/foo.rs crate:foo
114+
mod foo {}
115+
use self::foo as bar;
103116
"#,
104117
expect_file!["./test_data/highlight_macros.html"],
105118
false,

crates/mbe/src/token_map.rs

+4
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,8 @@ impl TokenMap {
117117
TokenTextRange::Delimiter(_) => None,
118118
})
119119
}
120+
121+
pub fn filter(&mut self, id: impl Fn(tt::TokenId) -> bool) {
122+
self.entries.retain(|&(tid, _)| id(tid));
123+
}
120124
}

crates/tt/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub mod token_id {
7070
}
7171

7272
impl Subtree {
73-
pub fn visit_ids(&mut self, f: &impl Fn(TokenId) -> TokenId) {
73+
pub fn visit_ids(&mut self, f: &mut impl FnMut(TokenId) -> TokenId) {
7474
self.delimiter.open = f(self.delimiter.open);
7575
self.delimiter.close = f(self.delimiter.close);
7676
self.token_trees.iter_mut().for_each(|tt| match tt {

0 commit comments

Comments
 (0)