Skip to content

Commit df8b8ec

Browse files
authored
Merge pull request #18723 from ChayimFriedman2/tracing-complete
fix: Fix a case where completion was unable to expand a macro
2 parents 90c345b + 0e266ae commit df8b8ec

File tree

16 files changed

+596
-360
lines changed

16 files changed

+596
-360
lines changed

src/tools/rust-analyzer/crates/hir-expand/src/db.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ fn syntax_context(db: &dyn ExpandDatabase, file: HirFileId) -> SyntaxContextId {
153153
/// This expands the given macro call, but with different arguments. This is
154154
/// used for completion, where we want to see what 'would happen' if we insert a
155155
/// token. The `token_to_map` mapped down into the expansion, with the mapped
156-
/// token returned.
156+
/// token(s) returned with their priority.
157157
pub fn expand_speculative(
158158
db: &dyn ExpandDatabase,
159159
actual_macro_call: MacroCallId,
160160
speculative_args: &SyntaxNode,
161161
token_to_map: SyntaxToken,
162-
) -> Option<(SyntaxNode, SyntaxToken)> {
162+
) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
163163
let loc = db.lookup_intern_macro_call(actual_macro_call);
164164
let (_, _, span) = db.macro_arg_considering_derives(actual_macro_call, &loc.kind);
165165

@@ -303,17 +303,19 @@ pub fn expand_speculative(
303303
token_tree_to_syntax_node(&speculative_expansion.value, expand_to, loc.def.edition);
304304

305305
let syntax_node = node.syntax_node();
306-
let (token, _) = rev_tmap
306+
let token = rev_tmap
307307
.ranges_with_span(span_map.span_for_range(token_to_map.text_range()))
308308
.filter_map(|(range, ctx)| syntax_node.covering_element(range).into_token().zip(Some(ctx)))
309-
.min_by_key(|(t, ctx)| {
309+
.map(|(t, ctx)| {
310310
// prefer tokens of the same kind and text, as well as non opaque marked ones
311311
// Note the inversion of the score here, as we want to prefer the first token in case
312312
// of all tokens having the same score
313-
ctx.is_opaque(db) as u8
313+
let ranking = ctx.is_opaque(db) as u8
314314
+ 2 * (t.kind() != token_to_map.kind()) as u8
315-
+ 4 * ((t.text() != token_to_map.text()) as u8)
316-
})?;
315+
+ 4 * ((t.text() != token_to_map.text()) as u8);
316+
(t, ranking)
317+
})
318+
.collect();
317319
Some((node.syntax_node(), token))
318320
}
319321

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ impl<'db> SemanticsImpl<'db> {
571571
actual_macro_call: &ast::MacroCall,
572572
speculative_args: &ast::TokenTree,
573573
token_to_map: SyntaxToken,
574-
) -> Option<(SyntaxNode, SyntaxToken)> {
574+
) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
575575
let SourceAnalyzer { file_id, resolver, .. } =
576576
self.analyze_no_infer(actual_macro_call.syntax())?;
577577
let macro_call = InFile::new(file_id, actual_macro_call);
@@ -592,7 +592,7 @@ impl<'db> SemanticsImpl<'db> {
592592
macro_file: MacroFileId,
593593
speculative_args: &SyntaxNode,
594594
token_to_map: SyntaxToken,
595-
) -> Option<(SyntaxNode, SyntaxToken)> {
595+
) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
596596
hir_expand::db::expand_speculative(
597597
self.db.upcast(),
598598
macro_file.macro_call_id,
@@ -608,7 +608,7 @@ impl<'db> SemanticsImpl<'db> {
608608
actual_macro_call: &ast::Item,
609609
speculative_args: &ast::Item,
610610
token_to_map: SyntaxToken,
611-
) -> Option<(SyntaxNode, SyntaxToken)> {
611+
) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
612612
let macro_call = self.wrap_node_infile(actual_macro_call.clone());
613613
let macro_call_id = self.with_ctx(|ctx| ctx.item_to_macro_call(macro_call.as_ref()))?;
614614
hir_expand::db::expand_speculative(
@@ -624,7 +624,7 @@ impl<'db> SemanticsImpl<'db> {
624624
actual_macro_call: &ast::Attr,
625625
speculative_args: &ast::Attr,
626626
token_to_map: SyntaxToken,
627-
) -> Option<(SyntaxNode, SyntaxToken)> {
627+
) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
628628
let attr = self.wrap_node_infile(actual_macro_call.clone());
629629
let adt = actual_macro_call.syntax().parent().and_then(ast::Adt::cast)?;
630630
let macro_call_id = self.with_ctx(|ctx| {

src/tools/rust-analyzer/crates/ide-completion/src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ impl<'a> CompletionContext<'a> {
718718
expected: (expected_type, expected_name),
719719
qualifier_ctx,
720720
token,
721-
offset,
721+
original_offset,
722722
} = expand_and_analyze(
723723
&sema,
724724
original_file.syntax().clone(),
@@ -728,7 +728,7 @@ impl<'a> CompletionContext<'a> {
728728
)?;
729729

730730
// adjust for macro input, this still fails if there is no token written yet
731-
let scope = sema.scope_at_offset(&token.parent()?, offset)?;
731+
let scope = sema.scope_at_offset(&token.parent()?, original_offset)?;
732732

733733
let krate = scope.krate();
734734
let module = scope.module();

0 commit comments

Comments
 (0)