Repository: rust-lang/rust-analyzer
Pull request #12671 (2022-07-01 14:33:17)

Add tests for #12669

flodiebold: @bors r+

betting that #12670 will merge before this :grimacing: 
bors: :pushpin: Commit 66c2dc3b3ff786eb20141bdfd6040394c62c82bc has been approved by `flodiebold`

<!-- @bors r=flodiebold 66c2dc3b3ff786eb20141bdfd6040394c62c82bc -->
<!-- homu: {"type":"Approved","sha":"66c2dc3b3ff786eb20141bdfd6040394c62c82bc","approver":"flodiebold"} -->
bors: :umbrella: The latest upstream changes (presumably #12668) made this pull request unmergeable. Please [resolve the merge conflicts](https://rustc-dev-guide.rust-lang.org/git.html#rebasing-and-conflicts).
flodiebold: @bors r+
bors: :pushpin: Commit c69e07f7b6bc53476b6d60a10c0fba766f4cac6d has been approved by `flodiebold`

<!-- @bors r=flodiebold c69e07f7b6bc53476b6d60a10c0fba766f4cac6d -->
<!-- homu: {"type":"Approved","sha":"c69e07f7b6bc53476b6d60a10c0fba766f4cac6d","approver":"flodiebold"} -->
bors: :hourglass: Testing commit c69e07f7b6bc53476b6d60a10c0fba766f4cac6d with merge bf5492ff61263cac27243c9740bd157d31d94cbd...
<!-- homu: {"type":"BuildStarted","head_sha":"c69e07f7b6bc53476b6d60a10c0fba766f4cac6d","merge_sha":"bf5492ff61263cac27243c9740bd157d31d94cbd"} -->
flodiebold: @bors r+
bors: :pushpin: Commit c7127709a99b693a0d21f97aa831e424402a1107 has been approved by `flodiebold`

<!-- @bors r=flodiebold c7127709a99b693a0d21f97aa831e424402a1107 -->
<!-- homu: {"type":"Approved","sha":"c7127709a99b693a0d21f97aa831e424402a1107","approver":"flodiebold"} -->
bors: :hourglass: Testing commit c7127709a99b693a0d21f97aa831e424402a1107 with merge 2eed3d25ad12cc0a0ae281a7df86a250a7718c3e...
<!-- homu: {"type":"BuildStarted","head_sha":"c7127709a99b693a0d21f97aa831e424402a1107","merge_sha":"2eed3d25ad12cc0a0ae281a7df86a250a7718c3e"} -->
bors: :broken_heart: Test failed - [checks-actions](https://github.com/rust-lang/rust-analyzer/runs/7153367563?check_suite_focus=true)
<!-- homu: {"type":"BuildFailed","builder_url":"https://github.com/rust-lang/rust-analyzer/runs/7153367563?check_suite_focus=true","builder_name":"checks-actions"} -->
flodiebold: @bors r+
bors: :pushpin: Commit dd5ac1b009b74215c58c628f5bf01359b8990a3a has been approved by `flodiebold`

<!-- @bors r=flodiebold dd5ac1b009b74215c58c628f5bf01359b8990a3a -->
<!-- homu: {"type":"Approved","sha":"dd5ac1b009b74215c58c628f5bf01359b8990a3a","approver":"flodiebold"} -->
bors: :hourglass: Testing commit dd5ac1b009b74215c58c628f5bf01359b8990a3a with merge 47f28ea334ee8cf80b5aa02348a618e348b467a1...
<!-- homu: {"type":"BuildStarted","head_sha":"dd5ac1b009b74215c58c628f5bf01359b8990a3a","merge_sha":"47f28ea334ee8cf80b5aa02348a618e348b467a1"} -->
bors: :broken_heart: Test failed - [checks-actions](https://github.com/rust-lang/rust-analyzer/runs/7153856109?check_suite_focus=true)
<!-- homu: {"type":"BuildFailed","builder_url":"https://github.com/rust-lang/rust-analyzer/runs/7153856109?check_suite_focus=true","builder_name":"checks-actions"} -->

Patch:
diff --git a/crates/hir-def/src/data.rs b/crates/hir-def/src/data.rs
index 12bfbbe308..8e8b5c322f 100644
--- a/crates/hir-def/src/data.rs
+++ b/crates/hir-def/src/data.rs
@@ -2,7 +2,7 @@
 
 use std::{mem, sync::Arc};
 
-use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId};
+use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId, MacroDefKind};
 use syntax::ast;
 
 use crate::{
@@ -498,6 +498,17 @@ impl<'a> AssocItemCollector<'a> {
                     if !self.db.enable_proc_attr_macros() {
                         continue 'attrs;
                     }
+                    let loc = self.db.lookup_intern_macro_call(call_id);
+                    if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
+                        // If there's no expander for the proc macro (e.g. the
+                        // proc macro is ignored, or building the proc macro
+                        // crate failed), skip expansion like we would if it was
+                        // disabled. This is analogous to the handling in
+                        // `DefCollector::collect_macros`.
+                        if exp.is_dummy() {
+                            continue 'attrs;
+                        }
+                    }
                     match self.expander.enter_expand_id(self.db, call_id) {
                         ExpandResult { value: Some((mark, mac)), .. } => {
                             self.collect_macro_items(mark, mac);
diff --git a/crates/ide-diagnostics/src/handlers/inactive_code.rs b/crates/ide-diagnostics/src/handlers/inactive_code.rs
index 17243d612a..97ea5c456a 100644
--- a/crates/ide-diagnostics/src/handlers/inactive_code.rs
+++ b/crates/ide-diagnostics/src/handlers/inactive_code.rs
@@ -104,6 +104,25 @@ fn f() {
         );
     }
 
+    #[test]
+    fn inactive_assoc_item() {
+        // FIXME these currently don't work, hence the *
+        check(
+            r#"
+struct Foo;
+impl Foo {
+    #[cfg(any())] pub fn f() {}
+  //*************************** weak: code is inactive due to #[cfg] directives
+}
+
+trait Bar {
+    #[cfg(any())] pub fn f() {}
+  //*************************** weak: code is inactive due to #[cfg] directives
+}
+"#,
+        );
+    }
+
     /// Tests that `cfg` attributes behind `cfg_attr` is handled properly.
     #[test]
     fn inactive_via_cfg_attr() {


Test patch:
diff --git a/crates/hir-ty/src/tests/macros.rs b/crates/hir-ty/src/tests/macros.rs
index a4299d9f05..a1ab6060e7 100644
--- a/crates/hir-ty/src/tests/macros.rs
+++ b/crates/hir-ty/src/tests/macros.rs
@@ -1274,3 +1274,65 @@ impl S {
         "#]],
     );
 }
+
+#[test]
+fn infer_in_unexpandable_attr_proc_macro_1() {
+    check_types(
+        r#"
+//- /main.rs crate:main deps:mac
+#[mac::attr_macro]
+fn foo() {
+    let xxx = 1;
+      //^^^ i32
+}
+
+//- /mac.rs crate:mac
+#![crate_type="proc-macro"]
+#[proc_macro_attribute]
+pub fn attr_macro() {}
+"#,
+    );
+}
+
+#[test]
+fn infer_in_unexpandable_attr_proc_macro_in_impl() {
+    check_types(
+        r#"
+//- /main.rs crate:main deps:mac
+struct Foo;
+impl Foo {
+    #[mac::attr_macro]
+    fn foo() {
+        let xxx = 1;
+          //^^^ i32
+    }
+}
+
+//- /mac.rs crate:mac
+#![crate_type="proc-macro"]
+#[proc_macro_attribute]
+pub fn attr_macro() {}
+"#,
+    );
+}
+
+#[test]
+fn infer_in_unexpandable_attr_proc_macro_in_trait() {
+    check_types(
+        r#"
+//- /main.rs crate:main deps:mac
+trait Foo {
+    #[mac::attr_macro]
+    fn foo() {
+        let xxx = 1;
+          //^^^ i32
+    }
+}
+
+//- /mac.rs crate:mac
+#![crate_type="proc-macro"]
+#[proc_macro_attribute]
+pub fn attr_macro() {}
+"#,
+    );
+}