Skip to content

Commit 12e6b48

Browse files
authored
Rollup merge of rust-lang#128080 - estebank:out-of-scope-macro, r=petrochenkov
Specify scope in `out_of_scope_macro_calls` lint ``` warning: cannot find macro `in_root` in the crate root --> $DIR/key-value-expansion-scope.rs:1:10 | LL | #![doc = in_root!()] | ^^^^^^^ not found in the crate root | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue rust-lang#124535 <rust-lang#124535> = help: import `macro_rules` with `use` to make it callable above its definition = note: `#[warn(out_of_scope_macro_calls)]` on by default ``` r? ```@petrochenkov```
2 parents a18bd8a + 835d434 commit 12e6b48

File tree

7 files changed

+48
-26
lines changed

7 files changed

+48
-26
lines changed

Diff for: compiler/rustc_lint/messages.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ lint_opaque_hidden_inferred_bound_sugg = add this bound
630630
lint_or_patterns_back_compat = the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro
631631
.suggestion = use pat_param to preserve semantics
632632
633-
lint_out_of_scope_macro_calls = cannot find macro `{$path}` in this scope
633+
lint_out_of_scope_macro_calls = cannot find macro `{$path}` in the current scope when looking from {$location}
634+
.label = not found from {$location}
634635
.help = import `macro_rules` with `use` to make it callable above its definition
635636
636637
lint_overflowing_bin_hex = literal out of range for `{$ty}`

Diff for: compiler/rustc_lint/src/early/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,8 @@ pub(super) fn decorate_lint(
444444
lints::InnerAttributeUnstable::CustomInnerAttribute
445445
}
446446
.decorate_lint(diag),
447-
BuiltinLintDiag::OutOfScopeMacroCalls { path } => {
448-
lints::OutOfScopeMacroCalls { path }.decorate_lint(diag)
447+
BuiltinLintDiag::OutOfScopeMacroCalls { span, path, location } => {
448+
lints::OutOfScopeMacroCalls { span, path, location }.decorate_lint(diag)
449449
}
450450
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by } => {
451451
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)

Diff for: compiler/rustc_lint/src/lints.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,10 @@ pub(crate) struct UnsafeAttrOutsideUnsafeSuggestion {
31083108
#[diag(lint_out_of_scope_macro_calls)]
31093109
#[help]
31103110
pub(crate) struct OutOfScopeMacroCalls {
3111+
#[label]
3112+
pub span: Span,
31113113
pub path: String,
3114+
pub location: String,
31123115
}
31133116

31143117
#[derive(LintDiagnostic)]

Diff for: compiler/rustc_lint_defs/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,9 @@ pub enum BuiltinLintDiag {
819819
is_macro: bool,
820820
},
821821
OutOfScopeMacroCalls {
822+
span: Span,
822823
path: String,
824+
location: String,
823825
},
824826
UnexpectedBuiltinCfg {
825827
cfg: String,

Diff for: compiler/rustc_resolve/src/macros.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
857857
),
858858
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
859859
let mut suggestion = None;
860-
let (span, label, module) =
861-
if let PathResult::Failed { span, label, module, .. } = path_res {
860+
let (span, label, module, segment) =
861+
if let PathResult::Failed { span, label, module, segment_name, .. } =
862+
path_res
863+
{
862864
// try to suggest if it's not a macro, maybe a function
863865
if let PathResult::NonModule(partial_res) =
864866
self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope, None)
@@ -876,7 +878,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
876878
Applicability::MaybeIncorrect,
877879
));
878880
}
879-
(span, label, module)
881+
(span, label, module, segment_name)
880882
} else {
881883
(
882884
path_span,
@@ -886,12 +888,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
886888
kind.descr()
887889
),
888890
None,
891+
path.last().map(|segment| segment.ident.name).unwrap(),
889892
)
890893
};
891894
self.report_error(
892895
span,
893896
ResolutionError::FailedToResolve {
894-
segment: path.last().map(|segment| segment.ident.name),
897+
segment: Some(segment),
895898
label,
896899
suggestion,
897900
module,
@@ -1067,11 +1070,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10671070
None,
10681071
);
10691072
if fallback_binding.ok().and_then(|b| b.res().opt_def_id()) != Some(def_id) {
1073+
let location = match parent_scope.module.kind {
1074+
ModuleKind::Def(_, _, name) if name == kw::Empty => {
1075+
"the crate root".to_string()
1076+
}
1077+
ModuleKind::Def(kind, def_id, name) => {
1078+
format!("{} `{name}`", kind.descr(def_id))
1079+
}
1080+
ModuleKind::Block => "this scope".to_string(),
1081+
};
10701082
self.tcx.sess.psess.buffer_lint(
10711083
OUT_OF_SCOPE_MACRO_CALLS,
10721084
path.span,
10731085
node_id,
1074-
BuiltinLintDiag::OutOfScopeMacroCalls { path: pprust::path_to_string(path) },
1086+
BuiltinLintDiag::OutOfScopeMacroCalls {
1087+
span: path.span,
1088+
path: pprust::path_to_string(path),
1089+
location,
1090+
},
10751091
);
10761092
}
10771093
}

