Skip to content

Add tests for #12669 #12671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion crates/hir-def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);
Expand Down
62 changes: 62 additions & 0 deletions crates/hir-ty/src/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
"#,
);
}
19 changes: 19 additions & 0 deletions crates/ide-diagnostics/src/handlers/inactive_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down