Skip to content

Commit 0d441c3

Browse files
authored
Merge pull request rust-lang#18410 from Veykril/veykril/push-lvwxpnowqrxk
internal: Invert token iteration order in macro mapping
2 parents 66a81d3 + 6c70806 commit 0d441c3

File tree

9 files changed

+59
-50
lines changed

9 files changed

+59
-50
lines changed

src/tools/rust-analyzer/crates/hir/src/semantics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,10 @@ impl<'db> SemanticsImpl<'db> {
945945
};
946946

947947
while let Some((expansion, ref mut tokens)) = stack.pop() {
948+
// Reverse the tokens so we prefer first tokens (to accommodate for popping from the
949+
// back)
950+
// alternatively we could pop from the front but that would shift the content on every pop
951+
tokens.reverse();
948952
while let Some((token, ctx)) = tokens.pop() {
949953
let was_not_remapped = (|| {
950954
// First expand into attribute invocations

src/tools/rust-analyzer/crates/ide-db/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,35 @@ impl SnippetCap {
293293
}
294294
}
295295
}
296+
297+
pub struct Ranker<'a> {
298+
pub kind: parser::SyntaxKind,
299+
pub text: &'a str,
300+
pub ident_kind: bool,
301+
}
302+
303+
impl<'a> Ranker<'a> {
304+
pub const MAX_RANK: usize = 0b1110;
305+
306+
pub fn from_token(token: &'a syntax::SyntaxToken) -> Self {
307+
let kind = token.kind();
308+
Ranker { kind, text: token.text(), ident_kind: kind.is_any_identifier() }
309+
}
310+
311+
/// A utility function that ranks a token again a given kind and text, returning a number that
312+
/// represents how close the token is to the given kind and text.
313+
pub fn rank_token(&self, tok: &syntax::SyntaxToken) -> usize {
314+
let tok_kind = tok.kind();
315+
316+
let exact_same_kind = tok_kind == self.kind;
317+
let both_idents = exact_same_kind || (tok_kind.is_any_identifier() && self.ident_kind);
318+
let same_text = tok.text() == self.text;
319+
// anything that mapped into a token tree has likely no semantic information
320+
let no_tt_parent =
321+
tok.parent().map_or(false, |it| it.kind() != parser::SyntaxKind::TOKEN_TREE);
322+
(both_idents as usize)
323+
| ((exact_same_kind as usize) << 1)
324+
| ((same_text as usize) << 2)
325+
| ((no_tt_parent as usize) << 3)
326+
}
327+
}

src/tools/rust-analyzer/crates/ide/src/call_hierarchy.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ fn caller$0() {
510510
expect![[]],
511511
);
512512
}
513+
513514
#[test]
514515
fn test_call_hierarchy_in_macros_incoming_different_files() {
515516
check_hierarchy(
@@ -591,9 +592,9 @@ macro_rules! call {
591592
"#,
592593
expect!["callee Function FileId(0) 22..37 30..36"],
593594
expect![[r#"
594-
callee Function FileId(0) 38..52 44..50 : FileId(0):44..50
595595
caller Function FileId(0) 38..52 : FileId(0):44..50
596-
caller Function FileId(1) 130..136 130..136 : FileId(0):44..50"#]],
596+
caller Function FileId(1) 130..136 130..136 : FileId(0):44..50
597+
callee Function FileId(0) 38..52 44..50 : FileId(0):44..50"#]],
597598
expect![[]],
598599
);
599600
}

src/tools/rust-analyzer/crates/ide/src/hover.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ide_db::{
1111
defs::{Definition, IdentClass, NameRefClass, OperatorClass},
1212
famous_defs::FamousDefs,
1313
helpers::pick_best_token,
14-
FileRange, FxIndexSet, RootDatabase,
14+
FileRange, FxIndexSet, Ranker, RootDatabase,
1515
};
1616
use itertools::{multizip, Itertools};
1717
use span::Edition;
@@ -182,27 +182,13 @@ fn hover_offset(
182182
// equivalency is more important
183183
let mut descended = sema.descend_into_macros(original_token.clone());
184184

185-
let kind = original_token.kind();
186-
let text = original_token.text();
187-
let ident_kind = kind.is_any_identifier();
188-
189-
descended.sort_by_cached_key(|tok| {
190-
let tok_kind = tok.kind();
191-
192-
let exact_same_kind = tok_kind == kind;
193-
let both_idents = exact_same_kind || (tok_kind.is_any_identifier() && ident_kind);
194-
let same_text = tok.text() == text;
195-
// anything that mapped into a token tree has likely no semantic information
196-
let no_tt_parent = tok.parent().map_or(false, |it| it.kind() != TOKEN_TREE);
197-
!((both_idents as usize)
198-
| ((exact_same_kind as usize) << 1)
199-
| ((same_text as usize) << 2)
200-
| ((no_tt_parent as usize) << 3))
201-
});
185+
let ranker = Ranker::from_token(&original_token);
186+
187+
descended.sort_by_cached_key(|tok| !ranker.rank_token(tok));
202188

203189
let mut res = vec![];
204190
for token in descended {
205-
let is_same_kind = token.kind() == kind;
191+
let is_same_kind = token.kind() == ranker.kind;
206192
let lint_hover = (|| {
207193
// FIXME: Definition should include known lints and the like instead of having this special case here
208194
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;

src/tools/rust-analyzer/crates/ide/src/hover/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ m!(ab$0c);
289289
*abc*
290290
291291
```rust
292-
test::module
292+
test
293293
```
294294
295295
```rust
@@ -298,11 +298,11 @@ m!(ab$0c);
298298
299299
---
300300
301-
Inner
301+
Outer
302302
---
303303
304304
```rust
305-
test
305+
test::module
306306
```
307307
308308
```rust
@@ -311,7 +311,7 @@ m!(ab$0c);
311311
312312
---
313313
314-
Outer
314+
Inner
315315
"#]],
316316
);
317317
}

src/tools/rust-analyzer/crates/ide/src/references.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,14 +1701,14 @@ fn f() {
17011701
}
17021702
"#,
17031703
expect![[r#"
1704-
func Function FileId(0) 137..146 140..144
1704+
func Function FileId(0) 137..146 140..144 module
17051705
1706-
FileId(0) 161..165
1706+
FileId(0) 181..185
17071707
17081708
1709-
func Function FileId(0) 137..146 140..144 module
1709+
func Function FileId(0) 137..146 140..144
17101710
1711-
FileId(0) 181..185
1711+
FileId(0) 161..165
17121712
"#]],
17131713
)
17141714
}

src/tools/rust-analyzer/crates/ide/src/syntax_highlighting.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod tests;
1616
use std::ops::ControlFlow;
1717

1818
use hir::{InRealFile, Name, Semantics};
19-
use ide_db::{FxHashMap, RootDatabase, SymbolKind};
19+
use ide_db::{FxHashMap, Ranker, RootDatabase, SymbolKind};
2020
use span::EditionedFileId;
2121
use syntax::{
2222
ast::{self, IsString},
@@ -397,13 +397,12 @@ fn traverse(
397397
Some(AttrOrDerive::Derive(_)) => inside_attribute,
398398
None => false,
399399
};
400+
400401
let descended_element = if in_macro {
401402
// Attempt to descend tokens into macro-calls.
402403
let res = match element {
403404
NodeOrToken::Token(token) if token.kind() != COMMENT => {
404-
let kind = token.kind();
405-
let text = token.text();
406-
let ident_kind = kind.is_any_identifier();
405+
let ranker = Ranker::from_token(&token);
407406

408407
let mut t = None;
409408
let mut r = 0;
@@ -412,21 +411,9 @@ fn traverse(
412411
|tok, _ctx| {
413412
// FIXME: Consider checking ctx transparency for being opaque?
414413
let tok = tok.value;
415-
let tok_kind = tok.kind();
416-
417-
let exact_same_kind = tok_kind == kind;
418-
let both_idents =
419-
exact_same_kind || (tok_kind.is_any_identifier() && ident_kind);
420-
let same_text = tok.text() == text;
421-
// anything that mapped into a token tree has likely no semantic information
422-
let no_tt_parent =
423-
tok.parent().map_or(false, |it| it.kind() != TOKEN_TREE);
424-
let my_rank = (both_idents as usize)
425-
| ((exact_same_kind as usize) << 1)
426-
| ((same_text as usize) << 2)
427-
| ((no_tt_parent as usize) << 3);
428-
429-
if my_rank > 0b1110 {
414+
let my_rank = ranker.rank_token(&tok);
415+
416+
if my_rank >= Ranker::MAX_RANK {
430417
// a rank of 0b1110 means that we have found a maximally interesting
431418
// token so stop early.
432419
t = Some(tok);

src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_issue_18089.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
<span class="brace">}</span>
5151

5252
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="module attribute crate_root library">proc_macros</span><span class="operator attribute">::</span><span class="attribute attribute library">issue_18089</span><span class="attribute_bracket attribute">]</span>
53-
<span class="keyword">fn</span> <span class="function declaration">template</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span></code></pre>
53+
<span class="keyword">fn</span> <span class="macro declaration">template</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span></code></pre>

src/tools/rust-analyzer/rust-bors.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)