Skip to content

Commit 885fc78

Browse files
committed
tip for define macro name after macro_rules!
1 parent 2f1ba4a commit 885fc78

File tree

5 files changed

+125
-4
lines changed

5 files changed

+125
-4
lines changed

compiler/rustc_resolve/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ resolve_method_not_member_of_trait =
181181
method `{$method}` is not a member of trait `{$trait_}`
182182
.label = not a member of trait `{$trait_}`
183183
184+
resolve_missing_macro_rules_name = maybe you have forgotten to define a name for this `macro_rules`
185+
184186
resolve_module_only =
185187
visibility must resolve to a module
186188

compiler/rustc_resolve/src/diagnostics.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
2727
use rustc_span::{BytePos, Span, SyntaxContext};
2828
use thin_vec::{thin_vec, ThinVec};
2929

30-
use crate::errors::{
31-
AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
32-
ExplicitUnsafeTraits,
33-
};
30+
use crate::errors::{AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion};
31+
use crate::errors::{ConsiderAddingADerive, ExplicitUnsafeTraits, MaybeMissingMacroRulesName};
3432
use crate::imports::{Import, ImportKind};
3533
use crate::late::{PatternSource, Rib};
3634
use crate::path_names_to_string;
@@ -1421,14 +1419,42 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14211419
"",
14221420
);
14231421

1422+
if macro_kind == MacroKind::Bang
1423+
&& ident.name == sym::macro_rules
1424+
&& let source_map = self.tcx.sess.source_map()
1425+
&& let Ok(next_source) = source_map.span_to_next_source(ident.span)
1426+
&& let Some(brace_offset) = {
1427+
// find the first `{`
1428+
let chars = next_source.chars().collect::<Vec<_>>();
1429+
if chars.len() < 2 || chars[0] != '!' {
1430+
None
1431+
} else {
1432+
let mut idx = 1;
1433+
while idx < chars.len() && chars[idx].is_ascii_whitespace() {
1434+
idx += 1;
1435+
}
1436+
(idx < chars.len() && chars[idx] == '{').then_some(idx)
1437+
}
1438+
}
1439+
{
1440+
let bang_pos = ident.span.hi().0;
1441+
let lo = bang_pos + 1; // the pos after `!`
1442+
let hi = bang_pos + brace_offset as u32; // the pos of '{'
1443+
let span = ident.span.with_hi(BytePos(hi)).with_lo(BytePos(lo));
1444+
err.subdiagnostic(MaybeMissingMacroRulesName { span });
1445+
return;
1446+
}
1447+
14241448
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
14251449
err.subdiagnostic(ExplicitUnsafeTraits { span: ident.span, ident });
14261450
return;
14271451
}
1452+
14281453
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
14291454
err.subdiagnostic(AddedMacroUse);
14301455
return;
14311456
}
1457+
14321458
if ident.name == kw::Default
14331459
&& let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind
14341460
{

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

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
macro_rules!{}
6+
//~^ ERROR cannot find macro `macro_rules` in this scope
7+
//~| NOTE maybe you have forgotten to define a name for this `macro_rules`
8+
9+
macro_rules! {}
10+
//~^ ERROR cannot find macro `macro_rules` in this scope
11+
//~| NOTE maybe you have forgotten to define a name for this `macro_rules`
12+
13+
macro_rules! {
14+
15+
}
16+
//~^^^ ERROR cannot find macro `macro_rules` in this scope
17+
//~| NOTE maybe you have forgotten to define a name for this `macro_rules`
18+
19+
macro_rules![];
20+
//~^ ERROR cannot find macro `macro_rules` in this scope
21+
macro_rules!();
22+
//~^ ERROR cannot find macro `macro_rules` in this scope
23+
24+
fn main() {}

tests/ui/resolve/issue-118295.stderr

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
error: cannot find macro `macro_rules` in this scope
2+
--> $DIR/issue-118295.rs:21:1
3+
|
4+
LL | macro_rules!();
5+
| ^^^^^^^^^^^
6+
7+
error: cannot find macro `macro_rules` in this scope
8+
--> $DIR/issue-118295.rs:19:1
9+
|
10+
LL | macro_rules![];
11+
| ^^^^^^^^^^^
12+
13+
error: cannot find macro `macro_rules` in this scope
14+
--> $DIR/issue-118295.rs:13:1
15+
|
16+
LL | macro_rules! {
17+
| ^^^^^^^^^^^
18+
|
19+
note: maybe you have forgotten to define a name for this `macro_rules`
20+
--> $DIR/issue-118295.rs:13:13
21+
|
22+
LL | macro_rules! {
23+
| ^
24+
25+
error: cannot find macro `macro_rules` in this scope
26+
--> $DIR/issue-118295.rs:9:1
27+
|
28+
LL | macro_rules! {}
29+
| ^^^^^^^^^^^
30+
|
31+
note: maybe you have forgotten to define a name for this `macro_rules`
32+
--> $DIR/issue-118295.rs:9:13
33+
|
34+
LL | macro_rules! {}
35+
| ^^
36+
37+
error: cannot find macro `macro_rules` in this scope
38+
--> $DIR/issue-118295.rs:5:1
39+
|
40+
LL | macro_rules!{}
41+
| ^^^^^^^^^^^
42+
|
43+
note: maybe you have forgotten to define a name for this `macro_rules`
44+
--> $DIR/issue-118295.rs:5:13
45+
|
46+
LL | macro_rules!{}
47+
| ^
48+
49+
error: cannot find macro `macro_rules` in this scope
50+
--> $DIR/issue-118295.rs:1:1
51+
|
52+
LL | macro_rules! {}
53+
| ^^^^^^^^^^^
54+
|
55+
note: maybe you have forgotten to define a name for this `macro_rules`
56+
--> $DIR/issue-118295.rs:1:13
57+
|
58+
LL | macro_rules! {}
59+
| ^
60+
61+
error: aborting due to 6 previous errors
62+

0 commit comments

Comments
 (0)