]> git.lizzy.rs Git - rust.git/commitdiff
Merge #9195
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Wed, 9 Jun 2021 21:45:35 +0000 (21:45 +0000)
committerGitHub <noreply@github.com>
Wed, 9 Jun 2021 21:45:35 +0000 (21:45 +0000)
9195: minor: Simplify r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
crates/hir/src/lib.rs
crates/hir_def/src/nameres/collector.rs
crates/hir_expand/src/builtin_attr.rs [new file with mode: 0644]
crates/hir_expand/src/db.rs
crates/hir_expand/src/eager.rs
crates/hir_expand/src/hygiene.rs
crates/hir_expand/src/lib.rs
crates/hir_expand/src/name.rs
crates/ide_completion/src/completions/qualified_path.rs
crates/ide_completion/src/completions/unqualified_path.rs

index dba46df04f9b756cbf8cdf4452cf3f3df51245be..debc3ee624c069be28ca25bab988ff316435ab86 100644 (file)
@@ -1344,6 +1344,7 @@ pub fn kind(&self) -> MacroKind {
             MacroDefKind::Declarative(_) => MacroKind::Declarative,
             MacroDefKind::BuiltIn(_, _) | MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
             MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
+            MacroDefKind::BuiltInAttr(_, _) => MacroKind::Attr,
             MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::CustomDerive, _) => {
                 MacroKind::Derive
             }
