Skip to content
/ rust Public
forked from rust-lang/rust

Commit 032f19d

Browse files
authored
Rollup merge of rust-lang#121130 - chenyukang:yukang-fix-121061-macro-later, r=matthiaskrgr
Suggest moving definition if non-found macro_rules! is defined later Fixes rust-lang#121061
2 parents 7b29e65 + 75a4fa1 commit 032f19d

7 files changed

+94
-1
lines changed

compiler/rustc_resolve/messages.ftl

+7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ resolve_consider_declaring_with_pub =
8787
resolve_consider_marking_as_pub =
8888
consider marking `{$ident}` as `pub` in the imported module
8989
90+
resolve_consider_move_macro_position =
91+
consider moving the definition of `{$ident}` before this call
92+
93+
9094
resolve_const_not_member_of_trait =
9195
const `{$const_}` is not a member of trait `{$trait_}`
9296
.label = not a member of trait `{$trait_}`
@@ -186,6 +190,9 @@ resolve_lowercase_self =
186190
attempt to use a non-constant value in a constant
187191
.suggestion = try using `Self`
188192
193+
resolve_macro_defined_later =
194+
a macro with the same name exists, but it appears later at here
195+
189196
resolve_macro_expected_found =
190197
expected {$expected}, found {$found} `{$macro_path}`
191198

compiler/rustc_resolve/src/diagnostics.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ use rustc_span::{BytePos, Span, SyntaxContext};
3030
use thin_vec::{thin_vec, ThinVec};
3131

3232
use crate::errors::{AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion};
33-
use crate::errors::{ConsiderAddingADerive, ExplicitUnsafeTraits, MaybeMissingMacroRulesName};
33+
use crate::errors::{
34+
ConsiderAddingADerive, ExplicitUnsafeTraits, MacroDefinedLater, MacroSuggMovePosition,
35+
MaybeMissingMacroRulesName,
36+
};
3437
use crate::imports::{Import, ImportKind};
3538
use crate::late::{PatternSource, Rib};
3639
use crate::{errors as errs, BindingKey};
@@ -1456,6 +1459,24 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14561459
return;
14571460
}
14581461

1462+
let unused_macro = self.unused_macros.iter().find_map(|(def_id, (_, unused_ident))| {
1463+
if unused_ident.name == ident.name {
1464+
Some((def_id.clone(), unused_ident.clone()))
1465+
} else {
1466+
None
1467+
}
1468+
});
1469+
1470+
if let Some((def_id, unused_ident)) = unused_macro {
1471+
let scope = self.local_macro_def_scopes[&def_id];
1472+
let parent_nearest = parent_scope.module.nearest_parent_mod();
1473+
if Some(parent_nearest) == scope.opt_def_id() {
1474+
err.subdiagnostic(self.dcx(), MacroDefinedLater { span: unused_ident.span });
1475+
err.subdiagnostic(self.dcx(), MacroSuggMovePosition { span: ident.span, ident });
1476+
return;
1477+
}
1478+
}
1479+
14591480
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
14601481
err.subdiagnostic(self.dcx(), AddedMacroUse);
14611482
return;

compiler/rustc_resolve/src/errors.rs

+15
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,21 @@ pub(crate) struct ExplicitUnsafeTraits {
685685
pub(crate) ident: Ident,
686686
}
687687

688+
#[derive(Subdiagnostic)]
689+
#[note(resolve_macro_defined_later)]
690+
pub(crate) struct MacroDefinedLater {
691+
#[primary_span]
692+
pub(crate) span: Span,
693+
}
694+
695+
#[derive(Subdiagnostic)]
696+
#[label(resolve_consider_move_macro_position)]
697+
pub(crate) struct MacroSuggMovePosition {
698+
#[primary_span]
699+
pub(crate) span: Span,
700+
pub(crate) ident: Ident,
701+
}
702+
688703
#[derive(Subdiagnostic)]
689704
#[note(resolve_missing_macro_rules_name)]
690705
pub(crate) struct MaybeMissingMacroRulesName {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mod demo {
2+
fn hello() {
3+
something_later!(); //~ ERROR cannot find macro `something_later` in this scope
4+
}
5+
6+
macro_rules! something_later {
7+
() => {
8+
println!("successfully expanded!");
9+
};
10+
}
11+
}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot find macro `something_later` in this scope
2+
--> $DIR/issue-121061-defined-later-2.rs:3:9
3+
|
4+
LL | something_later!();
5+
| ^^^^^^^^^^^^^^^ consider moving the definition of `something_later` before this call
6+
|
7+
note: a macro with the same name exists, but it appears later at here
8+
--> $DIR/issue-121061-defined-later-2.rs:6:18
9+
|
10+
LL | macro_rules! something_later {
11+
| ^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
something_later!(); //~ ERROR cannot find macro `something_later` in this scope
3+
}
4+
5+
macro_rules! something_later {
6+
() => {
7+
println!("successfully expanded!");
8+
};
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot find macro `something_later` in this scope
2+
--> $DIR/issue-121061-defined-later.rs:2:5
3+
|
4+
LL | something_later!();
5+
| ^^^^^^^^^^^^^^^ consider moving the definition of `something_later` before this call
6+
|
7+
note: a macro with the same name exists, but it appears later at here
8+
--> $DIR/issue-121061-defined-later.rs:5:14
9+
|
10+
LL | macro_rules! something_later {
11+
| ^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+

0 commit comments

Comments
 (0)