Skip to content

fix: Fix completion with some attribute macros #19942

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

Merged
merged 2 commits into from
Jun 12, 2025
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
46 changes: 26 additions & 20 deletions crates/hir-expand/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
AstId, BuiltinAttrExpander, BuiltinDeriveExpander, BuiltinFnLikeExpander, EagerCallInfo,
EagerExpander, EditionedFileId, ExpandError, ExpandResult, ExpandTo, HirFileId, MacroCallId,
MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind,
attrs::{AttrId, collect_attrs},
attrs::{AttrId, AttrInput, RawAttrs, collect_attrs},
builtin::pseudo_derive_attr_expansion,
cfg_process,
declarative::DeclarativeMacroExpander,
Expand Down Expand Up @@ -241,30 +241,36 @@ pub fn expand_speculative(

let attr_arg = match loc.kind {
MacroCallKind::Attr { invoc_attr_index, .. } => {
let attr = if loc.def.is_attribute_derive() {
if loc.def.is_attribute_derive() {
// for pseudo-derive expansion we actually pass the attribute itself only
ast::Attr::cast(speculative_args.clone())
ast::Attr::cast(speculative_args.clone()).and_then(|attr| attr.token_tree()).map(
|token_tree| {
let mut tree = syntax_node_to_token_tree(
token_tree.syntax(),
span_map,
span,
DocCommentDesugarMode::ProcMacro,
);
*tree.top_subtree_delimiter_mut() = tt::Delimiter::invisible_spanned(span);
tree
},
)
} else {
// Attributes may have an input token tree, build the subtree and map for this as well
// then try finding a token id for our token if it is inside this input subtree.
let item = ast::Item::cast(speculative_args.clone())?;
collect_attrs(&item)
.nth(invoc_attr_index.ast_index())
.and_then(|x| Either::left(x.1))
}?;
match attr.token_tree() {
Some(token_tree) => {
let mut tree = syntax_node_to_token_tree(
token_tree.syntax(),
span_map,
span,
DocCommentDesugarMode::ProcMacro,
);
*tree.top_subtree_delimiter_mut() = tt::Delimiter::invisible_spanned(span);

Some(tree)
}
_ => None,
let attrs = RawAttrs::new_expanded(db, &item, span_map, loc.krate.cfg_options(db));
attrs.iter().find(|attr| attr.id == invoc_attr_index).and_then(|attr| {
match attr.input.as_deref()? {
AttrInput::TokenTree(tt) => {
let mut attr_arg = tt.clone();
attr_arg.top_subtree_delimiter_mut().kind =
tt::DelimiterKind::Invisible;
Some(attr_arg)
}
AttrInput::Literal(_) => None,
}
})
}
}
_ => None,
Expand Down
8 changes: 0 additions & 8 deletions crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use hir_expand::{
builtin::{BuiltinFnLikeExpander, EagerExpander},
db::ExpandDatabase,
files::{FileRangeWrapper, HirFileRange, InRealFile},
inert_attr_macro::find_builtin_attr_idx,
mod_path::{ModPath, PathKind},
name::AsName,
};
Expand Down Expand Up @@ -953,13 +952,6 @@ impl<'db> SemanticsImpl<'db> {
let Some(item) = ast::Item::cast(ancestor) else {
return false;
};
// Optimization to skip the semantic check.
if item.attrs().all(|attr| {
attr.simple_name()
.is_some_and(|attr| find_builtin_attr_idx(&Symbol::intern(&attr)).is_some())
}) {
return false;
}
self.with_ctx(|ctx| {
if ctx.item_to_macro_call(token.with_value(&item)).is_some() {
return true;
Expand Down