index d019ba3a9626c45afbe7f46e8c5d32ed727440d5..93f30f23dd8ac10909968a623ace406df5dfb5c2 100644 (file)
@@ -9,6 +9,7 @@
 use cfg::{CfgExpr, CfgOptions};
 use hir_expand::{
     ast_id_map::FileAstId,
+    builtin_attr::find_builtin_attr,
     builtin_derive::find_builtin_derive,
     builtin_macro::find_builtin_macro,
     name::{name, AsName, Name},
@@ -1836,7 +1837,8 @@ fn collect_macro_def(&mut self, id: FileItemTreeId<MacroDef>) {
         let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
         if attrs.by_key("rustc_builtin_macro").exists() {
             let macro_id = find_builtin_macro(&mac.name, krate, ast_id)
-                .or_else(|| find_builtin_derive(&mac.name, krate, ast_id));
+                .or_else(|| find_builtin_derive(&mac.name, krate, ast_id))
+                .or_else(|| find_builtin_attr(&mac.name, krate, ast_id));
 
             match macro_id {
                 Some(macro_id) => {
diff --git a/crates/hir_expand/src/builtin_attr.rs b/crates/hir_expand/src/builtin_attr.rs
new file mode 100644 (file)
index 0000000..c843200
--- /dev/null
@@ -0,0 +1,67 @@
+//! Builtin derives.
+
+use syntax::ast;
+
+use crate::{db::AstDatabase, name, AstId, CrateId, MacroCallId, MacroDefId, MacroDefKind};
+
+macro_rules! register_builtin {
+    ( $(($name:ident, $variant:ident) => $expand:ident),* ) => {
+        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+        pub enum BuiltinAttrExpander {
+            $($variant),*
+        }
+
+        impl BuiltinAttrExpander {
+            pub fn expand(
+                &self,
+                db: &dyn AstDatabase,
+                id: MacroCallId,
+                tt: &tt::Subtree,
+            ) -> Result<tt::Subtree, mbe::ExpandError> {
+                let expander = match *self {
+                    $( BuiltinAttrExpander::$variant => $expand, )*
+                };
+                expander(db, id, tt)
+            }
+
+            fn find_by_name(name: &name::Name) -> Option<Self> {
+                match name {
+                    $( id if id == &name::name![$name] => Some(BuiltinAttrExpander::$variant), )*
+                     _ => None,
+                }
+            }
+        }
+
+    };
+}
+
+register_builtin! {
+    (bench, Bench) => dummy_attr_expand,
+    (cfg_accessible, CfgAccessible) => dummy_attr_expand,
+    (cfg_eval, CfgEval) => dummy_attr_expand,
+    (derive, Derive) => dummy_attr_expand,
+    (global_allocator, GlobalAllocator) => dummy_attr_expand,
+    (test, Test) => dummy_attr_expand,
+    (test_case, TestCase) => dummy_attr_expand
+}
+
+pub fn find_builtin_attr(
+    ident: &name::Name,
+    krate: CrateId,
+    ast_id: AstId<ast::Macro>,
+) -> Option<MacroDefId> {
+    let expander = BuiltinAttrExpander::find_by_name(ident)?;
+    Some(MacroDefId {
+        krate,
+        kind: MacroDefKind::BuiltInAttr(expander, ast_id),
+        local_inner: false,
+    })
+}
+
+fn dummy_attr_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    Ok(tt.clone())
+}
index 18a05579f4527b5c569f7f0b8e959fab8eb79b15..45e6e446afe970ad41a4126093c6cea0cebf1ceb 100644 (file)
@@ -12,9 +12,9 @@
 };
 
 use crate::{
-    ast_id_map::AstIdMap, hygiene::HygieneFrame, input::process_macro_input, BuiltinDeriveExpander,
-    BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallKind, MacroCallLoc,
-    MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander,
+    ast_id_map::AstIdMap, hygiene::HygieneFrame, input::process_macro_input, BuiltinAttrExpander,
+    BuiltinDeriveExpander, BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, MacroCallId,
+    MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander,
 };
 
 /// Total limit on the number of tokens produced by any macro invocation.
@@ -31,6 +31,8 @@ pub enum TokenExpander {
     MacroDef { mac: mbe::MacroDef, def_site_token_map: mbe::TokenMap },
     /// Stuff like `line!` and `file!`.
     Builtin(BuiltinFnLikeExpander),
+    /// `global_allocator` and such.
+    BuiltinAttr(BuiltinAttrExpander),
     /// `derive(Copy)` and such.
     BuiltinDerive(BuiltinDeriveExpander),
     /// The thing we love the most here in rust-analyzer -- procedural macros.
@@ -49,6 +51,7 @@ fn expand(
             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::BuiltinDerive(it) => it.expand(db, id, tt).into(),
             TokenExpander::ProcMacro(_) => {
                 // We store the result in salsa db to prevent non-deterministic behavior in
@@ -64,6 +67,7 @@ pub(crate) fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {
             TokenExpander::MacroRules { mac, .. } => mac.map_id_down(id),
             TokenExpander::MacroDef { mac, .. } => mac.map_id_down(id),
             TokenExpander::Builtin(..)
+            | TokenExpander::BuiltinAttr(..)
             | TokenExpander::BuiltinDerive(..)
             | TokenExpander::ProcMacro(..) => id,
         }
@@ -74,6 +78,7 @@ pub(crate) fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, mbe::Origin) {
             TokenExpander::MacroRules { mac, .. } => mac.map_id_up(id),
             TokenExpander::MacroDef { mac, .. } => mac.map_id_up(id),
             TokenExpander::Builtin(..)
+            | TokenExpander::BuiltinAttr(..)
             | TokenExpander::BuiltinDerive(..)
             | TokenExpander::ProcMacro(..) => (id, mbe::Origin::Call),
         }
@@ -299,6 +304,9 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<TokenExpander>>
             }
         },
         MacroDefKind::BuiltIn(expander, _) => Some(Arc::new(TokenExpander::Builtin(expander))),
+        MacroDefKind::BuiltInAttr(expander, _) => {
+            Some(Arc::new(TokenExpander::BuiltinAttr(expander)))
+        }
         MacroDefKind::BuiltInDerive(expander, _) => {
             Some(Arc::new(TokenExpander::BuiltinDerive(expander)))
         }
index 14af628a1fa729e44b4f577591cffdc82e46572d..9093255f4c3b17c448999f6b6447db8d3891d08b 100644 (file)
@@ -224,6 +224,7 @@ fn eager_macro_recur(
             }
             MacroDefKind::Declarative(_)
             | MacroDefKind::BuiltIn(..)
+            | MacroDefKind::BuiltInAttr(..)
             | MacroDefKind::BuiltInDerive(..)
             | MacroDefKind::ProcMacro(..) => {
                 let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate);
index d98913907ada12f5203674ce3de3b487137fb35e..05c6c3fb1644bceffa63872b4cb22c11c40d7d63 100644 (file)
@@ -192,6 +192,7 @@ pub(crate) fn new(db: &dyn AstDatabase, file_id: HirFileId) -> HygieneFrame {
                         (info, Some(loc.def.krate), loc.def.local_inner)
                     }
                     MacroDefKind::BuiltIn(..) => (info, Some(loc.def.krate), false),
+                    MacroDefKind::BuiltInAttr(..) => (info, None, false),
                     MacroDefKind::BuiltInDerive(..) => (info, None, false),
                     MacroDefKind::BuiltInEager(..) => (info, None, false),
                     MacroDefKind::ProcMacro(..) => (info, None, false),
index 618f26b9546bf8af797f3d49a621fd664dd241df..623791b5829e0142618a26687c800ce6733898d7 100644 (file)
@@ -8,6 +8,7 @@
 pub mod ast_id_map;
 pub mod name;
 pub mod hygiene;
+pub mod builtin_attr;
 pub mod builtin_derive;
 pub mod builtin_macro;
 pub mod proc_macro;
@@ -32,6 +33,7 @@
 };
 
 use crate::ast_id_map::FileAstId;
+use crate::builtin_attr::BuiltinAttrExpander;
 use crate::builtin_derive::BuiltinDeriveExpander;
 use crate::builtin_macro::{BuiltinFnLikeExpander, EagerExpander};
 use crate::proc_macro::ProcMacroExpander;
@@ -206,6 +208,7 @@ pub fn ast_id(&self) -> Either<AstId<ast::Macro>, AstId<ast::Fn>> {
         let id = match &self.kind {
             MacroDefKind::Declarative(id) => id,
             MacroDefKind::BuiltIn(_, id) => id,
+            MacroDefKind::BuiltInAttr(_, id) => id,
             MacroDefKind::BuiltInDerive(_, id) => id,
             MacroDefKind::BuiltInEager(_, id) => id,
             MacroDefKind::ProcMacro(.., id) => return Either::Right(*id),
@@ -223,6 +226,7 @@ pub enum MacroDefKind {
     Declarative(AstId<ast::Macro>),
     BuiltIn(BuiltinFnLikeExpander, AstId<ast::Macro>),
     // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
+    BuiltInAttr(BuiltinAttrExpander, AstId<ast::Macro>),
     BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>),
     BuiltInEager(EagerExpander, AstId<ast::Macro>),
     ProcMacro(ProcMacroExpander, ProcMacroKind, AstId<ast::Fn>),
index 00b8adc1ebda4c5557a2fc6d628bfe953e661e14..376fe130f1efbedba0a9b8f0605c7fb6d396d79d 100644 (file)
@@ -160,7 +160,6 @@ macro_rules! known_names {
         str,
         // Special names
         macro_rules,
-        derive,
         doc,
         cfg,
         cfg_attr,
@@ -240,6 +239,14 @@ macro_rules! known_names {
         PartialOrd,
         Eq,
         PartialEq,
+        // Builtin attributes
+        bench,
+        cfg_accessible,
+        cfg_eval,
+        derive,
+        global_allocator,
+        test,
+        test_case,
         // Safe intrinsics
         abort,
         size_of,
index 0b0a81410bee5473c7dd3bc87366d0ee2af735f4..58d4dd9ee81805ca19aba75e832ec8e4df605cd4 100644 (file)
@@ -656,6 +656,32 @@ fn main()  fn()
         );
     }
 
+    #[test]
+    fn does_not_complete_non_fn_macros() {
+        check(
+            r#"
+mod m {
+    #[rustc_builtin_macro]
+    pub macro Clone {}
+}
+
+fn f() {m::$0}
+"#,
+            expect![[r#""#]],
+        );
+        check(
+            r#"
+mod m {
+    #[rustc_builtin_macro]
+    pub macro bench {}
+}
+
+fn f() {m::$0}
+"#,
+            expect![[r#""#]],
+        );
+    }
+
     #[test]
     fn completes_in_assoc_item_list() {
         check(
index 1f6c4069f0f71a9630ed0dc1843022461501422a..b1e6b2b775ec5245d98856a8cb7b29c97bc90c3b 100644 (file)
@@ -481,14 +481,14 @@ impl S {
         );
         check(
             r#"
-mod m {
-    #[rustc_builtin_macro]
-    pub macro Clone {}
-}
+#[rustc_builtin_macro]
+pub macro bench {}
 
-fn f() {m::$0}
+fn f() {$0}
 "#,
-            expect![[r#""#]],
+            expect![[r#"
+                fn f() fn()
+            "#]],
         );
     }