Skip to content

Commit 017747f

Browse files
committed
Only suggest adding ! to expressions that can be macro invocation
1 parent 9ad5d82 commit 017747f

File tree

6 files changed

+82
-24
lines changed

6 files changed

+82
-24
lines changed

compiler/rustc_resolve/src/late.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2517,6 +2517,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
25172517
self.visit_expr(elem);
25182518
self.resolve_anon_const(ct, IsRepeatExpr::Yes);
25192519
}
2520+
ExprKind::Index(ref elem, ref idx) => {
2521+
self.resolve_expr(elem, Some(expr));
2522+
self.visit_expr(idx);
2523+
}
25202524
_ => {
25212525
visit::walk_expr(self, expr);
25222526
}

compiler/rustc_resolve/src/late/diagnostics.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,13 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
970970
};
971971

972972
match (res, source) {
973-
(Res::Def(DefKind::Macro(MacroKind::Bang), _), _) => {
973+
(
974+
Res::Def(DefKind::Macro(MacroKind::Bang), _),
975+
PathSource::Expr(Some(Expr {
976+
kind: ExprKind::Index(..) | ExprKind::Call(..), ..
977+
}))
978+
| PathSource::Struct,
979+
) => {
974980
err.span_label(span, fallback_label);
975981
err.span_suggestion_verbose(
976982
span.shrink_to_hi(),
@@ -982,6 +988,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
982988
err.note("if you want the `try` keyword, you need Rust 2018 or later");
983989
}
984990
}
991+
(Res::Def(DefKind::Macro(MacroKind::Bang), _), _) => {
992+
err.span_label(span, fallback_label);
993+
}
985994
(Res::Def(DefKind::TyAlias, def_id), PathSource::Trait(_)) => {
986995
err.span_label(span, "type aliases cannot be used as traits");
987996
if self.r.session.is_nightly_build() {

src/test/ui/hygiene/rustc-macro-transparency.stderr

+8-20
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@ LL | struct SemiTransparent;
1111
| ----------------------- similarly named unit struct `SemiTransparent` defined here
1212
...
1313
LL | semitransparent;
14-
| ^^^^^^^^^^^^^^^ not a value
15-
|
16-
help: use `!` to invoke the macro
17-
|
18-
LL | semitransparent!;
19-
| +
20-
help: a unit struct with a similar name exists
21-
|
22-
LL | SemiTransparent;
23-
| ~~~~~~~~~~~~~~~
14+
| ^^^^^^^^^^^^^^^
15+
| |
16+
| not a value
17+
| help: a unit struct with a similar name exists: `SemiTransparent`
2418

2519
error[E0423]: expected value, found macro `opaque`
2620
--> $DIR/rustc-macro-transparency.rs:30:5
@@ -29,16 +23,10 @@ LL | struct Opaque;
2923
| -------------- similarly named unit struct `Opaque` defined here
3024
...
3125
LL | opaque;
32-
| ^^^^^^ not a value
33-
|
34-
help: use `!` to invoke the macro
35-
|
36-
LL | opaque!;
37-
| +
38-
help: a unit struct with a similar name exists
39-
|
40-
LL | Opaque;
41-
| ~~~~~~
26+
| ^^^^^^
27+
| |
28+
| not a value
29+
| help: a unit struct with a similar name exists (notice the capitalization): `Opaque`
4230

4331
error: aborting due to 3 previous errors
4432

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
fn main() {
3+
assert_eq!(1, 1);
4+
//~^ ERROR expected function, found macro `assert_eq`
5+
assert_eq! { 1, 1 };
6+
//~^ ERROR expected struct, variant or union type, found macro `assert_eq`
7+
//~| ERROR expected identifier, found `1`
8+
//~| ERROR expected identifier, found `1`
9+
assert![true];
10+
//~^ ERROR expected value, found macro `assert`
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
// run-rustfix
12
fn main() {
23
assert_eq(1, 1);
34
//~^ ERROR expected function, found macro `assert_eq`
5+
assert_eq { 1, 1 };
6+
//~^ ERROR expected struct, variant or union type, found macro `assert_eq`
7+
//~| ERROR expected identifier, found `1`
8+
//~| ERROR expected identifier, found `1`
9+
assert[true];
10+
//~^ ERROR expected value, found macro `assert`
411
}
+42-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1+
error: expected identifier, found `1`
2+
--> $DIR/resolve-hint-macro.rs:5:17
3+
|
4+
LL | assert_eq { 1, 1 };
5+
| --------- ^ expected identifier
6+
| |
7+
| while parsing this struct
8+
9+
error: expected identifier, found `1`
10+
--> $DIR/resolve-hint-macro.rs:5:20
11+
|
12+
LL | assert_eq { 1, 1 };
13+
| --------- ^ expected identifier
14+
| |
15+
| while parsing this struct
16+
117
error[E0423]: expected function, found macro `assert_eq`
2-
--> $DIR/resolve-hint-macro.rs:2:5
18+
--> $DIR/resolve-hint-macro.rs:3:5
319
|
420
LL | assert_eq(1, 1);
521
| ^^^^^^^^^ not a function
@@ -9,6 +25,29 @@ help: use `!` to invoke the macro
925
LL | assert_eq!(1, 1);
1026
| +
1127

12-
error: aborting due to previous error
28+
error[E0574]: expected struct, variant or union type, found macro `assert_eq`
29+
--> $DIR/resolve-hint-macro.rs:5:5
30+
|
31+
LL | assert_eq { 1, 1 };
32+
| ^^^^^^^^^ not a struct, variant or union type
33+
|
34+
help: use `!` to invoke the macro
35+
|
36+
LL | assert_eq! { 1, 1 };
37+
| +
38+
39+
error[E0423]: expected value, found macro `assert`
40+
--> $DIR/resolve-hint-macro.rs:9:5
41+
|
42+
LL | assert[true];
43+
| ^^^^^^ not a value
44+
|
45+
help: use `!` to invoke the macro
46+
|
47+
LL | assert![true];
48+
| +
49+
50+
error: aborting due to 5 previous errors
1351

14-
For more information about this error, try `rustc --explain E0423`.
52+
Some errors have detailed explanations: E0423, E0574.
53+
For more information about an error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)