Skip to content

Commit

Permalink
Specify scope in out_of_scope_macro_calls lint
Browse files Browse the repository at this point in the history
```
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
```
  • Loading branch information
estebank committed Jul 23, 2024
1 parent 2a1c384 commit ede713b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,8 @@ lint_opaque_hidden_inferred_bound_sugg = add this bound
lint_or_patterns_back_compat = the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro
.suggestion = use pat_param to preserve semantics
lint_out_of_scope_macro_calls = cannot find macro `{$path}` in this scope
lint_out_of_scope_macro_calls = cannot find macro `{$path}` in {$scope}
.label = not found in {$scope}
.help = import `macro_rules` with `use` to make it callable above its definition
lint_overflowing_bin_hex = literal out of range for `{$ty}`
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
lints::InnerAttributeUnstable::CustomInnerAttribute
}
.decorate_lint(diag),
BuiltinLintDiag::OutOfScopeMacroCalls { path } => {
lints::OutOfScopeMacroCalls { path }.decorate_lint(diag)
BuiltinLintDiag::OutOfScopeMacroCalls { span, path, scope } => {
lints::OutOfScopeMacroCalls { span, path, scope }.decorate_lint(diag)
}
}
}
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2951,5 +2951,8 @@ pub struct UnsafeAttrOutsideUnsafeSuggestion {
#[diag(lint_out_of_scope_macro_calls)]
#[help]
pub struct OutOfScopeMacroCalls {
#[label]
pub span: Span,
pub path: String,
pub scope: String,
}
2 changes: 2 additions & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,9 @@ pub enum BuiltinLintDiag {
is_macro: bool,
},
OutOfScopeMacroCalls {
span: Span,
path: String,
scope: String,
},
}

Expand Down
26 changes: 21 additions & 5 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
),
path_res @ (PathResult::NonModule(..) | PathResult::Failed { .. }) => {
let mut suggestion = None;
let (span, label, module) =
if let PathResult::Failed { span, label, module, .. } = path_res {
let (span, label, module, segment) =
if let PathResult::Failed { span, label, module, segment_name, .. } =
path_res
{
// try to suggest if it's not a macro, maybe a function
if let PathResult::NonModule(partial_res) =
self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope)
Expand All @@ -881,7 +883,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Applicability::MaybeIncorrect,
));
}
(span, label, module)
(span, label, module, segment_name)
} else {
(
path_span,
Expand All @@ -891,12 +893,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
kind.descr()
),
None,
path.last().map(|segment| segment.ident.name).unwrap(),
)
};
self.report_error(
span,
ResolutionError::FailedToResolve {
segment: path.last().map(|segment| segment.ident.name),
segment: Some(segment),
label,
suggestion,
module,
Expand Down Expand Up @@ -1067,11 +1070,24 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
None,
);
if fallback_binding.ok().and_then(|b| b.res().opt_def_id()) != Some(def_id) {
let scope = match parent_scope.module.kind {
ModuleKind::Def(_, _, name) if name == kw::Empty => {
"the crate root".to_string()
}
ModuleKind::Def(kind, def_id, name) => {
format!("{} `{name}`", kind.descr(def_id))
}
ModuleKind::Block => "this scope".to_string(),
};
self.tcx.sess.psess.buffer_lint(
OUT_OF_SCOPE_MACRO_CALLS,
path.span,
node_id,
BuiltinLintDiag::OutOfScopeMacroCalls { path: pprust::path_to_string(path) },
BuiltinLintDiag::OutOfScopeMacroCalls {
span: path.span,
path: pprust::path_to_string(path),
scope,
},
);
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/attributes/key-value-expansion-scope.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![doc = in_root!()] //~ WARN cannot find macro `in_root` in this scope
#![doc = in_root!()] //~ WARN cannot find macro `in_root`
//~| WARN this was previously accepted by the compiler
#![doc = in_mod!()] //~ ERROR cannot find macro `in_mod` in this scope
#![doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape` in this scope
#![doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape`
//~| WARN this was previously accepted by the compiler
#![doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope

Expand All @@ -18,10 +18,10 @@ fn before() {

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

#[doc = in_mod!()] //~ WARN cannot find macro `in_mod` in this scope
#[doc = in_mod!()] //~ WARN cannot find macro `in_mod`
//~| WARN this was previously accepted by the compiler
mod macros_stay {
#![doc = in_mod!()] //~ WARN cannot find macro `in_mod` in this scope
#![doc = in_mod!()] //~ WARN cannot find macro `in_mod`
//~| WARN this was previously accepted by the compiler

macro_rules! in_mod { () => { "" } }
Expand All @@ -33,10 +33,10 @@ mod macros_stay {
}

#[macro_use]
#[doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape` in this scope
#[doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape`
//~| WARN this was previously accepted by the compiler
mod macros_escape {
#![doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape` in this scope
#![doc = in_mod_escape!()] //~ WARN cannot find macro `in_mod_escape`
//~| WARN this was previously accepted by the compiler

macro_rules! in_mod_escape { () => { "" } }
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/attributes/key-value-expansion-scope.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -126,62 +126,62 @@ LL | #![doc = in_block!()]
|
= help: have you added the `#[macro_use]` on the module/import?

warning: cannot find macro `in_root` in this scope
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 <https://github.com/rust-lang/rust/issues/124535>
= help: import `macro_rules` with `use` to make it callable above its definition
= note: `#[warn(out_of_scope_macro_calls)]` on by default

warning: cannot find macro `in_mod_escape` in this scope
warning: cannot find macro `in_mod_escape` in the crate root
--> $DIR/key-value-expansion-scope.rs:4:10
|
LL | #![doc = in_mod_escape!()]
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ 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 <https://github.com/rust-lang/rust/issues/124535>
= help: import `macro_rules` with `use` to make it callable above its definition

warning: cannot find macro `in_mod` in this scope
warning: cannot find macro `in_mod` in module `macros_stay`
--> $DIR/key-value-expansion-scope.rs:21:9
|
LL | #[doc = in_mod!()]
| ^^^^^^
| ^^^^^^ not found in module `macros_stay`
|
= 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 <https://github.com/rust-lang/rust/issues/124535>
= help: import `macro_rules` with `use` to make it callable above its definition

warning: cannot find macro `in_mod` in this scope
warning: cannot find macro `in_mod` in module `macros_stay`
--> $DIR/key-value-expansion-scope.rs:24:14
|
LL | #![doc = in_mod!()]
| ^^^^^^
| ^^^^^^ not found in module `macros_stay`
|
= 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 <https://github.com/rust-lang/rust/issues/124535>
= help: import `macro_rules` with `use` to make it callable above its definition

warning: cannot find macro `in_mod_escape` in this scope
warning: cannot find macro `in_mod_escape` in module `macros_escape`
--> $DIR/key-value-expansion-scope.rs:36:9
|
LL | #[doc = in_mod_escape!()]
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ not found in module `macros_escape`
|
= 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 <https://github.com/rust-lang/rust/issues/124535>
= help: import `macro_rules` with `use` to make it callable above its definition

warning: cannot find macro `in_mod_escape` in this scope
warning: cannot find macro `in_mod_escape` in module `macros_escape`
--> $DIR/key-value-expansion-scope.rs:39:14
|
LL | #![doc = in_mod_escape!()]
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ not found in module `macros_escape`
|
= 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 <https://github.com/rust-lang/rust/issues/124535>
Expand Down

0 comments on commit ede713b

Please sign in to comment.