Skip to content

Commit 4de26d7

Browse files
committed
Add scope context to unresolved macros
1 parent c577764 commit 4de26d7

File tree

62 files changed

+337
-317
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+337
-317
lines changed

compiler/rustc_lint/messages.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,8 @@ lint_opaque_hidden_inferred_bound_sugg = add this bound
604604
lint_or_patterns_back_compat = the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro
605605
.suggestion = use pat_param to preserve semantics
606606
607-
lint_out_of_scope_macro_calls = cannot find macro `{$path}` in this scope
608-
.label = not found in this scope
607+
lint_out_of_scope_macro_calls = cannot find macro `{$path}` in {$scope}
608+
.label = not found in {$scope}
609609
.help = import `macro_rules` with `use` to make it callable above its definition
610610
611611
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 { span, path } => {
438-
lints::OutOfScopeMacroCalls { span, 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

+1
Original file line numberDiff line numberDiff line change
@@ -2919,4 +2919,5 @@ pub struct OutOfScopeMacroCalls {
29192919
#[label]
29202920
pub span: Span,
29212921
pub path: String,
2922+
pub scope: String,
29222923
}

compiler/rustc_lint_defs/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ pub enum BuiltinLintDiag {
747747
OutOfScopeMacroCalls {
748748
span: Span,
749749
path: String,
750+
scope: String,
750751
},
751752
}
752753

compiler/rustc_resolve/messages.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ resolve_cannot_determine_macro_resolution =
8080
resolve_cannot_find_builtin_macro_with_name =
8181
cannot find a built-in macro with name `{$ident}`
8282
83-
resolve_cannot_find_ident_in_this_scope = cannot find {$expected} `{$ident}` in this scope
84-
.label = not found in this scope
83+
resolve_cannot_find_ident_in_this_scope = cannot find {$expected} `{$ident}` in {$scope}
84+
.label = not found in {$scope}
8585
8686
resolve_cannot_glob_import_possible_crates =
8787
cannot glob-import all possible crates

compiler/rustc_resolve/src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ pub(crate) struct CannotFindIdentInThisScope<'a> {
638638
#[label]
639639
pub(crate) span: Span,
640640
pub(crate) expected: &'a str,
641+
pub(crate) scope: &'a str,
641642
pub(crate) ident: Ident,
642643
}
643644

compiler/rustc_resolve/src/macros.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -949,10 +949,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
949949
}
950950
Err(..) => {
951951
let expected = kind.descr_expected();
952-
952+
let scope = match parent_scope.module.kind {
953+
ModuleKind::Def(_, _, name) if name == kw::Empty => "the crate root",
954+
ModuleKind::Def(kind, def_id, name) => {
955+
&format!("{} `{name}`", kind.descr(def_id))
956+
}
957+
ModuleKind::Block => "this scope",
958+
};
953959
let mut err = self.dcx().create_err(CannotFindIdentInThisScope {
954960
span: ident.span,
955961
expected,
962+
scope,
956963
ident,
957964
});
958965
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident, krate);
@@ -1076,13 +1083,23 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
10761083
None,
10771084
);
10781085
if fallback_binding.ok().and_then(|b| b.res().opt_def_id()) != Some(def_id) {
1086+
let scope = match parent_scope.module.kind {
1087+
ModuleKind::Def(_, _, name) if name == kw::Empty => {
1088+
"the crate root".to_string()
1089+
}
1090+
ModuleKind::Def(kind, def_id, name) => {
1091+
format!("{} `{name}`", kind.descr(def_id))
1092+
}
1093+
ModuleKind::Block => "this scope".to_string(),
1094+
};
10791095
self.tcx.sess.psess.buffer_lint(
10801096
OUT_OF_SCOPE_MACRO_CALLS,
10811097
path.span,
10821098
node_id,
10831099
BuiltinLintDiag::OutOfScopeMacroCalls {
10841100
span: path.span,
10851101
path: pprust::path_to_string(path),
1102+
scope,
10861103
},
10871104
);
10881105
}

tests/ui/attributes/issue-90873.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ error: attribute value must be a literal
1010
LL | #![a={impl std::ops::Neg for i8 {}}]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: cannot find attribute `u` in this scope
13+
error: cannot find attribute `u` in the crate root
1414
--> $DIR/issue-90873.rs:1:4
1515
|
1616
LL | #![u=||{static d=||1;}]
17-
| ^ not found in this scope
17+
| ^ not found in the crate root
1818

