Skip to content

Commit c31e02a

Browse files
authored
Rollup merge of #88232 - m-ou-se:macro-name-imported-but-not-macro, r=estebank
Add notes to macro-not-found diagnostics to point out how things with the same name were not a match. This adds notes like: ``` error: cannot find derive macro `Serialize` in this scope --> $DIR/issue-88206.rs:22:10 | LL | #[derive(Serialize)] | ^^^^^^^^^ | note: `Serialize` is imported here, but it is not a derive macro --> $DIR/issue-88206.rs:17:11 | LL | use hey::{Serialize, Deserialize}; | ^^^^^^^^^ ``` Fixes #88206 Includes #88229 r? `@estebank`
2 parents 5cf025f + 908ce2f commit c31e02a

File tree

8 files changed

+250
-1
lines changed

8 files changed

+250
-1
lines changed

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+52
Original file line numberDiff line numberDiff line change
@@ -956,9 +956,61 @@ impl<'a> Resolver<'a> {
956956
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
957957
let msg = format!("unsafe traits like `{}` should be implemented explicitly", ident);
958958
err.span_note(ident.span, &msg);
959+
return;
959960
}
960961
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
961962
err.help("have you added the `#[macro_use]` on the module/import?");
963+
return;
964+
}
965+
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
966+
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
967+
ident,
968+
ScopeSet::All(ns, false),
969+
&parent_scope,
970+
false,
971+
false,
972+
ident.span,
973+
) {
974+
let desc = match binding.res() {
975+
Res::Def(DefKind::Macro(MacroKind::Bang), _) => {
976+
"a function-like macro".to_string()
977+
}
978+
Res::Def(DefKind::Macro(MacroKind::Attr), _) | Res::NonMacroAttr(..) => {
979+
format!("an attribute: `#[{}]`", ident)
980+
}
981+
Res::Def(DefKind::Macro(MacroKind::Derive), _) => {
982+
format!("a derive macro: `#[derive({})]`", ident)
983+
}
984+
Res::ToolMod => {
985+
// Don't confuse the user with tool modules.
986+
continue;
987+
}
988+
Res::Def(DefKind::Trait, _) if macro_kind == MacroKind::Derive => {
989+
"only a trait, without a derive macro".to_string()
990+
}
991+
res => format!(
992+
"{} {}, not {} {}",
993+
res.article(),
994+
res.descr(),
995+
macro_kind.article(),
996+
macro_kind.descr_expected(),
997+
),
998+
};
999+
if let crate::NameBindingKind::Import { import, .. } = binding.kind {
1000+
if !import.span.is_dummy() {
1001+
err.span_note(
1002+
import.span,
1003+
&format!("`{}` is imported here, but it is {}", ident, desc),
1004+
);
1005+
// Silence the 'unused import' warning we might get,
1006+
// since this diagnostic already covers that import.
1007+
self.record_use(ident, binding, false);
1008+
return;
1009+
}
1010+
}
1011+
err.note(&format!("`{}` is in scope, but it is {}", ident, desc));
1012+
return;
1013+
}
9621014
}
9631015
}
9641016

Diff for: src/test/ui/issues/issue-11692-2.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: cannot find macro `test` in this scope
33
|
44
LL | concat!(test!());
55
| ^^^^
6+
|
7+
= note: `test` is in scope, but it is an attribute: `#[test]`
68

79
error: aborting due to previous error
810

