Skip to content

Commit 3abad14

Browse files
committed
Unify output of the different "cannot find X" errors
Do not mention the scope where an ident couldn't be resolved (particularly for macros), and add a primary span label mentioning "this scope" (ideally we would replace the text with the actual scope name in the future): ``` error: cannot find derive macro `rustfmt` --> $DIR/tool-attributes-misplaced-1.rs:4:10 | LL | #[derive(rustfmt)] | ^^^^^^^ not found in this scope ```
1 parent 86f8aae commit 3abad14

File tree

88 files changed

+526
-428
lines changed

Some content is hidden

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

88 files changed

+526
-428
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,8 @@ lint_opaque_hidden_inferred_bound_sugg = add this bound
609609
lint_or_patterns_back_compat = the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro
610610
.suggestion = use pat_param to preserve semantics
611611
612-
lint_out_of_scope_macro_calls = cannot find macro `{$path}` in this scope
612+
lint_out_of_scope_macro_calls = cannot find macro `{$path}`
613+
.label = not found in this scope
613614
.help = import `macro_rules` with `use` to make it callable above its definition
614615
615616
lint_overflowing_bin_hex = literal out of range for `{$ty}`
@@ -650,7 +651,7 @@ lint_pattern_in_foreign = patterns aren't allowed in foreign function declaratio
650651
lint_private_extern_crate_reexport = extern crate `{$ident}` is private and cannot be re-exported
651652
.suggestion = consider making the `extern crate` item publicly accessible
652653
653-
lint_proc_macro_derive_resolution_fallback = cannot find {$ns} `{$ident}` in this scope
654+
lint_proc_macro_derive_resolution_fallback = cannot find {$ns} `{$ident}`
654655
.label = names from parent modules are not accessible without an explicit import
655656
656657
lint_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false

compiler/rustc_lint/src/context/diagnostics.rs

Lines changed: 2 additions & 2 deletions
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 } => {
438+
lints::OutOfScopeMacroCalls { span, path }.decorate_lint(diag)
439439
}
440440
}
441441
}

compiler/rustc_lint/src/lints.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,5 +2917,7 @@ pub struct UnsafeAttrOutsideUnsafeSuggestion {
29172917
#[diag(lint_out_of_scope_macro_calls)]
29182918
#[help]
29192919
pub struct OutOfScopeMacroCalls {
2920+
#[label]
2921+
pub span: Span,
29202922
pub path: String,
29212923
}

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ pub enum BuiltinLintDiag {
745745
is_macro: bool,
746746
},
747747
OutOfScopeMacroCalls {
748+
span: Span,
748749
path: String,
749750
},
750751
}

compiler/rustc_resolve/messages.ftl

Lines changed: 2 additions & 2 deletions
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 =
84-
cannot find {$expected} `{$ident}` in this scope
83+
resolve_cannot_find_ident_in_this_scope = cannot find {$expected} `{$ident}`
84+
.label = not found in this scope
8585
8686
resolve_cannot_glob_import_possible_crates =
8787
cannot glob-import all possible crates

compiler/rustc_resolve/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ pub(crate) struct ImportsCannotReferTo<'a> {
635635
#[diag(resolve_cannot_find_ident_in_this_scope)]
636636
pub(crate) struct CannotFindIdentInThisScope<'a> {
637637
#[primary_span]
638+
#[label]
638639
pub(crate) span: Span,
639640
pub(crate) expected: &'a str,
640641
pub(crate) ident: Ident,

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3918,7 +3918,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
39183918

39193919
// There are two different error messages user might receive at
39203920
// this point:
3921-
// - E0412 cannot find type `{}` in this scope
3921+
// - E0412 cannot find type `{}`
39223922
// - E0433 failed to resolve: use of undeclared type or module `{}`
39233923
//
39243924
// The first one is emitted for paths in type-position, and the

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
25222522
if let Some(generics) = kind.generics() {
25232523
if span.overlaps(generics.span) {
25242524
// Avoid the following:
2525-
// error[E0405]: cannot find trait `A` in this scope
2525+
// error[E0405]: cannot find trait `A`
25262526
// --> $DIR/typo-suggestion-named-underscore.rs:CC:LL
25272527
// |
25282528
// L | fn foo<T: A>(x: T) {} // Shouldn't suggest underscore

compiler/rustc_resolve/src/macros.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
10711071
OUT_OF_SCOPE_MACRO_CALLS,
10721072
path.span,
10731073
node_id,
1074-
BuiltinLintDiag::OutOfScopeMacroCalls { path: pprust::path_to_string(path) },
1074+
BuiltinLintDiag::OutOfScopeMacroCalls {
1075+
span: path.span,
1076+
path: pprust::path_to_string(path),
1077+
},
10751078
);
10761079
}
10771080
}

