Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 283d741

Browse files
authoredMar 4, 2024
Unrolled build for rust-lang#121130
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 f7cb53e + 08fa734 commit 283d741

7 files changed

+94
-1
lines changed
 

‎compiler/rustc_resolve/messages.ftl

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ resolve_consider_declaring_with_pub =
8383
resolve_consider_marking_as_pub =
8484
consider marking `{$ident}` as `pub` in the imported module
8585
86+
resolve_consider_move_macro_position =
87+
consider moving the definition of `{$ident}` before this call
88+
89+
8690
resolve_const_not_member_of_trait =
8791
const `{$const_}` is not a member of trait `{$trait_}`
8892
.label = not a member of trait `{$trait_}`
@@ -176,6 +180,9 @@ resolve_lowercase_self =
176180
attempt to use a non-constant value in a constant
177181
.suggestion = try using `Self`
178182
183+
resolve_macro_defined_later =
184+
a macro with the same name exists, but it appears later at here
185+
179186
resolve_macro_expected_found =
180187
expected {$expected}, found {$found} `{$macro_path}`
181188

‎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
@@ -646,6 +646,21 @@ pub(crate) struct ExplicitUnsafeTraits {
646646
pub(crate) ident: Ident,
647647
}
648648

649+
#[derive(Subdiagnostic)]
650+
#[note(resolve_macro_defined_later)]
651+
pub(crate) struct MacroDefinedLater {
652+
#[primary_span]
653+
pub(crate) span: Span,
654+
}
655+
656+
#[derive(Subdiagnostic)]
657+
#[label(resolve_consider_move_macro_position)]
658+
pub(crate) struct MacroSuggMovePosition {
659+
#[primary_span]
660+
pub(crate) span: Span,
661+
pub(crate) ident: Ident,
662+
}
663+
649664
#[derive(Subdiagnostic)]
650665
#[note(resolve_missing_macro_rules_name)]
651666
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/defined-later-issue-121061-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/defined-later-issue-121061-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/defined-later-issue-121061.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/defined-later-issue-121061.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)
Failed to load comments.