Diff for: src/test/ui/macros/issue-88206.rs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// compile-flags: -Z deduplicate-diagnostics=yes
2+
3+
#![warn(unused_imports)]
4+
5+
use std::str::*;
6+
//~^ NOTE `from_utf8` is imported here, but it is a function
7+
//~| NOTE `from_utf8_mut` is imported here, but it is a function
8+
//~| NOTE `from_utf8_unchecked` is imported here, but it is a function
9+
10+
mod hey {
11+
pub trait Serialize {}
12+
pub trait Deserialize {}
13+
14+
pub struct X(i32);
15+
}
16+
17+
use hey::{Serialize, Deserialize, X};
18+
//~^ NOTE `Serialize` is imported here, but it is only a trait, without a derive macro
19+
//~| NOTE `Deserialize` is imported here, but it is a trait
20+
//~| NOTE `X` is imported here, but it is a struct
21+
22+
#[derive(Serialize)]
23+
//~^ ERROR cannot find derive macro `Serialize`
24+
struct A;
25+
26+
#[derive(from_utf8_mut)]
27+
//~^ ERROR cannot find derive macro `from_utf8_mut`
28+
struct B;
29+
30+
#[derive(println)]
31+
//~^ ERROR cannot find derive macro `println`
32+
//~| NOTE `println` is in scope, but it is a function-like macro
33+
struct C;
34+
35+
#[Deserialize]
36+
//~^ ERROR cannot find attribute `Deserialize`
37+
struct D;
38+
39+
#[from_utf8_unchecked]
40+
//~^ ERROR cannot find attribute `from_utf8_unchecked`
41+
struct E;
42+
43+
#[println]
44+
//~^ ERROR cannot find attribute `println`
45+
//~| NOTE `println` is in scope, but it is a function-like macro
46+
struct F;
47+
48+
fn main() {
49+
from_utf8!();
50+
//~^ ERROR cannot find macro `from_utf8`
51+
52+
Box!();
53+
//~^ ERROR cannot find macro `Box`
54+
//~| NOTE `Box` is in scope, but it is a struct
55+
56+
Copy!();
57+
//~^ ERROR cannot find macro `Copy`
58+
//~| NOTE `Copy` is in scope, but it is a derive macro
59+
60+
test!();
61+
//~^ ERROR cannot find macro `test`
62+
//~| NOTE `test` is in scope, but it is an attribute
63+
64+
X!();
65+
//~^ ERROR cannot find macro `X`
66+
}

Diff for: src/test/ui/macros/issue-88206.stderr

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
error: cannot find macro `X` in this scope
2+
--> $DIR/issue-88206.rs:64:5
3+
|
4+
LL | X!();
5+
| ^
6+
|
7+
note: `X` is imported here, but it is a struct, not a macro
8+
--> $DIR/issue-88206.rs:17:35
9+
|
10+
LL | use hey::{Serialize, Deserialize, X};
11+
| ^
12+
13+
error: cannot find macro `test` in this scope
14+
--> $DIR/issue-88206.rs:60:5
15+
|
16+
LL | test!();
17+
| ^^^^
18+
|
19+
= note: `test` is in scope, but it is an attribute: `#[test]`
20+
21+
error: cannot find macro `Copy` in this scope
22+
--> $DIR/issue-88206.rs:56:5
23+
|
24+
LL | Copy!();
25+
| ^^^^
26+
|
27+
= note: `Copy` is in scope, but it is a derive macro: `#[derive(Copy)]`
28+
29+
error: cannot find macro `Box` in this scope
30+
--> $DIR/issue-88206.rs:52:5
31+
|
32+
LL | Box!();
33+
| ^^^
34+
|
35+
= note: `Box` is in scope, but it is a struct, not a macro
36+
37+
error: cannot find macro `from_utf8` in this scope
38+
--> $DIR/issue-88206.rs:49:5
39+
|
40+
LL | from_utf8!();
41+
| ^^^^^^^^^
42+
|
43+
note: `from_utf8` is imported here, but it is a function, not a macro
44+
--> $DIR/issue-88206.rs:5:5
45+
|
46+
LL | use std::str::*;
47+
| ^^^^^^^^^^^
48+
49+
error: cannot find attribute `println` in this scope
50+
--> $DIR/issue-88206.rs:43:3
51+
|
52+
LL | #[println]
53+
| ^^^^^^^
54+
|
55+
= note: `println` is in scope, but it is a function-like macro
56+
57+
error: cannot find attribute `from_utf8_unchecked` in this scope
58+
--> $DIR/issue-88206.rs:39:3
59+
|
60+
LL | #[from_utf8_unchecked]
61+
| ^^^^^^^^^^^^^^^^^^^
62+
|
63+
note: `from_utf8_unchecked` is imported here, but it is a function, not an attribute
64+
--> $DIR/issue-88206.rs:5:5
65+
|
66+
LL | use std::str::*;
67+
| ^^^^^^^^^^^
68+
69+
error: cannot find attribute `Deserialize` in this scope
70+
--> $DIR/issue-88206.rs:35:3
71+
|
72+
LL | #[Deserialize]
73+
| ^^^^^^^^^^^
74+
|
75+
note: `Deserialize` is imported here, but it is a trait, not an attribute
76+
--> $DIR/issue-88206.rs:17:22
77+
|
78+
LL | use hey::{Serialize, Deserialize, X};
79+
| ^^^^^^^^^^^
80+
81+
error: cannot find derive macro `println` in this scope
82+
--> $DIR/issue-88206.rs:30:10
83+
|
84+
LL | #[derive(println)]
85+
| ^^^^^^^
86+
|
87+
= note: `println` is in scope, but it is a function-like macro
88+
89+
error: cannot find derive macro `from_utf8_mut` in this scope
90+
--> $DIR/issue-88206.rs:26:10
91+
|
92+
LL | #[derive(from_utf8_mut)]
93+
| ^^^^^^^^^^^^^
94+
|
95+
note: `from_utf8_mut` is imported here, but it is a function, not a derive macro
96+
--> $DIR/issue-88206.rs:5:5
97+
|
98+
LL | use std::str::*;
99+
| ^^^^^^^^^^^
100+
101+
error: cannot find derive macro `Serialize` in this scope
102+
--> $DIR/issue-88206.rs:22:10
103+
|
104+
LL | #[derive(Serialize)]
105+
| ^^^^^^^^^
106+
|
107+
note: `Serialize` is imported here, but it is only a trait, without a derive macro
108+
--> $DIR/issue-88206.rs:17:11
109+
|
110+
LL | use hey::{Serialize, Deserialize, X};
111+
| ^^^^^^^^^
112+
113+
error: aborting due to 11 previous errors
114+