tests/rustdoc-ui/impl-fn-nesting.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: cannot find macro `unknown_macro` in this scope
1+
error: cannot find macro `unknown_macro`
22
--> $DIR/impl-fn-nesting.rs:32:13
33
|
44
LL | unknown_macro!();
5-
| ^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^ not found in this scope
66

77
error[E0405]: cannot find trait `UnknownBound`
88
--> $DIR/impl-fn-nesting.rs:11:13

tests/ui-fulldeps/session-diagnostic/diagnostic-derive.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ struct WrongStructAttrStyle {}
6262
#[derive(Diagnostic)]
6363
#[nonsense(no_crate_example, code = E0123)]
6464
//~^ ERROR `#[nonsense(...)]` is not a valid attribute
65-
//~^^ ERROR diagnostic slug not specified
66-
//~^^^ ERROR cannot find attribute `nonsense` in this scope
65+
//~| ERROR diagnostic slug not specified
66+
//~| ERROR cannot find attribute `nonsense`
6767
struct InvalidStructAttr {}
6868

6969
#[derive(Diagnostic)]
@@ -149,7 +149,7 @@ struct MessageWrongType {
149149
struct InvalidPathFieldAttr {
150150
#[nonsense]
151151
//~^ ERROR `#[nonsense]` is not a valid attribute
152-
//~^^ ERROR cannot find attribute `nonsense` in this scope
152+
//~^^ ERROR cannot find attribute `nonsense`
153153
foo: String,
154154
}
155155

@@ -583,28 +583,28 @@ struct ErrorWithWarn {
583583
#[error(no_crate_example, code = E0123)]
584584
//~^ ERROR `#[error(...)]` is not a valid attribute
585585
//~| ERROR diagnostic slug not specified
586-
//~| ERROR cannot find attribute `error` in this scope
586+
//~| ERROR cannot find attribute `error`
587587
struct ErrorAttribute {}
588588

589589
#[derive(Diagnostic)]
590590
#[warn_(no_crate_example, code = E0123)]
591591
//~^ ERROR `#[warn_(...)]` is not a valid attribute
592592
//~| ERROR diagnostic slug not specified
593-
//~| ERROR cannot find attribute `warn_` in this scope
593+
//~| ERROR cannot find attribute `warn_`
594594
struct WarnAttribute {}
595595

596596
#[derive(Diagnostic)]
597597
#[lint(no_crate_example, code = E0123)]
598598
//~^ ERROR `#[lint(...)]` is not a valid attribute
599599
//~| ERROR diagnostic slug not specified
600-
//~| ERROR cannot find attribute `lint` in this scope
600+
//~| ERROR cannot find attribute `lint`
601601
struct LintAttributeOnSessionDiag {}
602602

603603
#[derive(LintDiagnostic)]
604604
#[lint(no_crate_example, code = E0123)]
605605
//~^ ERROR `#[lint(...)]` is not a valid attribute
606606
//~| ERROR diagnostic slug not specified
607-
//~| ERROR cannot find attribute `lint` in this scope
607+
//~| ERROR cannot find attribute `lint`
608608
struct LintAttributeOnLintDiag {}
609609

610610
#[derive(Diagnostic)]
@@ -643,14 +643,14 @@ struct MissingCodeInSuggestion {
643643
#[diag(no_crate_example, code = E0123)]
644644
#[multipart_suggestion(no_crate_suggestion)]
645645
//~^ ERROR `#[multipart_suggestion(...)]` is not a valid attribute
646-
//~| ERROR cannot find attribute `multipart_suggestion` in this scope
646+
//~| ERROR cannot find attribute `multipart_suggestion`
647647
#[multipart_suggestion()]
648-
//~^ ERROR cannot find attribute `multipart_suggestion` in this scope
648+
//~^ ERROR cannot find attribute `multipart_suggestion`
649649
//~| ERROR `#[multipart_suggestion(...)]` is not a valid attribute
650650
struct MultipartSuggestion {
651651
#[multipart_suggestion(no_crate_suggestion)]
652652
//~^ ERROR `#[multipart_suggestion(...)]` is not a valid attribute
653-
//~| ERROR cannot find attribute `multipart_suggestion` in this scope
653+
//~| ERROR cannot find attribute `multipart_suggestion`
654654
suggestion: Span,
655655
}
656656

tests/ui-fulldeps/session-diagnostic/subdiagnostic-derive.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ struct O {
161161

162162
#[derive(Subdiagnostic)]
163163
#[foo]
164-
//~^ ERROR cannot find attribute `foo` in this scope
164+
//~^ ERROR cannot find attribute `foo`
165165
//~^^ ERROR unsupported type attribute for subdiagnostic enum
166166
enum P {
167167
#[label(no_crate_example)]
@@ -176,7 +176,7 @@ enum P {
176176
enum Q {
177177
#[bar]
178178
//~^ ERROR `#[bar]` is not a valid attribute
179-
//~^^ ERROR cannot find attribute `bar` in this scope
179+
//~^^ ERROR cannot find attribute `bar`
180180
A {
181181
#[primary_span]
182182
span: Span,
@@ -188,7 +188,7 @@ enum Q {
188188
enum R {
189189
#[bar = "..."]
190190
//~^ ERROR `#[bar = ...]` is not a valid attribute
191-
//~^^ ERROR cannot find attribute `bar` in this scope
191+
//~^^ ERROR cannot find attribute `bar`
192192
A {
193193
#[primary_span]
194194
span: Span,
@@ -200,7 +200,7 @@ enum R {
200200
enum S {
201201
#[bar = 4]
202202
//~^ ERROR `#[bar = ...]` is not a valid attribute
203-
//~^^ ERROR cannot find attribute `bar` in this scope
203+
//~^^ ERROR cannot find attribute `bar`
204204
A {
205205
#[primary_span]
206206
span: Span,
@@ -212,7 +212,7 @@ enum S {
212212
enum T {
213213
#[bar("...")]
214214
//~^ ERROR `#[bar(...)]` is not a valid attribute
215-
//~^^ ERROR cannot find attribute `bar` in this scope
215+
//~^^ ERROR cannot find attribute `bar`
216216
A {
217217
#[primary_span]
218218
span: Span,
@@ -273,7 +273,7 @@ struct Y {
273273
span: Span,
274274
#[bar]
275275
//~^ ERROR `#[bar]` is not a valid attribute
276-
//~^^ ERROR cannot find attribute `bar` in this scope
276+
//~^^ ERROR cannot find attribute `bar`
277277
bar: String,
278278
}
279279

@@ -284,7 +284,7 @@ struct Z {
284284
span: Span,
285285
#[bar = "..."]
286286
//~^ ERROR `#[bar = ...]` is not a valid attribute
287-
//~^^ ERROR cannot find attribute `bar` in this scope
287+
//~^^ ERROR cannot find attribute `bar`
288288
bar: String,
289289
}
290290

@@ -295,7 +295,7 @@ struct AA {
295295
span: Span,
296296
#[bar("...")]
297297
//~^ ERROR `#[bar(...)]` is not a valid attribute
298-
//~^^ ERROR cannot find attribute `bar` in this scope
298+
//~^^ ERROR cannot find attribute `bar`
299299
bar: String,
300300
}
301301

tests/ui/attributes/issue-90873.stderr

Lines changed: 4 additions & 4 deletions
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`
1414
--> $DIR/issue-90873.rs:1:4
1515
|
1616
LL | #![u=||{static d=||1;}]
17-
| ^
17+
| ^ not found in this scope
1818

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 { () => { "" } }
@@ -47,7 +47,7 @@ mod macros_escape {
4747
}
4848
}
4949

50-
#[doc = in_block!()] //~ ERROR cannot find macro `in_block` in this scope
50+
#[doc = in_block!()] //~ ERROR cannot find macro `in_block`
5151
fn block() {
5252
#![doc = in_block!()] //~ ERROR cannot find macro `in_block`
5353

0 commit comments

Comments
 (0)