Skip to content

Commit

Permalink
Add a label to point to the lacking macro name definition
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Jul 26, 2024
1 parent 5be2ec7 commit 2fca4ea
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
5 changes: 4 additions & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
);

if macro_kind == MacroKind::Bang && ident.name == sym::macro_rules {
err.subdiagnostic(MaybeMissingMacroRulesName { span: ident.span });
let label_span = ident.span.shrink_to_hi();
let mut spans = MultiSpan::from_span(label_span);
spans.push_span_label(label_span, "put a macro name here");
err.subdiagnostic(MaybeMissingMacroRulesName { spans: spans });
return;
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ pub(crate) struct MacroSuggMovePosition {
#[note(resolve_missing_macro_rules_name)]
pub(crate) struct MaybeMissingMacroRulesName {
#[primary_span]
pub(crate) span: Span,
pub(crate) spans: MultiSpan,
}

#[derive(Subdiagnostic)]
Expand Down
1 change: 1 addition & 0 deletions tests/ui/macros/issue-118786.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ macro_rules! make_macro {
macro_rules! $macro_name {
//~^ ERROR macro expansion ignores token `{` and any following
//~| ERROR cannot find macro `macro_rules` in this scope
//~| put a macro name here
() => {}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/macros/issue-118786.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: macros that expand to items must be delimited with braces or followed by a semicolon
--> $DIR/issue-118786.rs:15:13
--> $DIR/issue-118786.rs:16:13
|
LL | make_macro!((meow));
| ^^^^^^
Expand Down Expand Up @@ -34,10 +34,10 @@ LL | make_macro!((meow));
| ------------------- in this macro invocation
|
note: maybe you have forgotten to define a name for this `macro_rules!`
--> $DIR/issue-118786.rs:7:9
--> $DIR/issue-118786.rs:7:20
|
LL | macro_rules! $macro_name {
| ^^^^^^^^^^^
| ^ put a macro name here
...
LL | make_macro!((meow));
| ------------------- in this macro invocation
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/resolve/issue-118295.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
macro_rules! {}
//~^ ERROR cannot find macro `macro_rules` in this scope
//~| NOTE maybe you have forgotten to define a name for this `macro_rules!`
//~| put a macro name here

macro_rules!{}
//~^ ERROR cannot find macro `macro_rules` in this scope
//~| NOTE maybe you have forgotten to define a name for this `macro_rules!`
//~| put a macro name here

fn main() {}
18 changes: 15 additions & 3 deletions tests/ui/resolve/issue-118295.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
error: cannot find macro `macro_rules` in this scope
--> $DIR/issue-118295.rs:1:1
--> $DIR/issue-118295.rs:6:1
|
LL | macro_rules! {}
LL | macro_rules!{}
| ^^^^^^^^^^^
|
note: maybe you have forgotten to define a name for this `macro_rules!`
--> $DIR/issue-118295.rs:6:12
|
LL | macro_rules!{}
| ^ put a macro name here

error: cannot find macro `macro_rules` in this scope
--> $DIR/issue-118295.rs:1:1
|
LL | macro_rules! {}
| ^^^^^^^^^^^
|
note: maybe you have forgotten to define a name for this `macro_rules!`
--> $DIR/issue-118295.rs:1:12
|
LL | macro_rules! {}
| ^ put a macro name here

error: aborting due to 1 previous error
error: aborting due to 2 previous errors

0 comments on commit 2fca4ea

Please sign in to comment.