19-
error: cannot find attribute `a` in this scope
19+
error: cannot find attribute `a` in the crate root
2020
--> $DIR/issue-90873.rs:6:4
2121
|
2222
LL | #![a={impl std::ops::Neg for i8 {}}]
23-
| ^ not found in this scope
23+
| ^ not found in the crate root
2424

2525
error[E0601]: `main` function not found in crate `issue_90873`
2626
--> $DIR/issue-90873.rs:6:37

tests/ui/attributes/key-value-expansion-scope.stderr

+44-44
Original file line numberDiff line numberDiff line change
@@ -1,187 +1,187 @@
1-
error: cannot find macro `in_mod` in this scope
1+
error: cannot find macro `in_mod` in the crate root
22
--> $DIR/key-value-expansion-scope.rs:3:10
33
|
44
LL | #![doc = in_mod!()]
5-
| ^^^^^^ not found in this scope
5+
| ^^^^^^ not found in the crate root
66
|
77
= help: have you added the `#[macro_use]` on the module/import?
88

9-
error: cannot find macro `in_block` in this scope
9+
error: cannot find macro `in_block` in the crate root
1010
--> $DIR/key-value-expansion-scope.rs:6:10
1111
|
1212
LL | #![doc = in_block!()]
13-
| ^^^^^^^^ not found in this scope
13+
| ^^^^^^^^ not found in the crate root
1414
|
1515
= help: have you added the `#[macro_use]` on the module/import?
1616

17-
error: cannot find macro `in_root` in this scope
17+
error: cannot find macro `in_root` in the crate root
1818
--> $DIR/key-value-expansion-scope.rs:8:9
1919
|
2020
LL | #[doc = in_root!()]
21-
| ^^^^^^^ not found in this scope
21+
| ^^^^^^^ not found in the crate root
2222
|
2323
= help: have you added the `#[macro_use]` on the module/import?
2424

25-
error: cannot find macro `in_mod` in this scope
25+
error: cannot find macro `in_mod` in the crate root
2626
--> $DIR/key-value-expansion-scope.rs:9:9
2727
|
2828
LL | #[doc = in_mod!()]
29-
| ^^^^^^ not found in this scope
29+
| ^^^^^^ not found in the crate root
3030
|
3131
= help: have you added the `#[macro_use]` on the module/import?
3232

33-
error: cannot find macro `in_mod_escape` in this scope
33+
error: cannot find macro `in_mod_escape` in the crate root
3434
--> $DIR/key-value-expansion-scope.rs:10:9
3535
|
3636
LL | #[doc = in_mod_escape!()]
37-
| ^^^^^^^^^^^^^ not found in this scope
37+
| ^^^^^^^^^^^^^ not found in the crate root
3838
|
3939
= help: have you added the `#[macro_use]` on the module/import?
4040

41-
error: cannot find macro `in_block` in this scope
41+
error: cannot find macro `in_block` in the crate root
4242
--> $DIR/key-value-expansion-scope.rs:11:9
4343
|
4444
LL | #[doc = in_block!()]
45-
| ^^^^^^^^ not found in this scope
45+
| ^^^^^^^^ not found in the crate root
4646
|
4747
= help: have you added the `#[macro_use]` on the module/import?
4848

49-
error: cannot find macro `in_root` in this scope
49+
error: cannot find macro `in_root` in the crate root
5050
--> $DIR/key-value-expansion-scope.rs:13:14
5151
|
5252
LL | #![doc = in_root!()]
53-
| ^^^^^^^ not found in this scope
53+
| ^^^^^^^ not found in the crate root
5454
|
5555
= help: have you added the `#[macro_use]` on the module/import?
5656

57-
error: cannot find macro `in_mod` in this scope
57+
error: cannot find macro `in_mod` in the crate root
5858
--> $DIR/key-value-expansion-scope.rs:14:14
5959
|
6060
LL | #![doc = in_mod!()]
61-
| ^^^^^^ not found in this scope
61+
| ^^^^^^ not found in the crate root
6262
|
6363
= help: have you added the `#[macro_use]` on the module/import?
6464

65-
error: cannot find macro `in_mod_escape` in this scope
65+
error: cannot find macro `in_mod_escape` in the crate root
6666
--> $DIR/key-value-expansion-scope.rs:15:14
6767
|
6868
LL | #![doc = in_mod_escape!()]
69-
| ^^^^^^^^^^^^^ not found in this scope
69+
| ^^^^^^^^^^^^^ not found in the crate root
7070
|
7171
= help: have you added the `#[macro_use]` on the module/import?
7272

