Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not offer completions within macro strings #18832

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/ide-completion/src/context/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,15 @@ fn analyze(
derive_ctx,
} = expansion_result;

if original_token.kind() != self_token.kind()
// FIXME: This check can be removed once we use speculative database forking for completions
&& !(original_token.kind().is_punct() || original_token.kind().is_trivia())
vishruth-thimmaiah marked this conversation as resolved.
Show resolved Hide resolved
&& !(SyntaxKind::is_any_identifier(original_token.kind())
&& SyntaxKind::is_any_identifier(self_token.kind()))
{
return None;
}

// Overwrite the path kind for derives
if let Some((original_file, file_with_fake_ident, offset, origin_attr)) = derive_ctx {
if let Some(ast::NameLike::NameRef(name_ref)) =
Expand Down
22 changes: 22 additions & 0 deletions crates/ide-completion/src/tests/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,28 @@ struct Foo;
);
}

#[test]
fn issue_17479() {
check(
r#"
//- proc_macros: issue_17479
fn main() {
proc_macros::issue_17479!("te$0");
}
"#,
expect![""],
);
check(
ChayimFriedman2 marked this conversation as resolved.
Show resolved Hide resolved
r#"
//- proc_macros: issue_17479
fn main() {
proc_macros::issue_17479!("$0");
}
"#,
expect![""],
)
}

mod cfg {
use super::*;

Expand Down
42 changes: 41 additions & 1 deletion crates/test-fixture/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl ChangeFixture {
}
}

fn default_test_proc_macros() -> [(String, ProcMacro); 7] {
fn default_test_proc_macros() -> [(String, ProcMacro); 8] {
[
(
r#"
Expand Down Expand Up @@ -483,6 +483,21 @@ pub fn issue_18840(_attr: TokenStream, _item: TokenStream) -> TokenStream {
disabled: false,
},
),
(
r#"
#[proc_macro]
pub fn issue_17479(input: TokenStream) -> TokenStream {
input
}
"#
.into(),
ProcMacro {
name: Symbol::intern("issue_17479"),
kind: ProcMacroKind::Bang,
expander: sync::Arc::new(Issue17479ProcMacroExpander),
disabled: false,
},
),
]
}

Expand Down Expand Up @@ -761,3 +776,28 @@ impl ProcMacroExpander for ShortenProcMacroExpander {
}
}
}

// Reads ident type within string quotes, for issue #17479.
#[derive(Debug)]
struct Issue17479ProcMacroExpander;
impl ProcMacroExpander for Issue17479ProcMacroExpander {
fn expand(
&self,
subtree: &TopSubtree,
_: Option<&TopSubtree>,
_: &Env,
_: Span,
_: Span,
_: Span,
_: Option<String>,
) -> Result<TopSubtree, ProcMacroExpansionError> {
let TokenTree::Leaf(Leaf::Literal(lit)) = &subtree.0[1] else {
return Err(ProcMacroExpansionError::Panic("incorrect Input".into()));
};
let symbol = &lit.symbol;
let span = lit.span;
Ok(quote! { span =>
#symbol()
ChayimFriedman2 marked this conversation as resolved.
Show resolved Hide resolved
})
}
}
Loading