Skip to content

Commit 2095af2

Browse files
authored
Merge pull request #19942 from ChayimFriedman2/faux
fix: Fix completion with some attribute macros
2 parents 18358d6 + 7b64b40 commit 2095af2

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

crates/hir-expand/src/db.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
AstId, BuiltinAttrExpander, BuiltinDeriveExpander, BuiltinFnLikeExpander, EagerCallInfo,
1414
EagerExpander, EditionedFileId, ExpandError, ExpandResult, ExpandTo, HirFileId, MacroCallId,
1515
MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind,
16-
attrs::{AttrId, collect_attrs},
16+
attrs::{AttrId, AttrInput, RawAttrs, collect_attrs},
1717
builtin::pseudo_derive_attr_expansion,
1818
cfg_process,
1919
declarative::DeclarativeMacroExpander,
@@ -241,30 +241,36 @@ pub fn expand_speculative(
241241

242242
let attr_arg = match loc.kind {
243243
MacroCallKind::Attr { invoc_attr_index, .. } => {
244-
let attr = if loc.def.is_attribute_derive() {
244+
if loc.def.is_attribute_derive() {
245245
// for pseudo-derive expansion we actually pass the attribute itself only
246-
ast::Attr::cast(speculative_args.clone())
246+
ast::Attr::cast(speculative_args.clone()).and_then(|attr| attr.token_tree()).map(
247+
|token_tree| {
248+
let mut tree = syntax_node_to_token_tree(
249+
token_tree.syntax(),
250+
span_map,
251+
span,
252+
DocCommentDesugarMode::ProcMacro,
253+
);
254+
*tree.top_subtree_delimiter_mut() = tt::Delimiter::invisible_spanned(span);
255+
tree
256+
},
257+
)
247258
} else {
248259
// Attributes may have an input token tree, build the subtree and map for this as well
249260
// then try finding a token id for our token if it is inside this input subtree.
250261
let item = ast::Item::cast(speculative_args.clone())?;
251-
collect_attrs(&item)
252-
.nth(invoc_attr_index.ast_index())
253-
.and_then(|x| Either::left(x.1))
254-
}?;
255-
match attr.token_tree() {
256-
Some(token_tree) => {
257-
let mut tree = syntax_node_to_token_tree(
258-
token_tree.syntax(),
259-
span_map,
260-
span,
261-
DocCommentDesugarMode::ProcMacro,
262-
);
263-
*tree.top_subtree_delimiter_mut() = tt::Delimiter::invisible_spanned(span);
264-
265-
Some(tree)
266-
}
267-
_ => None,
262+
let attrs = RawAttrs::new_expanded(db, &item, span_map, loc.krate.cfg_options(db));
263+
attrs.iter().find(|attr| attr.id == invoc_attr_index).and_then(|attr| {
264+
match attr.input.as_deref()? {
265+
AttrInput::TokenTree(tt) => {
266+
let mut attr_arg = tt.clone();
267+
attr_arg.top_subtree_delimiter_mut().kind =
268+
tt::DelimiterKind::Invisible;
269+
Some(attr_arg)
270+
}
271+
AttrInput::Literal(_) => None,
272+
}
273+
})
268274
}
269275
}
270276
_ => None,

crates/hir/src/semantics.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use hir_expand::{
2525
builtin::{BuiltinFnLikeExpander, EagerExpander},
2626
db::ExpandDatabase,
2727
files::{FileRangeWrapper, HirFileRange, InRealFile},
28-
inert_attr_macro::find_builtin_attr_idx,
2928
mod_path::{ModPath, PathKind},
3029
name::AsName,
3130
};
@@ -969,13 +968,6 @@ impl<'db> SemanticsImpl<'db> {
969968
let Some(item) = ast::Item::cast(ancestor) else {
970969
return false;
971970
};
972-
// Optimization to skip the semantic check.
973-
if item.attrs().all(|attr| {
974-
attr.simple_name()
975-
.is_some_and(|attr| find_builtin_attr_idx(&Symbol::intern(&attr)).is_some())
976-
}) {
977-
return false;
978-
}
979971
self.with_ctx(|ctx| {
980972
if ctx.item_to_macro_call(token.with_value(&item)).is_some() {
981973
return true;

0 commit comments

Comments
 (0)