Unnamed repository; edit this file 'description' to name the repository.
Do not replace items annotated with builtin attrs with the attr input
Lukas Wirth 2021-08-20
parent 8dd3a71 · commit 351cec0
-rw-r--r--crates/hir_def/src/builtin_attr.rs3
-rw-r--r--crates/hir_def/src/nameres/collector.rs19
-rw-r--r--crates/hir_expand/src/builtin_attr.rs8
-rw-r--r--crates/hir_expand/src/db.rs13
4 files changed, 25 insertions, 18 deletions
diff --git a/crates/hir_def/src/builtin_attr.rs b/crates/hir_def/src/builtin_attr.rs
index 39c7f84f7e..6cd185ceeb 100644
--- a/crates/hir_def/src/builtin_attr.rs
+++ b/crates/hir_def/src/builtin_attr.rs
@@ -34,9 +34,6 @@ macro_rules! rustc_attr {
};
}
-/// Built-in macro-like attributes.
-pub const EXTRA_ATTRIBUTES: &[BuiltinAttribute] = &["test", "bench"];
-
/// "Inert" built-in attributes that have a special meaning to rustc or rustdoc.
#[rustfmt::skip]
pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs
index cca51b8167..f8b3c3949f 100644
--- a/crates/hir_def/src/nameres/collector.rs
+++ b/crates/hir_def/src/nameres/collector.rs
@@ -1712,27 +1712,24 @@ impl ModCollector<'_, '_> {
if path.kind == PathKind::Plain {
if let Some(tool_module) = path.segments().first() {
let tool_module = tool_module.to_string();
- if builtin_attr::TOOL_MODULES
+ let is_tool = builtin_attr::TOOL_MODULES
.iter()
.copied()
- .chain(self.def_collector.registered_tools.iter().map(|s| &**s))
- .any(|m| tool_module == *m)
- {
+ .chain(self.def_collector.registered_tools.iter().map(AsRef::as_ref))
+ .any(|m| tool_module == *m);
+ if is_tool {
return true;
}
}
if let Some(name) = path.as_ident() {
let name = name.to_string();
- if builtin_attr::INERT_ATTRIBUTES
+ let is_inert = builtin_attr::INERT_ATTRIBUTES
.iter()
- .chain(builtin_attr::EXTRA_ATTRIBUTES)
.copied()
- .chain(self.def_collector.registered_attrs.iter().map(|s| &**s))
- .any(|attr| name == *attr)
- {
- return true;
- }
+ .chain(self.def_collector.registered_attrs.iter().map(AsRef::as_ref))
+ .any(|attr| name == *attr);
+ return is_inert;
}
}
diff --git a/crates/hir_expand/src/builtin_attr.rs b/crates/hir_expand/src/builtin_attr.rs
index d39206f2b0..5764c682be 100644
--- a/crates/hir_expand/src/builtin_attr.rs
+++ b/crates/hir_expand/src/builtin_attr.rs
@@ -17,11 +17,12 @@ macro_rules! register_builtin {
db: &dyn AstDatabase,
id: MacroCallId,
tt: &tt::Subtree,
+ item: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
let expander = match *self {
$( BuiltinAttrExpander::$variant => $expand, )*
};
- expander(db, id, tt)
+ expander(db, id, tt, item)
}
fn find_by_name(name: &name::Name) -> Option<Self> {
@@ -61,7 +62,8 @@ pub fn find_builtin_attr(
fn dummy_attr_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
- tt: &tt::Subtree,
+ _tt: &tt::Subtree,
+ item: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
- Ok(tt.clone())
+ Ok(item.clone())
}
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs
index 795980660d..d71cc22ce4 100644
--- a/crates/hir_expand/src/db.rs
+++ b/crates/hir_expand/src/db.rs
@@ -54,7 +54,18 @@ impl TokenExpander {
TokenExpander::MacroDef { mac, .. } => mac.expand(tt),
TokenExpander::Builtin(it) => it.expand(db, id, tt),
// FIXME switch these to ExpandResult as well
- TokenExpander::BuiltinAttr(it) => it.expand(db, id, tt).into(),
+ TokenExpander::BuiltinAttr(it) => {
+ let macro_arg = match db.macro_arg(id) {
+ Some(it) => it,
+ None => {
+ return mbe::ExpandResult::only_err(
+ mbe::ExpandError::Other("No item argument for attribute".to_string())
+ .into(),
+ );
+ }
+ };
+ it.expand(db, id, tt, &macro_arg.0).into()
+ }
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(),
TokenExpander::ProcMacro(_) => {
// We store the result in salsa db to prevent non-deterministic behavior in