Skip to content

Commit ee2e9e1

Browse files
Rollup merge of #118533 - chenyukang:yukang-fix-118455, r=petrochenkov
Suppress unhelpful diagnostics for unresolved top level attributes Fixes #118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them. Also fixes #89566, fixes #67107. r? `@petrochenkov`
2 parents efff267 + 492df34 commit ee2e9e1

30 files changed

+100
-202
lines changed

compiler/rustc_errors/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ pub enum StashKey {
514514
MaybeForgetReturn,
515515
/// Query cycle detected, stashing in favor of a better error.
516516
Cycle,
517+
UndeterminedMacroResolution,
517518
}
518519

519520
fn default_track_diagnostic(diag: Diagnostic, f: &mut dyn FnMut(Diagnostic)) {

compiler/rustc_passes/src/check_attr.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
//! item.
66
77
use crate::{errors, fluent_generated as fluent};
8-
use rustc_ast::{ast, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem};
8+
use rustc_ast::{ast, AttrKind, AttrStyle, Attribute, LitKind};
9+
use rustc_ast::{MetaItemKind, MetaItemLit, NestedMetaItem};
910
use rustc_data_structures::fx::FxHashMap;
11+
use rustc_errors::StashKey;
1012
use rustc_errors::{Applicability, DiagCtxt, IntoDiagnosticArg, MultiSpan};
1113
use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
1214
use rustc_hir as hir;
@@ -2530,6 +2532,14 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
25302532
if attr.style == AttrStyle::Inner {
25312533
for attr_to_check in ATTRS_TO_CHECK {
25322534
if attr.has_name(*attr_to_check) {
2535+
if let AttrKind::Normal(ref p) = attr.kind
2536+
&& let Some(diag) = tcx.dcx().steal_diagnostic(
2537+
p.item.path.span,
2538+
StashKey::UndeterminedMacroResolution,
2539+
)
2540+
{
2541+
diag.cancel();
2542+
}
25332543
let item = tcx
25342544
.hir()
25352545
.items()

compiler/rustc_resolve/src/macros.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
//! A bunch of methods and structures more or less related to resolving macros and
22
//! interface provided by `Resolver` to macro expander.
33
4-
use crate::errors::{
5-
self, AddAsNonDerive, CannotDetermineMacroResolution, CannotFindIdentInThisScope,
6-
MacroExpectedFound, RemoveSurroundingDerive,
7-
};
4+
use crate::errors::CannotDetermineMacroResolution;
5+
use crate::errors::{self, AddAsNonDerive, CannotFindIdentInThisScope};
6+
use crate::errors::{MacroExpectedFound, RemoveSurroundingDerive};
87
use crate::Namespace::*;
98
use crate::{BuiltinMacroState, Determinacy, MacroData};
109
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
@@ -15,7 +14,7 @@ use rustc_ast_pretty::pprust;
1514
use rustc_attr::StabilityLevel;
1615
use rustc_data_structures::intern::Interned;
1716
use rustc_data_structures::sync::Lrc;
18-
use rustc_errors::{codes::*, struct_span_code_err, Applicability};
17+
use rustc_errors::{codes::*, struct_span_code_err, Applicability, StashKey};
1918
use rustc_expand::base::{Annotatable, DeriveResolutions, Indeterminate, ResolverExpand};
2019
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
2120
use rustc_expand::compile_declarative_macro;
@@ -25,9 +24,8 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
2524
use rustc_middle::middle::stability;
2625
use rustc_middle::ty::RegisteredTools;
2726
use rustc_middle::ty::{TyCtxt, Visibility};
28-
use rustc_session::lint::builtin::{
29-
LEGACY_DERIVE_HELPERS, SOFT_UNSTABLE, UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
30-
};
27+
use rustc_session::lint::builtin::UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES;
28+
use rustc_session::lint::builtin::{LEGACY_DERIVE_HELPERS, SOFT_UNSTABLE};
3129
use rustc_session::lint::builtin::{UNUSED_MACROS, UNUSED_MACRO_RULES};
3230
use rustc_session::lint::BuiltinLintDiagnostics;
3331
use rustc_session::parse::feature_err;
@@ -703,21 +701,21 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
703701
// situations should be reported as errors, so this is a bug.
704702
this.dcx().span_delayed_bug(span, "inconsistent resolution for a macro");
705703
}
706-
} else {
704+
} else if this.tcx.dcx().has_errors().is_none() && this.privacy_errors.is_empty() {
707705
// It's possible that the macro was unresolved (indeterminate) and silently
708706
// expanded into a dummy fragment for recovery during expansion.
709707
// Now, post-expansion, the resolution may succeed, but we can't change the
710708
// past and need to report an error.
711709
// However, non-speculative `resolve_path` can successfully return private items
712710
// even if speculative `resolve_path` returned nothing previously, so we skip this
713-
// less informative error if the privacy error is reported elsewhere.
714-
if this.privacy_errors.is_empty() {
715-
this.dcx().emit_err(CannotDetermineMacroResolution {
716-
span,
717-
kind: kind.descr(),
718-
path: Segment::names_to_string(path),
719-
});
720-
}
711+
// less informative error if no other error is reported elsewhere.
712+
713+
let err = this.dcx().create_err(CannotDetermineMacroResolution {
714+
span,
715+
kind: kind.descr(),
716+
path: Segment::names_to_string(path),
717+
});
718+
err.stash(span, StashKey::UndeterminedMacroResolution);
721719
}
722720
};
723721

tests/ui/derives/issue-36617.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
#![derive(Copy)] //~ ERROR cannot determine resolution for the attribute macro `derive`
1+
#![derive(Copy)]
22
//~^ ERROR `derive` attribute cannot be used at crate level
33

4-
#![test]//~ ERROR cannot determine resolution for the attribute macro `test`
4+
#![test]
55
//~^ ERROR `test` attribute cannot be used at crate level
66

7-
#![test_case]//~ ERROR cannot determine resolution for the attribute macro `test_case`
7+
#![test_case]
88
//~^ ERROR `test_case` attribute cannot be used at crate level
99

10-
#![bench]//~ ERROR cannot determine resolution for the attribute macro `bench`
10+
#![bench]
1111
//~^ ERROR `bench` attribute cannot be used at crate level
1212

13-
#![global_allocator]//~ ERROR cannot determine resolution for the attribute macro `global_allocator`
13+
#![global_allocator]
1414
//~^ ERROR `global_allocator` attribute cannot be used at crate level
1515

1616
fn main() {}

tests/ui/derives/issue-36617.stderr

+1-41
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,3 @@
1-
error: cannot determine resolution for the attribute macro `derive`
2-
--> $DIR/issue-36617.rs:1:4
3-
|
4-
LL | #![derive(Copy)]
5-
| ^^^^^^
6-
|
7-
= note: import resolution is stuck, try simplifying macro imports
8-
9-
error: cannot determine resolution for the attribute macro `test`
10-
--> $DIR/issue-36617.rs:4:4
11-
|
12-
LL | #![test]
13-
| ^^^^
14-
|
15-
= note: import resolution is stuck, try simplifying macro imports
16-
17-
error: cannot determine resolution for the attribute macro `test_case`
18-
--> $DIR/issue-36617.rs:7:4
19-
|
20-
LL | #![test_case]
21-
| ^^^^^^^^^
22-
|
23-
= note: import resolution is stuck, try simplifying macro imports
24-
25-
error: cannot determine resolution for the attribute macro `bench`
26-
--> $DIR/issue-36617.rs:10:4
27-
|
28-
LL | #![bench]
29-
| ^^^^^
30-
|
31-
= note: import resolution is stuck, try simplifying macro imports
32-
33-
error: cannot determine resolution for the attribute macro `global_allocator`
34-
--> $DIR/issue-36617.rs:13:4
35-
|
36-
LL | #![global_allocator]
37-
| ^^^^^^^^^^^^^^^^
38-
|
39-
= note: import resolution is stuck, try simplifying macro imports
40-
411
error: `derive` attribute cannot be used at crate level
422
--> $DIR/issue-36617.rs:1:1
433
|
@@ -113,5 +73,5 @@ LL - #![global_allocator]
11373
LL + #[global_allocator]
11474
|
11575

116-
error: aborting due to 10 previous errors
76+
error: aborting due to 5 previous errors
11777

tests/ui/extenv/issue-55897.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ mod unresolved_env {
44
use env; //~ ERROR unresolved import `env`
55

66
include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
7-
//~^ ERROR cannot determine resolution for the macro `env`
87
}
98

109
mod nonexistent_env {

tests/ui/extenv/issue-55897.stderr

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: environment variable `NON_EXISTENT` not defined at compile time
2-
--> $DIR/issue-55897.rs:11:22
2+
--> $DIR/issue-55897.rs:10:22
33
|
44
LL | include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
55
| ^^^^^^^^^^^^^^^^^^^^
@@ -8,7 +8,7 @@ LL | include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
88
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
99

1010
error: suffixes on string literals are invalid
11-
--> $DIR/issue-55897.rs:16:22
11+
--> $DIR/issue-55897.rs:15:22
1212
|
1313
LL | include!(concat!("NON_EXISTENT"suffix, "/data.rs"));
1414
| ^^^^^^^^^^^^^^^^^^^^ invalid suffix `suffix`
@@ -33,14 +33,6 @@ help: consider importing this module instead
3333
LL | use std::env;
3434
| ~~~~~~~~
3535

36-
error: cannot determine resolution for the macro `env`
37-
--> $DIR/issue-55897.rs:6:22
38-
|
39-
LL | include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
40-
| ^^^
41-
|
42-
= note: import resolution is stuck, try simplifying macro imports
43-
44-
error: aborting due to 5 previous errors
36+
error: aborting due to 4 previous errors
4537

4638
For more information about this error, try `rustc --explain E0432`.

tests/ui/feature-gates/issue-43106-gating-of-bench.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
#![feature(custom_inner_attributes)]
66

77
#![bench = "4100"]
8-
//~^ ERROR cannot determine resolution for the attribute macro `bench`
9-
//~^^ ERROR `bench` attribute cannot be used at crate level
8+
//~^ ERROR `bench` attribute cannot be used at crate level
109
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
error: cannot determine resolution for the attribute macro `bench`
2-
--> $DIR/issue-43106-gating-of-bench.rs:7:4
3-
|
4-
LL | #![bench = "4100"]
5-
| ^^^^^
6-
|
7-
= note: import resolution is stuck, try simplifying macro imports
8-
91
error: `bench` attribute cannot be used at crate level
102
--> $DIR/issue-43106-gating-of-bench.rs:7:1
113
|
124
LL | #![bench = "4100"]
135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14-
...
6+
LL |
157
LL | fn main() {}
168
| ---- the inner attribute doesn't annotate this function
179
|
@@ -21,5 +13,5 @@ LL - #![bench = "4100"]
2113
LL + #[bench = "4100"]
2214
|
2315

24-
error: aborting due to 2 previous errors
16+
error: aborting due to 1 previous error
2517

tests/ui/feature-gates/issue-43106-gating-of-test.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22

33
#![allow(soft_unstable)]
44
#![test = "4200"]
5-
//~^ ERROR cannot determine resolution for the attribute macro `test`
6-
//~^^ ERROR `test` attribute cannot be used at crate level
5+
//~^ ERROR `test` attribute cannot be used at crate level
76
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
error: cannot determine resolution for the attribute macro `test`
2-
--> $DIR/issue-43106-gating-of-test.rs:4:4
3-
|
4-
LL | #![test = "4200"]
5-
| ^^^^
6-
|
7-
= note: import resolution is stuck, try simplifying macro imports
8-
91
error: `test` attribute cannot be used at crate level
102
--> $DIR/issue-43106-gating-of-test.rs:4:1
113
|
124
LL | #![test = "4200"]
135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14-
...
6+
LL |
157
LL | fn main() {}
168
| ---- the inner attribute doesn't annotate this function
179
|
@@ -21,5 +13,5 @@ LL - #![test = "4200"]
2113
LL + #[test = "4200"]
2214
|
2315

24-
error: aborting due to 2 previous errors
16+
error: aborting due to 1 previous error
2517

tests/ui/imports/issue-28134.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// compile-flags: --test
22

33
#![allow(soft_unstable)]
4-
#![test] //~ ERROR cannot determine resolution for the attribute macro `test`
4+
#![test]
55
//~^ ERROR 4:1: 4:9: `test` attribute cannot be used at crate level

tests/ui/imports/issue-28134.stderr

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error: cannot determine resolution for the attribute macro `test`
2-
--> $DIR/issue-28134.rs:4:4
3-
|
4-
LL | #![test]
5-
| ^^^^
6-
|
7-
= note: import resolution is stuck, try simplifying macro imports
8-
91
error: `test` attribute cannot be used at crate level
102
--> $DIR/issue-28134.rs:4:1
113
|
@@ -18,5 +10,5 @@ LL - #![test]
1810
LL + #[test]
1911
|
2012

21-
error: aborting due to 2 previous errors
13+
error: aborting due to 1 previous error
2214

tests/ui/imports/issue-55457.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use NonExistent; //~ ERROR unresolved import `NonExistent`
22
use non_existent::non_existent; //~ ERROR unresolved import `non_existent`
33

4-
#[non_existent] //~ ERROR cannot determine resolution for the attribute macro `non_existent`
5-
#[derive(NonExistent)] //~ ERROR cannot determine resolution for the derive macro `NonExistent`
6-
//~| ERROR cannot determine resolution for the derive macro `NonExistent`
7-
//~| ERROR cannot determine resolution for the derive macro `NonExistent`
4+
#[non_existent]
5+
#[derive(NonExistent)]
6+
87
struct S;
98

109
fn main() {}

tests/ui/imports/issue-55457.stderr

+1-35
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,6 @@ LL | use non_existent::non_existent;
1515
|
1616
= help: consider adding `extern crate non_existent` to use the `non_existent` crate
1717

18-
error: cannot determine resolution for the derive macro `NonExistent`
19-
--> $DIR/issue-55457.rs:5:10
20-
|
21-
LL | #[derive(NonExistent)]
22-
| ^^^^^^^^^^^
23-
|
24-
= note: import resolution is stuck, try simplifying macro imports
25-
26-
error: cannot determine resolution for the attribute macro `non_existent`
27-
--> $DIR/issue-55457.rs:4:3
28-
|
29-
LL | #[non_existent]
30-
| ^^^^^^^^^^^^
31-
|
32-
= note: import resolution is stuck, try simplifying macro imports
33-
34-
error: cannot determine resolution for the derive macro `NonExistent`
35-
--> $DIR/issue-55457.rs:5:10
36-
|
37-
LL | #[derive(NonExistent)]
38-
| ^^^^^^^^^^^
39-
|
40-
= note: import resolution is stuck, try simplifying macro imports
41-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
42-
43-
error: cannot determine resolution for the derive macro `NonExistent`
44-
--> $DIR/issue-55457.rs:5:10
45-
|
46-
LL | #[derive(NonExistent)]
47-
| ^^^^^^^^^^^
48-
|
49-
= note: import resolution is stuck, try simplifying macro imports
50-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
51-
52-
error: aborting due to 6 previous errors
18+
error: aborting due to 2 previous errors
5319

5420
For more information about this error, try `rustc --explain E0432`.

tests/ui/imports/issue-59764.rs

-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ use issue_59764::foo::makro;
128128
//~^ ERROR unresolved import `issue_59764::foo::makro` [E0432]
129129

130130
makro!(bar);
131-
//~^ ERROR cannot determine resolution for the macro `makro`
132131

133132
fn main() {
134133
bar();

tests/ui/imports/issue-59764.stderr

+2-10
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,13 @@ help: a macro with this name exists at the root of the crate
226226
LL | use issue_59764::makro;
227227
| ~~~~~~~~~~~~~~~~~~
228228

229-
error: cannot determine resolution for the macro `makro`
230-
--> $DIR/issue-59764.rs:130:1
231-
|
232-
LL | makro!(bar);
233-
| ^^^^^
234-
|
235-
= note: import resolution is stuck, try simplifying macro imports
236-
237229
error[E0425]: cannot find function `bar` in this scope
238-
--> $DIR/issue-59764.rs:134:5
230+
--> $DIR/issue-59764.rs:133:5
239231
|
240232
LL | bar();
241233
| ^^^ not found in this scope
242234

243-
error: aborting due to 18 previous errors
235+
error: aborting due to 17 previous errors
244236

245237
Some errors have detailed explanations: E0425, E0432.
246238
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)