mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 13:33:31 +00:00
fix: Strip unused token ids from eager macro input token maps
This commit is contained in:
parent
bf5624664d
commit
c7b34e4873
5 changed files with 37 additions and 7 deletions
|
@ -19,7 +19,7 @@
|
||||||
//!
|
//!
|
||||||
//! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros>
|
//! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros>
|
||||||
use base_db::CrateId;
|
use base_db::CrateId;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use syntax::{ted, Parse, SyntaxNode, TextRange, TextSize, WalkEvent};
|
use syntax::{ted, Parse, SyntaxNode, TextRange, TextSize, WalkEvent};
|
||||||
use triomphe::Arc;
|
use triomphe::Arc;
|
||||||
|
|
||||||
|
@ -83,10 +83,11 @@ pub fn expand_eager_macro_input(
|
||||||
mbe::syntax_node_to_token_tree(&expanded_eager_input);
|
mbe::syntax_node_to_token_tree(&expanded_eager_input);
|
||||||
|
|
||||||
let og_tmap = if let Some(tt) = macro_call.value.token_tree() {
|
let og_tmap = if let Some(tt) = macro_call.value.token_tree() {
|
||||||
let og_tmap = mbe::syntax_node_to_token_map(tt.syntax());
|
let mut ids_used = FxHashSet::default();
|
||||||
|
let mut og_tmap = mbe::syntax_node_to_token_map(tt.syntax());
|
||||||
// The tokenmap and ids of subtree point into the expanded syntax node, but that is inaccessible from the outside
|
// The tokenmap and ids of subtree point into the expanded syntax node, but that is inaccessible from the outside
|
||||||
// so we need to remap them to the original input of the eager macro.
|
// so we need to remap them to the original input of the eager macro.
|
||||||
subtree.visit_ids(&|id| {
|
subtree.visit_ids(&mut |id| {
|
||||||
// Note: we discard all token ids of braces and the like here, but that's not too bad and only a temporary fix
|
// Note: we discard all token ids of braces and the like here, but that's not too bad and only a temporary fix
|
||||||
|
|
||||||
if let Some(range) = expanded_eager_input_token_map
|
if let Some(range) = expanded_eager_input_token_map
|
||||||
|
@ -97,6 +98,7 @@ pub fn expand_eager_macro_input(
|
||||||
// remap from eager input expansion to original eager input
|
// remap from eager input expansion to original eager input
|
||||||
if let Some(&og_range) = ws_mapping.get(og_range) {
|
if let Some(&og_range) = ws_mapping.get(og_range) {
|
||||||
if let Some(og_token) = og_tmap.token_by_range(og_range) {
|
if let Some(og_token) = og_tmap.token_by_range(og_range) {
|
||||||
|
ids_used.insert(id);
|
||||||
return og_token;
|
return og_token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,6 +106,7 @@ pub fn expand_eager_macro_input(
|
||||||
}
|
}
|
||||||
tt::TokenId::UNSPECIFIED
|
tt::TokenId::UNSPECIFIED
|
||||||
});
|
});
|
||||||
|
og_tmap.filter(|id| ids_used.contains(&id));
|
||||||
og_tmap
|
og_tmap
|
||||||
} else {
|
} else {
|
||||||
Default::default()
|
Default::default()
|
||||||
|
|
|
@ -90,8 +90,18 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
||||||
<span class="brace">}</span>
|
<span class="brace">}</span>
|
||||||
<span class="brace">}</span>
|
<span class="brace">}</span>
|
||||||
|
|
||||||
|
<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>
|
||||||
|
<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>
|
||||||
|
<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>
|
||||||
|
<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>
|
||||||
|
<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>
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<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>
|
||||||
|
|
||||||
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
|
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
|
||||||
<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>
|
<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>
|
||||||
<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>
|
<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>
|
||||||
<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>
|
<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>
|
||||||
<span class="brace">}</span></code></pre>
|
<span class="brace">}</span>
|
||||||
|
</code></pre>
|
|
@ -48,6 +48,7 @@ fn macros() {
|
||||||
check_highlighting(
|
check_highlighting(
|
||||||
r#"
|
r#"
|
||||||
//- proc_macros: mirror
|
//- proc_macros: mirror
|
||||||
|
//- /lib.rs crate:lib
|
||||||
proc_macros::mirror! {
|
proc_macros::mirror! {
|
||||||
{
|
{
|
||||||
,i32 :x pub
|
,i32 :x pub
|
||||||
|
@ -95,11 +96,23 @@ macro without_args {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rustc_builtin_macro]
|
||||||
|
macro_rules! concat {}
|
||||||
|
#[rustc_builtin_macro]
|
||||||
|
macro_rules! include {}
|
||||||
|
#[rustc_builtin_macro]
|
||||||
|
macro_rules! format_args {}
|
||||||
|
|
||||||
|
include!(concat!("foo/", "foo.rs"));
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, {}!", 92);
|
format_args!("Hello, {}!", 92);
|
||||||
dont_color_me_braces!();
|
dont_color_me_braces!();
|
||||||
noop!(noop!(1));
|
noop!(noop!(1));
|
||||||
}
|
}
|
||||||
|
//- /foo/foo.rs crate:foo
|
||||||
|
mod foo {}
|
||||||
|
use self::foo as bar;
|
||||||
"#,
|
"#,
|
||||||
expect_file!["./test_data/highlight_macros.html"],
|
expect_file!["./test_data/highlight_macros.html"],
|
||||||
false,
|
false,
|
||||||
|
|
|
@ -117,4 +117,8 @@ impl TokenMap {
|
||||||
TokenTextRange::Delimiter(_) => None,
|
TokenTextRange::Delimiter(_) => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn filter(&mut self, id: impl Fn(tt::TokenId) -> bool) {
|
||||||
|
self.entries.retain(|&(tid, _)| id(tid));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ pub mod token_id {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Subtree {
|
impl Subtree {
|
||||||
pub fn visit_ids(&mut self, f: &impl Fn(TokenId) -> TokenId) {
|
pub fn visit_ids(&mut self, f: &mut impl FnMut(TokenId) -> TokenId) {
|
||||||
self.delimiter.open = f(self.delimiter.open);
|
self.delimiter.open = f(self.delimiter.open);
|
||||||
self.delimiter.close = f(self.delimiter.close);
|
self.delimiter.close = f(self.delimiter.close);
|
||||||
self.token_trees.iter_mut().for_each(|tt| match tt {
|
self.token_trees.iter_mut().for_each(|tt| match tt {
|
||||||
|
|
Loading…
Reference in a new issue