73-
error: cannot find macro `in_block` in this scope
73+
error: cannot find macro `in_block` in the crate root
7474
--> $DIR/key-value-expansion-scope.rs:16:14
7575
|
7676
LL | #![doc = in_block!()]
77-
| ^^^^^^^^ not found in this scope
77+
| ^^^^^^^^ not found in the crate root
7878
|
7979
= help: have you added the `#[macro_use]` on the module/import?
8080

81-
error: cannot find macro `in_block` in this scope
81+
error: cannot find macro `in_block` in the crate root
8282
--> $DIR/key-value-expansion-scope.rs:50:9
8383
|
8484
LL | #[doc = in_block!()]
85-
| ^^^^^^^^ not found in this scope
85+
| ^^^^^^^^ not found in the crate root
8686
|
8787
= help: have you added the `#[macro_use]` on the module/import?
8888

89-
error: cannot find macro `in_block` in this scope
89+
error: cannot find macro `in_block` in the crate root
9090
--> $DIR/key-value-expansion-scope.rs:52:14
9191
|
9292
LL | #![doc = in_block!()]
93-
| ^^^^^^^^ not found in this scope
93+
| ^^^^^^^^ not found in the crate root
9494
|
9595
= help: have you added the `#[macro_use]` on the module/import?
9696

97-
error: cannot find macro `in_mod` in this scope
97+
error: cannot find macro `in_mod` in the crate root
9898
--> $DIR/key-value-expansion-scope.rs:63:9
9999
|
100100
LL | #[doc = in_mod!()]
101-
| ^^^^^^ not found in this scope
101+
| ^^^^^^ not found in the crate root
102102
|
103103
= help: have you added the `#[macro_use]` on the module/import?
104104

105-
error: cannot find macro `in_block` in this scope
105+
error: cannot find macro `in_block` in the crate root
106106
--> $DIR/key-value-expansion-scope.rs:65:9
107107
|
108108
LL | #[doc = in_block!()]
109-
| ^^^^^^^^ not found in this scope
109+
| ^^^^^^^^ not found in the crate root
110110
|
111111
= help: have you added the `#[macro_use]` on the module/import?
112112

113-
error: cannot find macro `in_mod` in this scope
113+
error: cannot find macro `in_mod` in the crate root
114114
--> $DIR/key-value-expansion-scope.rs:68:14
115115
|
116116
LL | #![doc = in_mod!()]
117-
| ^^^^^^ not found in this scope
117+
| ^^^^^^ not found in the crate root
118118
|
119119
= help: have you added the `#[macro_use]` on the module/import?
120120

121-
error: cannot find macro `in_block` in this scope
121+
error: cannot find macro `in_block` in the crate root
122122
--> $DIR/key-value-expansion-scope.rs:70:14
123123
|
124124
LL | #![doc = in_block!()]
125-
| ^^^^^^^^ not found in this scope
125+
| ^^^^^^^^ not found in the crate root
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-
| ^^^^^^^ not found in this scope
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-
| ^^^^^^^^^^^^^ not found in this scope
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-
| ^^^^^^ not found in this scope
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-
| ^^^^^^ not found in this scope
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-
| ^^^^^^^^^^^^^ not found in this scope
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-
| ^^^^^^^^^^^^^ not found in this scope
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>

tests/ui/attributes/main-removed-1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: cannot find attribute `main` in this scope
1+
error: cannot find attribute `main` in the crate root
22
--> $DIR/main-removed-1.rs:1:3
33
|
44
LL | #[main]
5-
| ^^^^ not found in this scope
5+
| ^^^^ not found in the crate root
66
|
77
= note: `main` is in scope, but it is a function, not an attribute
88

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error: cannot find attribute `fixed_stack_segment` in this scope
1+
error: cannot find attribute `fixed_stack_segment` in the crate root
22
--> $DIR/obsolete-attr.rs:6:3
33
|
44
LL | #[fixed_stack_segment] fn f() {}
5-
| ^^^^^^^^^^^^^^^^^^^ not found in this scope
5+
| ^^^^^^^^^^^^^^^^^^^ not found in the crate root
66

7-
error: cannot find attribute `ab_isize` in this scope
7+
error: cannot find attribute `ab_isize` in the crate root
88
--> $DIR/obsolete-attr.rs:3:3
99
|
1010
LL | #[ab_isize = "stdcall"] extern "C" {}
11-
| ^^^^^^^^ not found in this scope
11+
| ^^^^^^^^ not found in the crate root
1212

1313
error: aborting due to 2 previous errors
1414

0 commit comments

Comments
 (0)