Diff for: tests/ui/attributes/key-value-expansion-scope.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#![doc = in_root!()] //~ WARN cannot find macro `in_root` in this scope
1+
#![doc = in_root!()] //~ WARN cannot find macro `in_root`
22
//~| WARN this was previously accepted by the compiler
33
#![doc = in_mod!()] //~ ERROR cannot find macro `in_mod` in this scope
4-
#![doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape` in this scope
4+
#![doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape`
55
//~| WARN this was previously accepted by the compiler
66
#![doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope
77

@@ -18,10 +18,10 @@ fn before() {
1818

1919
macro_rules! in_root { () => { "" } }
2020

21-
#[doc = in_mod!()] //~ WARN cannot find macro `in_mod` in this scope
21+
#[doc = in_mod!()] //~ WARN cannot find macro `in_mod`
2222
//~| WARN this was previously accepted by the compiler
2323
mod macros_stay {
24-
#![doc = in_mod!()] //~ WARN cannot find macro `in_mod` in this scope
24+
#![doc = in_mod!()] //~ WARN cannot find macro `in_mod`
2525
//~| WARN this was previously accepted by the compiler
2626

2727
macro_rules! in_mod { () => { "" } }
@@ -33,10 +33,10 @@ mod macros_stay {
3333
}
3434

3535
#[macro_use]
36-
#[doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape` in this scope
36+
#[doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape`
3737
//~| WARN this was previously accepted by the compiler
3838
mod macros_escape {
39-
#![doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape` in this scope
39+
#![doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape`
4040
//~| WARN this was previously accepted by the compiler
4141

4242
macro_rules! in_mod_escape { () => { "" } }

Diff for: tests/ui/attributes/key-value-expansion-scope.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -126,62 +126,62 @@ LL | #![doc = in_block!()]
126126
|
127127
= help: have you added the `#[macro_use]` on the module/import?
128128

129-
warning: cannot find macro `in_root` in this scope
129+
warning: cannot find macro `in_root` in the current scope when looking from the crate root
130130
--> $DIR/key-value-expansion-scope.rs:1:10
131131
|
132132
LL | #![doc = in_root!()]
133-
| ^^^^^^^
133+
| ^^^^^^^ not found from the crate root
134134
|
135135
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
136136
= note: for more information, see issue #124535 <https://github.com/rust-lang/rust/issues/124535>
137137
= help: import `macro_rules` with `use` to make it callable above its definition
138138
= note: `#[warn(out_of_scope_macro_calls)]` on by default
139139

140-
warning: cannot find macro `in_mod_escape` in this scope
140+
warning: cannot find macro `in_mod_escape` in the current scope when looking from the crate root
141141
--> $DIR/key-value-expansion-scope.rs:4:10
142142
|
143143
LL | #![doc = in_mod_escape!()]
144-
| ^^^^^^^^^^^^^
144+
| ^^^^^^^^^^^^^ not found from the crate root
145145
|
146146
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
147147
= note: for more information, see issue #124535 <https://github.com/rust-lang/rust/issues/124535>
148148
= help: import `macro_rules` with `use` to make it callable above its definition
149149

150-
warning: cannot find macro `in_mod` in this scope
150+
warning: cannot find macro `in_mod` in the current scope when looking from module `macros_stay`
151151
--> $DIR/key-value-expansion-scope.rs:21:9
152152
|
153153
LL | #[doc = in_mod!()]
154-
| ^^^^^^
154+
| ^^^^^^ not found from module `macros_stay`
155155
|
156156
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
157157
= note: for more information, see issue #124535 <https://github.com/rust-lang/rust/issues/124535>
158158
= help: import `macro_rules` with `use` to make it callable above its definition
159159

160-
warning: cannot find macro `in_mod` in this scope
160+
warning: cannot find macro `in_mod` in the current scope when looking from module `macros_stay`
161161
--> $DIR/key-value-expansion-scope.rs:24:14
162162
|
163163
LL | #![doc = in_mod!()]
164-
| ^^^^^^
164+
| ^^^^^^ not found from module `macros_stay`
165165
|
166166
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
167167
= note: for more information, see issue #124535 <https://github.com/rust-lang/rust/issues/124535>
168168
= help: import `macro_rules` with `use` to make it callable above its definition
169169

170-
warning: cannot find macro `in_mod_escape` in this scope
170+
warning: cannot find macro `in_mod_escape` in the current scope when looking from module `macros_escape`
171171
--> $DIR/key-value-expansion-scope.rs:36:9
172172
|
173173
LL | #[doc = in_mod_escape!()]
174-
| ^^^^^^^^^^^^^
174+
| ^^^^^^^^^^^^^ not found from module `macros_escape`
175175
|
176176
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
177177
= note: for more information, see issue #124535 <https://github.com/rust-lang/rust/issues/124535>
178178
= help: import `macro_rules` with `use` to make it callable above its definition
179179

180-
warning: cannot find macro `in_mod_escape` in this scope
180+
warning: cannot find macro `in_mod_escape` in the current scope when looking from module `macros_escape`
181181
--> $DIR/key-value-expansion-scope.rs:39:14
182182
|
183183
LL | #![doc = in_mod_escape!()]
184-
| ^^^^^^^^^^^^^
184+
| ^^^^^^^^^^^^^ not found from module `macros_escape`
185185
|
186186
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
187187
= note: for more information, see issue #124535 <https://github.com/rust-lang/rust/issues/124535>

0 commit comments

Comments
 (0)