about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2025-01-08 10:14:51 +0000
committerGitHub <noreply@github.com>2025-01-08 10:14:51 +0000
commit8982535dc2247ac5fc1970bd498793fa2ec8c4f8 (patch)
treeb093c0c28c9a47345d67d7ff51bab7c93965826a
parent16721e33dc6f0e139a4057d86ef99531b8329472 (diff)
parentbc1a7fa83457009b9949a844179512f38ccec0a1 (diff)
downloadrust-8982535dc2247ac5fc1970bd498793fa2ec8c4f8.tar.gz
rust-8982535dc2247ac5fc1970bd498793fa2ec8c4f8.zip
Merge pull request #18884 from Veykril/push-xwqkorxozzkq
fix: Fix `env`/`option_env` macro check disregarding macro_rules definitions
-rw-r--r--src/tools/rust-analyzer/crates/hir/src/lib.rs17
-rw-r--r--src/tools/rust-analyzer/crates/ide-completion/src/completions/env_vars.rs57
2 files changed, 40 insertions, 34 deletions
diff --git a/src/tools/rust-analyzer/crates/hir/src/lib.rs b/src/tools/rust-analyzer/crates/hir/src/lib.rs
index 00b4db54374..b7c3e6bb415 100644
--- a/src/tools/rust-analyzer/crates/hir/src/lib.rs
+++ b/src/tools/rust-analyzer/crates/hir/src/lib.rs
@@ -3046,14 +3046,23 @@ impl Macro {
             MacroId::Macro2Id(it) => {
                 matches!(it.lookup(db.upcast()).expander, MacroExpander::BuiltInEager(eager) if eager.is_env_or_option_env())
             }
-            MacroId::MacroRulesId(_) | MacroId::ProcMacroId(_) => false,
+            MacroId::MacroRulesId(it) => {
+                matches!(it.lookup(db.upcast()).expander, MacroExpander::BuiltInEager(eager) if eager.is_env_or_option_env())
+            }
+            MacroId::ProcMacroId(_) => false,
         }
     }
 
     pub fn is_asm_or_global_asm(&self, db: &dyn HirDatabase) -> bool {
-        matches!(self.id, MacroId::Macro2Id(it) if {
-            matches!(it.lookup(db.upcast()).expander, MacroExpander::BuiltIn(m) if m.is_asm())
-        })
+        match self.id {
+            MacroId::Macro2Id(it) => {
+                matches!(it.lookup(db.upcast()).expander, MacroExpander::BuiltIn(m) if m.is_asm())
+            }
+            MacroId::MacroRulesId(it) => {
+                matches!(it.lookup(db.upcast()).expander, MacroExpander::BuiltIn(m) if m.is_asm())
+            }
+            MacroId::ProcMacroId(_) => false,
+        }
     }
 
     pub fn is_attr(&self, db: &dyn HirDatabase) -> bool {
diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/completions/env_vars.rs b/src/tools/rust-analyzer/crates/ide-completion/src/completions/env_vars.rs
index 0b6790d42a6..40af5203e9c 100644
--- a/src/tools/rust-analyzer/crates/ide-completion/src/completions/env_vars.rs
+++ b/src/tools/rust-analyzer/crates/ide-completion/src/completions/env_vars.rs
@@ -68,43 +68,40 @@ pub(crate) fn complete_cargo_env_vars(
 mod tests {
     use crate::tests::{check_edit, completion_list};
 
-    fn check(macro_name: &str) {
+    #[test]
+    fn completes_env_variable_in_env() {
         check_edit(
             "CARGO_BIN_NAME",
-            &format!(
-                r#"
-            #[rustc_builtin_macro]
-            macro {macro_name} {{
-                ($var:literal) => {{ 0 }}
-            }}
-
-            fn main() {{
-                let foo = {macro_name}!("CAR$0");
-            }}
-        "#
-            ),
-            &format!(
-                r#"
-            #[rustc_builtin_macro]
-            macro {macro_name} {{
-                ($var:literal) => {{ 0 }}
-            }}
-
-            fn main() {{
-                let foo = {macro_name}!("CARGO_BIN_NAME");
-            }}
-        "#
-            ),
+            r#"
+//- minicore: env
+fn main() {
+    let foo = env!("CAR$0");
+}
+        "#,
+            r#"
+fn main() {
+    let foo = env!("CARGO_BIN_NAME");
+}
+        "#,
         );
     }
-    #[test]
-    fn completes_env_variable_in_env() {
-        check("env")
-    }
 
     #[test]
     fn completes_env_variable_in_option_env() {
-        check("option_env");
+        check_edit(
+            "CARGO_BIN_NAME",
+            r#"
+//- minicore: env
+fn main() {
+    let foo = option_env!("CAR$0");
+}
+        "#,
+            r#"
+fn main() {
+    let foo = option_env!("CARGO_BIN_NAME");
+}
+        "#,
+        );
     }
 
     #[test]