Skip to content

Commit e4929bd

Browse files
committed
Do not replace items annotated with builtin attrs with the attr input
1 parent 320bb72 commit e4929bd

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

crates/hir_def/src/builtin_attr.rs

-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ macro_rules! rustc_attr {
3434
};
3535
}
3636

37-
/// Built-in macro-like attributes.
38-
pub const EXTRA_ATTRIBUTES: &[BuiltinAttribute] = &["test", "bench"];
39-
4037
/// "Inert" built-in attributes that have a special meaning to rustc or rustdoc.
4138
#[rustfmt::skip]
4239
pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[

crates/hir_def/src/nameres/collector.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -1712,27 +1712,22 @@ impl ModCollector<'_, '_> {
17121712
if path.kind == PathKind::Plain {
17131713
if let Some(tool_module) = path.segments().first() {
17141714
let tool_module = tool_module.to_string();
1715-
if builtin_attr::TOOL_MODULES
1715+
let is_tool = builtin_attr::TOOL_MODULES
17161716
.iter()
17171717
.copied()
1718-
.chain(self.def_collector.registered_tools.iter().map(|s| &**s))
1719-
.any(|m| tool_module == *m)
1720-
{
1721-
return true;
1722-
}
1718+
.chain(self.def_collector.registered_tools.iter().map(AsRef::as_ref))
1719+
.any(|m| tool_module == *m);
1720+
return is_tool;
17231721
}
17241722

17251723
if let Some(name) = path.as_ident() {
17261724
let name = name.to_string();
1727-
if builtin_attr::INERT_ATTRIBUTES
1725+
let is_inert = builtin_attr::INERT_ATTRIBUTES
17281726
.iter()
1729-
.chain(builtin_attr::EXTRA_ATTRIBUTES)
17301727
.copied()
1731-
.chain(self.def_collector.registered_attrs.iter().map(|s| &**s))
1732-
.any(|attr| name == *attr)
1733-
{
1734-
return true;
1735-
}
1728+
.chain(self.def_collector.registered_attrs.iter().map(AsRef::as_ref))
1729+
.any(|attr| name == *attr);
1730+
return is_inert;
17361731
}
17371732
}
17381733

crates/hir_expand/src/builtin_attr.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ macro_rules! register_builtin {
1717
db: &dyn AstDatabase,
1818
id: MacroCallId,
1919
tt: &tt::Subtree,
20+
item: &tt::Subtree,
2021
) -> Result<tt::Subtree, mbe::ExpandError> {
2122
let expander = match *self {
2223
$( BuiltinAttrExpander::$variant => $expand, )*
2324
};
24-
expander(db, id, tt)
25+
expander(db, id, tt, item)
2526
}
2627

2728
fn find_by_name(name: &name::Name) -> Option<Self> {
@@ -61,7 +62,8 @@ pub fn find_builtin_attr(
6162
fn dummy_attr_expand(
6263
_db: &dyn AstDatabase,
6364
_id: MacroCallId,
64-
tt: &tt::Subtree,
65+
_tt: &tt::Subtree,
66+
item: &tt::Subtree,
6567
) -> Result<tt::Subtree, mbe::ExpandError> {
66-
Ok(tt.clone())
68+
Ok(item.clone())
6769
}

crates/hir_expand/src/db.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,18 @@ impl TokenExpander {
5454
TokenExpander::MacroDef { mac, .. } => mac.expand(tt),
5555
TokenExpander::Builtin(it) => it.expand(db, id, tt),
5656
// FIXME switch these to ExpandResult as well
57-
TokenExpander::BuiltinAttr(it) => it.expand(db, id, tt).into(),
57+
TokenExpander::BuiltinAttr(it) => {
58+
let macro_arg = match db.macro_arg(id) {
59+
Some(it) => it,
60+
None => {
61+
return mbe::ExpandResult::only_err(
62+
mbe::ExpandError::Other("No item argument for attribute".to_string())
63+
.into(),
64+
);
65+
}
66+
};
67+
it.expand(db, id, tt, &macro_arg.0).into()
68+
}
5869
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(),
5970
TokenExpander::ProcMacro(_) => {
6071
// We store the result in salsa db to prevent non-deterministic behavior in

0 commit comments

Comments
 (0)