Skip to content

Commit ec78b6b

Browse files
committed
Auto merge of rust-lang#12671 - flodiebold:test-for-12669, r=flodiebold
Add tests for rust-lang#12669
2 parents 994f3cf + 9a12d0d commit ec78b6b

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

crates/hir-def/src/data.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{mem, sync::Arc};
44

5-
use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId};
5+
use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId, MacroDefKind};
66
use syntax::ast;
77

88
use crate::{
@@ -498,6 +498,17 @@ impl<'a> AssocItemCollector<'a> {
498498
if !self.db.enable_proc_attr_macros() {
499499
continue 'attrs;
500500
}
501+
let loc = self.db.lookup_intern_macro_call(call_id);
502+
if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
503+
// If there's no expander for the proc macro (e.g. the
504+
// proc macro is ignored, or building the proc macro
505+
// crate failed), skip expansion like we would if it was
506+
// disabled. This is analogous to the handling in
507+
// `DefCollector::collect_macros`.
508+
if exp.is_dummy() {
509+
continue 'attrs;
510+
}
511+
}
501512
match self.expander.enter_expand_id(self.db, call_id) {
502513
ExpandResult { value: Some((mark, mac)), .. } => {
503514
self.collect_macro_items(mark, mac);

crates/hir-ty/src/tests/macros.rs

+62
Original file line numberDiff line numberDiff line change
@@ -1274,3 +1274,65 @@ impl S {
12741274
"#]],
12751275
);
12761276
}
1277+
1278+
#[test]
1279+
fn infer_in_unexpandable_attr_proc_macro_1() {
1280+
check_types(
1281+
r#"
1282+
//- /main.rs crate:main deps:mac
1283+
#[mac::attr_macro]
1284+
fn foo() {
1285+
let xxx = 1;
1286+
//^^^ i32
1287+
}
1288+
1289+
//- /mac.rs crate:mac
1290+
#![crate_type="proc-macro"]
1291+
#[proc_macro_attribute]
1292+
pub fn attr_macro() {}
1293+
"#,
1294+
);
1295+
}
1296+
1297+
#[test]
1298+
fn infer_in_unexpandable_attr_proc_macro_in_impl() {
1299+
check_types(
1300+
r#"
1301+
//- /main.rs crate:main deps:mac
1302+
struct Foo;
1303+
impl Foo {
1304+
#[mac::attr_macro]
1305+
fn foo() {
1306+
let xxx = 1;
1307+
//^^^ i32
1308+
}
1309+
}
1310+
1311+
//- /mac.rs crate:mac
1312+
#![crate_type="proc-macro"]
1313+
#[proc_macro_attribute]
1314+
pub fn attr_macro() {}
1315+
"#,
1316+
);
1317+
}
1318+
1319+
#[test]
1320+
fn infer_in_unexpandable_attr_proc_macro_in_trait() {
1321+
check_types(
1322+
r#"
1323+
//- /main.rs crate:main deps:mac
1324+
trait Foo {
1325+
#[mac::attr_macro]
1326+
fn foo() {
1327+
let xxx = 1;
1328+
//^^^ i32
1329+
}
1330+
}
1331+
1332+
//- /mac.rs crate:mac
1333+
#![crate_type="proc-macro"]
1334+
#[proc_macro_attribute]
1335+
pub fn attr_macro() {}
1336+
"#,
1337+
);
1338+
}

crates/ide-diagnostics/src/handlers/inactive_code.rs

+19
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,25 @@ fn f() {
104104
);
105105
}
106106

107+
#[test]
108+
fn inactive_assoc_item() {
109+
// FIXME these currently don't work, hence the *
110+
check(
111+
r#"
112+
struct Foo;
113+
impl Foo {
114+
#[cfg(any())] pub fn f() {}
115+
//*************************** weak: code is inactive due to #[cfg] directives
116+
}
117+
118+
trait Bar {
119+
#[cfg(any())] pub fn f() {}
120+
//*************************** weak: code is inactive due to #[cfg] directives
121+
}
122+
"#,
123+
);
124+
}
125+
107126
/// Tests that `cfg` attributes behind `cfg_attr` is handled properly.
108127
#[test]
109128
fn inactive_via_cfg_attr() {

0 commit comments

Comments
 (0)