Skip to content

Commit 8a0544e

Browse files
committed
tip for define macro name after macro_rules!
1 parent 274b524 commit 8a0544e

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

compiler/rustc_resolve/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ resolve_method_not_member_of_trait =
183183
method `{$method}` is not a member of trait `{$trait_}`
184184
.label = not a member of trait `{$trait_}`
185185
186+
resolve_missing_macro_rules_name = maybe you have forgotten to define a name for this `macro_rules`
187+
186188
resolve_module_only =
187189
visibility must resolve to a module
188190

compiler/rustc_resolve/src/diagnostics.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use thin_vec::{thin_vec, ThinVec};
2929

3030
use crate::errors::{
3131
AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
32-
ExplicitUnsafeTraits,
3332
};
33+
use crate::errors::{ExplicitUnsafeTraits, MaybeMissingMacroRulesName};
3434
use crate::imports::{Import, ImportKind};
3535
use crate::late::{PatternSource, Rib};
3636
use crate::path_names_to_string;
@@ -1421,14 +1421,23 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14211421
"",
14221422
);
14231423

1424+
if macro_kind == MacroKind::Bang && ident.name == sym::macro_rules {
1425+
let hi = BytePos(ident.span.hi().0 + 1);
1426+
let span = ident.span.with_hi(hi).shrink_to_hi();
1427+
err.subdiagnostic(MaybeMissingMacroRulesName { span });
1428+
return;
1429+
}
1430+
14241431
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
14251432
err.subdiagnostic(ExplicitUnsafeTraits { span: ident.span, ident });
14261433
return;
14271434
}
1435+
14281436
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
14291437
err.subdiagnostic(AddedMacroUse);
14301438
return;
14311439
}
1440+
14321441
if ident.name == kw::Default
14331442
&& let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind
14341443
{

compiler/rustc_resolve/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,13 @@ pub(crate) struct ExplicitUnsafeTraits {
665665
pub(crate) ident: Ident,
666666
}
667667

668+
#[derive(Subdiagnostic)]
669+
#[note(resolve_missing_macro_rules_name)]
670+
pub(crate) struct MaybeMissingMacroRulesName {
671+
#[primary_span]
672+
pub(crate) span: Span,
673+
}
674+
668675
#[derive(Subdiagnostic)]
669676
#[help(resolve_added_macro_use)]
670677
pub(crate) struct AddedMacroUse;

tests/ui/resolve/issue-118295.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
macro_rules! {}
2+
//~^ ERROR cannot find macro `macro_rules` in this scope
3+
//~| NOTE maybe you have forgotten to define a name for this `macro_rules`
4+
5+
fn main() {}

tests/ui/resolve/issue-118295.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot find macro `macro_rules` in this scope
2+
--> $DIR/issue-118295.rs:1:1
3+
|
4+
LL | macro_rules! {}
5+
| ^^^^^^^^^^^
6+
|
7+
note: maybe you have forgotten to define a name for this `macro_rules`
8+
--> $DIR/issue-118295.rs:1:13
9+
|
10+
LL | macro_rules! {}
11+
| ^
12+
13+
error: aborting due to 1 previous error
14+

0 commit comments

Comments
 (0)