Skip to content

Commit ede713b

Browse files
committed
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 #124535 <#124535> = help: import `macro_rules` with `use` to make it callable above its definition = note: `#[warn(out_of_scope_macro_calls)]` on by default ```
1 parent 2a1c384 commit ede713b

File tree

7 files changed

+48
-26
lines changed

7 files changed

+48
-26
lines changed

compiler/rustc_lint/messages.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,8 @@ lint_opaque_hidden_inferred_bound_sugg = add this bound
624624
lint_or_patterns_back_compat = the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro
625625
.suggestion = use pat_param to preserve semantics
626626
627-
lint_out_of_scope_macro_calls = cannot find macro `{$path}` in this scope
627+
lint_out_of_scope_macro_calls = cannot find macro `{$path}` in {$scope}
628+
.label = not found in {$scope}
628629
.help = import `macro_rules` with `use` to make it callable above its definition
629630
630631
lint_overflowing_bin_hex = literal out of range for `{$ty}`

compiler/rustc_lint/src/context/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
434434
lints::InnerAttributeUnstable::CustomInnerAttribute
435435
}
436436
.decorate_lint(diag),
437-
BuiltinLintDiag::OutOfScopeMacroCalls { path } => {
438-
lints::OutOfScopeMacroCalls { path }.decorate_lint(diag)
437+
BuiltinLintDiag::OutOfScopeMacroCalls { span, path, scope } => {
438+
lints::OutOfScopeMacroCalls { span, path, scope }.decorate_lint(diag)
439439
}
440440
}
441441
}

compiler/rustc_lint/src/lints.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2951,5 +2951,8 @@ pub struct UnsafeAttrOutsideUnsafeSuggestion {
29512951
#[diag(lint_out_of_scope_macro_calls)]
29522952
#[help]
29532953
pub struct OutOfScopeMacroCalls {
2954+
#[label]
2955+
pub span: Span,
29542956
pub path: String,
2957+
pub scope: String,
29552958
}

compiler/rustc_lint_defs/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,9 @@ pub enum BuiltinLintDiag {
745745
is_macro: bool,
746746
},
747747
OutOfScopeMacroCalls {
748+
span: Span,
748749
path: String,
750+
scope: String,
749751
},
750752
}
751753

compiler/rustc_resolve/src/macros.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
862862
),
863863
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
864864
let mut suggestion = None;
865-
let (span, label, module) =
866-
if let PathResult::Failed { span, label, module, .. } = path_res {
865+
let (span, label, module, segment) =
866+
if let PathResult::Failed { span, label, module, segment_name, .. } =
867+
path_res
868+
{
867869
// try to suggest if it's not a macro, maybe a function
868870
if let PathResult::NonModule(partial_res) =
869871
self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope)
@@ -881,7 +883,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
881883
Applicability::MaybeIncorrect,
882884
));
883885
}
884-
(span, label, module)
886+
(span, label, module, segment_name)
885887
} else {
886888
(
887889
path_span,
@@ -891,12 +893,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
891893
kind.descr()
892894
),
893895
None,
896+
path.last().map(|segment| segment.ident.name).unwrap(),
894897
)
895898
};
896899
self.report_error(
897900
span,
898901
ResolutionError::FailedToResolve {
899-
segment: path.last().map(|segment| segment.ident.name),
902+
segment: Some(segment),
900903
label,
901904
suggestion,
902905
module,
@@ -1067,11 +1070,24 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
10671070
None,
10681071
);
10691072
if fallback_binding.ok().and_then(|b| b.res().opt_def_id()) != Some(def_id) {
1073+
let scope = 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+
scope,
1090+
},
10751091
);
10761092
}
10771093
}

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 { () => { "" } }

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 crate root
130130
--> $DIR/key-value-expansion-scope.rs:1:10
131131
|
132132
LL | #![doc = in_root!()]
133-
| ^^^^^^^
133+
| ^^^^^^^ not found in 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 crate root
141141
--> $DIR/key-value-expansion-scope.rs:4:10
142142
|
143143
LL | #![doc = in_mod_escape!()]
144-
| ^^^^^^^^^^^^^
144+
| ^^^^^^^^^^^^^ not found in 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 module `macros_stay`
151151
--> $DIR/key-value-expansion-scope.rs:21:9
152152
|
153153
LL | #[doc = in_mod!()]
154-
| ^^^^^^
154+
| ^^^^^^ not found in 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 module `macros_stay`
161161
--> $DIR/key-value-expansion-scope.rs:24:14
162162
|
163163
LL | #![doc = in_mod!()]
164-
| ^^^^^^
164+
| ^^^^^^ not found in 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 module `macros_escape`
171171
--> $DIR/key-value-expansion-scope.rs:36:9
172172
|
173173
LL | #[doc = in_mod_escape!()]
174-
| ^^^^^^^^^^^^^
174+
| ^^^^^^^^^^^^^ not found in 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 module `macros_escape`
181181
--> $DIR/key-value-expansion-scope.rs:39:14
182182
|
183183
LL | #![doc = in_mod_escape!()]
184-
| ^^^^^^^^^^^^^
184+
| ^^^^^^^^^^^^^ not found in 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)