Skip to content

Commit

Permalink
Unrolled build for rust-lang#118533
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#118533 - chenyukang:yukang-fix-118455, r=petrochenkov

Suppress unhelpful diagnostics for unresolved top level attributes

Fixes rust-lang#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 rust-lang#89566, fixes rust-lang#67107.

r? `@petrochenkov`
  • Loading branch information
rust-timer authored Jan 30, 2024
2 parents 5ad7454 + 492df34 commit 445cea3
Show file tree
Hide file tree
Showing 30 changed files with 100 additions and 202 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ pub enum StashKey {
MaybeForgetReturn,
/// Query cycle detected, stashing in favor of a better error.
Cycle,
UndeterminedMacroResolution,
}

fn default_track_diagnostic(diag: Diagnostic, f: &mut dyn FnMut(Diagnostic)) {
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
//! item.

use crate::{errors, fluent_generated as fluent};
use rustc_ast::{ast, AttrStyle, Attribute, LitKind, MetaItemKind, MetaItemLit, NestedMetaItem};
use rustc_ast::{ast, AttrKind, AttrStyle, Attribute, LitKind};
use rustc_ast::{MetaItemKind, MetaItemLit, NestedMetaItem};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::StashKey;
use rustc_errors::{Applicability, DiagCtxt, IntoDiagnosticArg, MultiSpan};
use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
use rustc_hir as hir;
Expand Down Expand Up @@ -2530,6 +2532,14 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
if attr.style == AttrStyle::Inner {
for attr_to_check in ATTRS_TO_CHECK {
if attr.has_name(*attr_to_check) {
if let AttrKind::Normal(ref p) = attr.kind
&& let Some(diag) = tcx.dcx().steal_diagnostic(
p.item.path.span,
StashKey::UndeterminedMacroResolution,
)
{
diag.cancel();
}
let item = tcx
.hir()
.items()
Expand Down
32 changes: 15 additions & 17 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! A bunch of methods and structures more or less related to resolving macros and
//! interface provided by `Resolver` to macro expander.

use crate::errors::{
self, AddAsNonDerive, CannotDetermineMacroResolution, CannotFindIdentInThisScope,
MacroExpectedFound, RemoveSurroundingDerive,
};
use crate::errors::CannotDetermineMacroResolution;
use crate::errors::{self, AddAsNonDerive, CannotFindIdentInThisScope};
use crate::errors::{MacroExpectedFound, RemoveSurroundingDerive};
use crate::Namespace::*;
use crate::{BuiltinMacroState, Determinacy, MacroData};
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
Expand All @@ -15,7 +14,7 @@ use rustc_ast_pretty::pprust;
use rustc_attr::StabilityLevel;
use rustc_data_structures::intern::Interned;
use rustc_data_structures::sync::Lrc;
use rustc_errors::{codes::*, struct_span_code_err, Applicability};
use rustc_errors::{codes::*, struct_span_code_err, Applicability, StashKey};
use rustc_expand::base::{Annotatable, DeriveResolutions, Indeterminate, ResolverExpand};
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
use rustc_expand::compile_declarative_macro;
Expand All @@ -25,9 +24,8 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
use rustc_middle::middle::stability;
use rustc_middle::ty::RegisteredTools;
use rustc_middle::ty::{TyCtxt, Visibility};
use rustc_session::lint::builtin::{
LEGACY_DERIVE_HELPERS, SOFT_UNSTABLE, UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
};
use rustc_session::lint::builtin::UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES;
use rustc_session::lint::builtin::{LEGACY_DERIVE_HELPERS, SOFT_UNSTABLE};
use rustc_session::lint::builtin::{UNUSED_MACROS, UNUSED_MACRO_RULES};
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_session::parse::feature_err;
Expand Down Expand Up @@ -703,21 +701,21 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
// situations should be reported as errors, so this is a bug.
this.dcx().span_delayed_bug(span, "inconsistent resolution for a macro");
}
} else {
} else if this.tcx.dcx().has_errors().is_none() && this.privacy_errors.is_empty() {
// It's possible that the macro was unresolved (indeterminate) and silently
// expanded into a dummy fragment for recovery during expansion.
// Now, post-expansion, the resolution may succeed, but we can't change the
// past and need to report an error.
// However, non-speculative `resolve_path` can successfully return private items
// even if speculative `resolve_path` returned nothing previously, so we skip this
// less informative error if the privacy error is reported elsewhere.
if this.privacy_errors.is_empty() {
this.dcx().emit_err(CannotDetermineMacroResolution {
span,
kind: kind.descr(),
path: Segment::names_to_string(path),
});
}
// less informative error if no other error is reported elsewhere.

let err = this.dcx().create_err(CannotDetermineMacroResolution {
span,
kind: kind.descr(),
path: Segment::names_to_string(path),
});
err.stash(span, StashKey::UndeterminedMacroResolution);
}
};

Expand Down
10 changes: 5 additions & 5 deletions tests/ui/derives/issue-36617.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#![derive(Copy)] //~ ERROR cannot determine resolution for the attribute macro `derive`
#![derive(Copy)]
//~^ ERROR `derive` attribute cannot be used at crate level

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

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

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

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

fn main() {}
42 changes: 1 addition & 41 deletions tests/ui/derives/issue-36617.stderr
Original file line number Diff line number Diff line change
@@ -1,43 +1,3 @@
error: cannot determine resolution for the attribute macro `derive`
--> $DIR/issue-36617.rs:1:4
|
LL | #![derive(Copy)]
| ^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the attribute macro `test`
--> $DIR/issue-36617.rs:4:4
|
LL | #![test]
| ^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the attribute macro `test_case`
--> $DIR/issue-36617.rs:7:4
|
LL | #![test_case]
| ^^^^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the attribute macro `bench`
--> $DIR/issue-36617.rs:10:4
|
LL | #![bench]
| ^^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the attribute macro `global_allocator`
--> $DIR/issue-36617.rs:13:4
|
LL | #![global_allocator]
| ^^^^^^^^^^^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: `derive` attribute cannot be used at crate level
--> $DIR/issue-36617.rs:1:1
|
Expand Down Expand Up @@ -113,5 +73,5 @@ LL - #![global_allocator]
LL + #[global_allocator]
|

error: aborting due to 10 previous errors
error: aborting due to 5 previous errors

1 change: 0 additions & 1 deletion tests/ui/extenv/issue-55897.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod unresolved_env {
use env; //~ ERROR unresolved import `env`

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

mod nonexistent_env {
Expand Down
14 changes: 3 additions & 11 deletions tests/ui/extenv/issue-55897.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: environment variable `NON_EXISTENT` not defined at compile time
--> $DIR/issue-55897.rs:11:22
--> $DIR/issue-55897.rs:10:22
|
LL | include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -8,7 +8,7 @@ LL | include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)

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

error: cannot determine resolution for the macro `env`
--> $DIR/issue-55897.rs:6:22
|
LL | include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
| ^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0432`.
3 changes: 1 addition & 2 deletions tests/ui/feature-gates/issue-43106-gating-of-bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
#![feature(custom_inner_attributes)]

#![bench = "4100"]
//~^ ERROR cannot determine resolution for the attribute macro `bench`
//~^^ ERROR `bench` attribute cannot be used at crate level
//~^ ERROR `bench` attribute cannot be used at crate level
fn main() {}
12 changes: 2 additions & 10 deletions tests/ui/feature-gates/issue-43106-gating-of-bench.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
error: cannot determine resolution for the attribute macro `bench`
--> $DIR/issue-43106-gating-of-bench.rs:7:4
|
LL | #![bench = "4100"]
| ^^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: `bench` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-bench.rs:7:1
|
LL | #![bench = "4100"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL |
LL | fn main() {}
| ---- the inner attribute doesn't annotate this function
|
Expand All @@ -21,5 +13,5 @@ LL - #![bench = "4100"]
LL + #[bench = "4100"]
|

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

3 changes: 1 addition & 2 deletions tests/ui/feature-gates/issue-43106-gating-of-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

#![allow(soft_unstable)]
#![test = "4200"]
//~^ ERROR cannot determine resolution for the attribute macro `test`
//~^^ ERROR `test` attribute cannot be used at crate level
//~^ ERROR `test` attribute cannot be used at crate level
fn main() {}
12 changes: 2 additions & 10 deletions tests/ui/feature-gates/issue-43106-gating-of-test.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
error: cannot determine resolution for the attribute macro `test`
--> $DIR/issue-43106-gating-of-test.rs:4:4
|
LL | #![test = "4200"]
| ^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: `test` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-test.rs:4:1
|
LL | #![test = "4200"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL |
LL | fn main() {}
| ---- the inner attribute doesn't annotate this function
|
Expand All @@ -21,5 +13,5 @@ LL - #![test = "4200"]
LL + #[test = "4200"]
|

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

2 changes: 1 addition & 1 deletion tests/ui/imports/issue-28134.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// compile-flags: --test

#![allow(soft_unstable)]
#![test] //~ ERROR cannot determine resolution for the attribute macro `test`
#![test]
//~^ ERROR 4:1: 4:9: `test` attribute cannot be used at crate level
10 changes: 1 addition & 9 deletions tests/ui/imports/issue-28134.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
error: cannot determine resolution for the attribute macro `test`
--> $DIR/issue-28134.rs:4:4
|
LL | #![test]
| ^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: `test` attribute cannot be used at crate level
--> $DIR/issue-28134.rs:4:1
|
Expand All @@ -18,5 +10,5 @@ LL - #![test]
LL + #[test]
|

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

7 changes: 3 additions & 4 deletions tests/ui/imports/issue-55457.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use NonExistent; //~ ERROR unresolved import `NonExistent`
use non_existent::non_existent; //~ ERROR unresolved import `non_existent`

#[non_existent] //~ ERROR cannot determine resolution for the attribute macro `non_existent`
#[derive(NonExistent)] //~ ERROR cannot determine resolution for the derive macro `NonExistent`
//~| ERROR cannot determine resolution for the derive macro `NonExistent`
//~| ERROR cannot determine resolution for the derive macro `NonExistent`
#[non_existent]
#[derive(NonExistent)]

struct S;

fn main() {}
36 changes: 1 addition & 35 deletions tests/ui/imports/issue-55457.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,6 @@ LL | use non_existent::non_existent;
|
= help: consider adding `extern crate non_existent` to use the `non_existent` crate

error: cannot determine resolution for the derive macro `NonExistent`
--> $DIR/issue-55457.rs:5:10
|
LL | #[derive(NonExistent)]
| ^^^^^^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the attribute macro `non_existent`
--> $DIR/issue-55457.rs:4:3
|
LL | #[non_existent]
| ^^^^^^^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the derive macro `NonExistent`
--> $DIR/issue-55457.rs:5:10
|
LL | #[derive(NonExistent)]
| ^^^^^^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: cannot determine resolution for the derive macro `NonExistent`
--> $DIR/issue-55457.rs:5:10
|
LL | #[derive(NonExistent)]
| ^^^^^^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 6 previous errors
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0432`.
1 change: 0 additions & 1 deletion tests/ui/imports/issue-59764.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ use issue_59764::foo::makro;
//~^ ERROR unresolved import `issue_59764::foo::makro` [E0432]

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

fn main() {
bar();
Expand Down
12 changes: 2 additions & 10 deletions tests/ui/imports/issue-59764.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,13 @@ help: a macro with this name exists at the root of the crate
LL | use issue_59764::makro;
| ~~~~~~~~~~~~~~~~~~

error: cannot determine resolution for the macro `makro`
--> $DIR/issue-59764.rs:130:1
|
LL | makro!(bar);
| ^^^^^
|
= note: import resolution is stuck, try simplifying macro imports

error[E0425]: cannot find function `bar` in this scope
--> $DIR/issue-59764.rs:134:5
--> $DIR/issue-59764.rs:133:5
|
LL | bar();
| ^^^ not found in this scope

error: aborting due to 18 previous errors
error: aborting due to 17 previous errors

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

0 comments on commit 445cea3

Please sign in to comment.