Skip to content

Commit

Permalink
tip for define macro name after macro_rules!
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Dec 3, 2023
1 parent 8b6a4a9 commit 9425884
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 4 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ resolve_method_not_member_of_trait =
method `{$method}` is not a member of trait `{$trait_}`
.label = not a member of trait `{$trait_}`
resolve_missing_macro_rules_name = maybe you have forgotten to define a name for this `macro_rules!`
resolve_module_only =
visibility must resolve to a module
Expand Down
21 changes: 17 additions & 4 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, Span, SyntaxContext};
use thin_vec::{thin_vec, ThinVec};

use crate::errors::{
AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
ExplicitUnsafeTraits,
};
use crate::errors::{AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion};
use crate::errors::{ConsiderAddingADerive, ExplicitUnsafeTraits, MaybeMissingMacroRulesName};
use crate::imports::{Import, ImportKind};
use crate::late::{PatternSource, Rib};
use crate::path_names_to_string;
Expand Down Expand Up @@ -1421,14 +1419,29 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
"",
);

if macro_kind == MacroKind::Bang
&& ident.name == sym::macro_rules
&& let source_map = self.tcx.sess.source_map()
&& let Ok(next_source) = source_map.span_to_next_source(ident.span)
&& next_source.starts_with("!")
&& let next = ident.span.with_hi(ident.span.hi() + BytePos(1))
&& let Ok(span) = source_map.span_extend_while(next, |c| c.is_whitespace() || c == '{')
&& next != span
{
err.subdiagnostic(MaybeMissingMacroRulesName { span: next });
return;
}

if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
err.subdiagnostic(ExplicitUnsafeTraits { span: ident.span, ident });
return;
}

if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
err.subdiagnostic(AddedMacroUse);
return;
}

if ident.name == kw::Default
&& let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind
{
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,13 @@ pub(crate) struct ExplicitUnsafeTraits {
pub(crate) ident: Ident,
}

#[derive(Subdiagnostic)]
#[note(resolve_missing_macro_rules_name)]
pub(crate) struct MaybeMissingMacroRulesName {
#[primary_span]
pub(crate) span: Span,
}

#[derive(Subdiagnostic)]
#[help(resolve_added_macro_use)]
pub(crate) struct AddedMacroUse;
Expand Down
24 changes: 24 additions & 0 deletions tests/ui/resolve/issue-118295.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
macro_rules! {}
//~^ ERROR cannot find macro `macro_rules` in this scope
//~| NOTE maybe you have forgotten to define a name for this `macro_rules!`

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

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

macro_rules! {

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

macro_rules![];
//~^ ERROR cannot find macro `macro_rules` in this scope
macro_rules!();
//~^ ERROR cannot find macro `macro_rules` in this scope

fn main() {}
62 changes: 62 additions & 0 deletions tests/ui/resolve/issue-118295.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
error: cannot find macro `macro_rules` in this scope
--> $DIR/issue-118295.rs:21:1
|
LL | macro_rules!();
| ^^^^^^^^^^^

error: cannot find macro `macro_rules` in this scope
--> $DIR/issue-118295.rs:19:1
|
LL | macro_rules![];
| ^^^^^^^^^^^

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

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

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

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:1
|
LL | macro_rules! {}
| ^^^^^^^^^^^^

error: aborting due to 6 previous errors

0 comments on commit 9425884

Please sign in to comment.