Skip to content

Commit

Permalink
Rollup merge of #93061 - estebank:macr-suggestion, r=cjgillot
Browse files Browse the repository at this point in the history
Only suggest adding `!` to expressions that can be macro invocation
  • Loading branch information
matthiaskrgr authored Jan 20, 2022
2 parents aa7f452 + 017747f commit dc393b2
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 24 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.visit_expr(elem);
self.resolve_anon_const(ct, IsRepeatExpr::Yes);
}
ExprKind::Index(ref elem, ref idx) => {
self.resolve_expr(elem, Some(expr));
self.visit_expr(idx);
}
_ => {
visit::walk_expr(self, expr);
}
Expand Down
11 changes: 10 additions & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,13 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
};

match (res, source) {
(Res::Def(DefKind::Macro(MacroKind::Bang), _), _) => {
(
Res::Def(DefKind::Macro(MacroKind::Bang), _),
PathSource::Expr(Some(Expr {
kind: ExprKind::Index(..) | ExprKind::Call(..), ..
}))
| PathSource::Struct,
) => {
err.span_label(span, fallback_label);
err.span_suggestion_verbose(
span.shrink_to_hi(),
Expand All @@ -982,6 +988,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.note("if you want the `try` keyword, you need Rust 2018 or later");
}
}
(Res::Def(DefKind::Macro(MacroKind::Bang), _), _) => {
err.span_label(span, fallback_label);
}
(Res::Def(DefKind::TyAlias, def_id), PathSource::Trait(_)) => {
err.span_label(span, "type aliases cannot be used as traits");
if self.r.session.is_nightly_build() {
Expand Down
28 changes: 8 additions & 20 deletions src/test/ui/hygiene/rustc-macro-transparency.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@ LL | struct SemiTransparent;
| ----------------------- similarly named unit struct `SemiTransparent` defined here
...
LL | semitransparent;
| ^^^^^^^^^^^^^^^ not a value
|
help: use `!` to invoke the macro
|
LL | semitransparent!;
| +
help: a unit struct with a similar name exists
|
LL | SemiTransparent;
| ~~~~~~~~~~~~~~~
| ^^^^^^^^^^^^^^^
| |
| not a value
| help: a unit struct with a similar name exists: `SemiTransparent`

error[E0423]: expected value, found macro `opaque`
--> $DIR/rustc-macro-transparency.rs:30:5
Expand All @@ -29,16 +23,10 @@ LL | struct Opaque;
| -------------- similarly named unit struct `Opaque` defined here
...
LL | opaque;
| ^^^^^^ not a value
|
help: use `!` to invoke the macro
|
LL | opaque!;
| +
help: a unit struct with a similar name exists
|
LL | Opaque;
| ~~~~~~
| ^^^^^^
| |
| not a value
| help: a unit struct with a similar name exists (notice the capitalization): `Opaque`

error: aborting due to 3 previous errors

Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/resolve/resolve-hint-macro.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-rustfix
fn main() {
assert_eq!(1, 1);
//~^ ERROR expected function, found macro `assert_eq`
assert_eq! { 1, 1 };
//~^ ERROR expected struct, variant or union type, found macro `assert_eq`
//~| ERROR expected identifier, found `1`
//~| ERROR expected identifier, found `1`
assert![true];
//~^ ERROR expected value, found macro `assert`
}
7 changes: 7 additions & 0 deletions src/test/ui/resolve/resolve-hint-macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
// run-rustfix
fn main() {
assert_eq(1, 1);
//~^ ERROR expected function, found macro `assert_eq`
assert_eq { 1, 1 };
//~^ ERROR expected struct, variant or union type, found macro `assert_eq`
//~| ERROR expected identifier, found `1`
//~| ERROR expected identifier, found `1`
assert[true];
//~^ ERROR expected value, found macro `assert`
}
45 changes: 42 additions & 3 deletions src/test/ui/resolve/resolve-hint-macro.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
error: expected identifier, found `1`
--> $DIR/resolve-hint-macro.rs:5:17
|
LL | assert_eq { 1, 1 };
| --------- ^ expected identifier
| |
| while parsing this struct

error: expected identifier, found `1`
--> $DIR/resolve-hint-macro.rs:5:20
|
LL | assert_eq { 1, 1 };
| --------- ^ expected identifier
| |
| while parsing this struct

error[E0423]: expected function, found macro `assert_eq`
--> $DIR/resolve-hint-macro.rs:2:5
--> $DIR/resolve-hint-macro.rs:3:5
|
LL | assert_eq(1, 1);
| ^^^^^^^^^ not a function
Expand All @@ -9,6 +25,29 @@ help: use `!` to invoke the macro
LL | assert_eq!(1, 1);
| +

error: aborting due to previous error
error[E0574]: expected struct, variant or union type, found macro `assert_eq`
--> $DIR/resolve-hint-macro.rs:5:5
|
LL | assert_eq { 1, 1 };
| ^^^^^^^^^ not a struct, variant or union type
|
help: use `!` to invoke the macro
|
LL | assert_eq! { 1, 1 };
| +

error[E0423]: expected value, found macro `assert`
--> $DIR/resolve-hint-macro.rs:9:5
|
LL | assert[true];
| ^^^^^^ not a value
|
help: use `!` to invoke the macro
|
LL | assert![true];
| +

error: aborting due to 5 previous errors

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

0 comments on commit dc393b2

Please sign in to comment.