Diff for: src/test/ui/macros/issue-88228.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct A;
1313

1414
#[derive(println)]
1515
//~^ ERROR cannot find derive macro `println`
16+
//~|`println` is in scope, but it is a function-like macro
1617
struct B;
1718

1819
fn main() {

Diff for: src/test/ui/macros/issue-88228.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: cannot find macro `bla` in this scope
2-
--> $DIR/issue-88228.rs:19:5
2+
--> $DIR/issue-88228.rs:20:5
33
|
44
LL | bla!();
55
| ^^^
@@ -12,6 +12,8 @@ error: cannot find derive macro `println` in this scope
1212
|
1313
LL | #[derive(println)]
1414
| ^^^^^^^
15+
|
16+
= note: `println` is in scope, but it is a function-like macro
1517

1618
error: cannot find derive macro `Bla` in this scope
1719
--> $DIR/issue-88228.rs:9:10

Diff for: src/test/ui/macros/macro-path-prelude-fail-3.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ LL | inline!();
88
|
99
LL | macro_rules! line {
1010
| ----------------- similarly named macro `line` defined here
11+
|
12+
= note: `inline` is in scope, but it is an attribute: `#[inline]`
1113

1214
error: aborting due to previous error
1315

Diff for: src/test/ui/proc-macro/macro-namespace-reserved-2.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,40 @@ error: cannot find macro `my_macro_attr` in this scope
9393
|
9494
LL | my_macro_attr!();
9595
| ^^^^^^^^^^^^^
96+
|
97+
= note: `my_macro_attr` is in scope, but it is an attribute: `#[my_macro_attr]`
9698

9799
error: cannot find macro `MyTrait` in this scope
98100
--> $DIR/macro-namespace-reserved-2.rs:33:5
99101
|
100102
LL | MyTrait!();
101103
| ^^^^^^^
104+
|
105+
= note: `MyTrait` is in scope, but it is a derive macro: `#[derive(MyTrait)]`
102106

103107
error: cannot find attribute `my_macro` in this scope
104108
--> $DIR/macro-namespace-reserved-2.rs:38:3
105109
|
106110
LL | #[my_macro]
107111
| ^^^^^^^^
112+
|
113+
= note: `my_macro` is in scope, but it is a function-like macro
108114

109115
error: cannot find derive macro `my_macro` in this scope
110116
--> $DIR/macro-namespace-reserved-2.rs:48:10
111117
|
112118
LL | #[derive(my_macro)]
113119
| ^^^^^^^^
120+
|
121+
= note: `my_macro` is in scope, but it is a function-like macro
114122

115123
error: cannot find derive macro `my_macro` in this scope
116124
--> $DIR/macro-namespace-reserved-2.rs:48:10
117125
|
118126
LL | #[derive(my_macro)]
119127
| ^^^^^^^^
128+
|
129+
= note: `my_macro` is in scope, but it is a function-like macro
120130

121131
error: aborting due to 20 previous errors
122132

0 commit comments

Comments
 (0)