Rollup merge of #89701 - tom7980:issue-89566-fix, r=petrochenkov

Updated error message for accidental uses of derive attribute as a crate attribute

This partially fixes the original issue #89566 by adding derive to the list of invalid crate attributes and then providing an updated error message however I'm not sure how to prevent the resolution error message from emitting without causing the compiler to just abort when it finds an invalid crate attribute (which I'd prefer not to do so we can find and emit other errors).

`@petrochenkov` I have been told you may have some insight on why it's emitting the resolution error though honestly I'm not sure if we need to worry about fixing it as long as we can provide the invalid crate attribute error also (which happens first anyway)
This commit is contained in:
Matthias Krüger 2021-12-04 10:42:19 +01:00 committed by GitHub
commit 9f8e822364
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 213 additions and 13 deletions

View File

@ -1953,7 +1953,12 @@ fn is_c_like_enum(item: &Item<'_>) -> bool {
} }
} }
// FIXME: Fix "Cannot determine resolution" error and remove built-in macros
// from this check.
fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) { fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
// Check for builtin attributes at the crate level
// which were unsuccessfully resolved due to cannot determine
// resolution for the attribute macro error.
const ATTRS_TO_CHECK: &[Symbol] = &[ const ATTRS_TO_CHECK: &[Symbol] = &[
sym::macro_export, sym::macro_export,
sym::repr, sym::repr,
@ -1961,20 +1966,39 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
sym::automatically_derived, sym::automatically_derived,
sym::start, sym::start,
sym::rustc_main, sym::rustc_main,
sym::derive,
sym::test,
sym::test_case,
sym::global_allocator,
sym::bench,
]; ];
for attr in attrs { for attr in attrs {
for attr_to_check in ATTRS_TO_CHECK { // This function should only be called with crate attributes
if attr.has_name(*attr_to_check) { // which are inner attributes always but lets check to make sure
tcx.sess if attr.style == AttrStyle::Inner {
.struct_span_err( for attr_to_check in ATTRS_TO_CHECK {
if attr.has_name(*attr_to_check) {
let mut err = tcx.sess.struct_span_err(
attr.span, attr.span,
&format!( &format!(
"`{}` attribute cannot be used at crate level", "`{}` attribute cannot be used at crate level",
attr_to_check.to_ident_string() attr_to_check.to_ident_string()
), ),
) );
.emit(); // Only emit an error with a suggestion if we can create a
// string out of the attribute span
if let Ok(src) = tcx.sess.source_map().span_to_snippet(attr.span) {
let replacement = src.replace("#!", "#");
err.span_suggestion_verbose(
attr.span,
"perhaps you meant to use an outer attribute",
replacement,
rustc_errors::Applicability::MachineApplicable,
);
}
err.emit()
}
} }
} }
} }

View File

@ -1,3 +1,16 @@
#![derive(Copy)] //~ ERROR cannot determine resolution for the attribute macro `derive` #![derive(Copy)] //~ ERROR cannot determine resolution for the attribute macro `derive`
//~^ ERROR `derive` attribute cannot be used at crate level
#![test]//~ ERROR cannot determine resolution for the attribute macro `test`
//~^ ERROR `test` attribute cannot be used at crate level
#![test_case]//~ ERROR cannot determine resolution for the attribute macro `test_case`
//~^ ERROR `test_case` attribute cannot be used at crate level
#![bench]//~ ERROR cannot determine resolution for the attribute macro `bench`
//~^ ERROR `bench` attribute cannot be used at crate level
#![global_allocator]//~ ERROR cannot determine resolution for the attribute macro `global_allocator`
//~^ ERROR `global_allocator` attribute cannot be used at crate level
fn main() {} fn main() {}

View File

@ -6,5 +6,92 @@ LL | #![derive(Copy)]
| |
= note: import resolution is stuck, try simplifying macro imports = note: import resolution is stuck, try simplifying macro imports
error: aborting due to previous error 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
|
LL | #![derive(Copy)]
| ^^^^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[derive(Copy)]
| ~~~~~~~~~~~~~~~
error: `test` attribute cannot be used at crate level
--> $DIR/issue-36617.rs:4:1
|
LL | #![test]
| ^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[test]
| ~~~~~~~
error: `test_case` attribute cannot be used at crate level
--> $DIR/issue-36617.rs:7:1
|
LL | #![test_case]
| ^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[test_case]
| ~~~~~~~~~~~~
error: `bench` attribute cannot be used at crate level
--> $DIR/issue-36617.rs:10:1
|
LL | #![bench]
| ^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[bench]
| ~~~~~~~~
error: `global_allocator` attribute cannot be used at crate level
--> $DIR/issue-36617.rs:13:1
|
LL | #![global_allocator]
| ^^^^^^^^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[global_allocator]
| ~~~~~~~~~~~~~~~~~~~
error: aborting due to 10 previous errors

View File

@ -6,5 +6,5 @@
#![bench = "4100"] #![bench = "4100"]
//~^ ERROR cannot determine resolution for the attribute macro `bench` //~^ ERROR cannot determine resolution for the attribute macro `bench`
//~^^ ERROR `bench` attribute cannot be used at crate level
fn main() {} fn main() {}

View File

@ -6,5 +6,16 @@ LL | #![bench = "4100"]
| |
= note: import resolution is stuck, try simplifying macro imports = note: import resolution is stuck, try simplifying macro imports
error: aborting due to previous error error: `bench` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-bench.rs:7:1
|
LL | #![bench = "4100"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[bench = "4100"]
|
error: aborting due to 2 previous errors

View File

@ -129,36 +129,66 @@ error: `macro_export` attribute cannot be used at crate level
| |
LL | #![macro_export] LL | #![macro_export]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[macro_export]
|
error: `rustc_main` attribute cannot be used at crate level error: `rustc_main` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:14:1 --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:14:1
| |
LL | #![rustc_main] LL | #![rustc_main]
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[rustc_main]
| ~~~~~~~~~~~~~
error: `start` attribute cannot be used at crate level error: `start` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:16:1 --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:16:1
| |
LL | #![start] LL | #![start]
| ^^^^^^^^^ | ^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[start]
|
error: `repr` attribute cannot be used at crate level error: `repr` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:18:1 --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:18:1
| |
LL | #![repr()] LL | #![repr()]
| ^^^^^^^^^^ | ^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[repr()]
|
error: `path` attribute cannot be used at crate level error: `path` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:20:1 --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:20:1
| |
LL | #![path = "3800"] LL | #![path = "3800"]
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[path = "3800"]
|
error: `automatically_derived` attribute cannot be used at crate level error: `automatically_derived` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:22:1 --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:22:1
| |
LL | #![automatically_derived] LL | #![automatically_derived]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[automatically_derived]
|
error[E0518]: attribute should be applied to function or closure error[E0518]: attribute should be applied to function or closure
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:36:17 --> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:36:17

View File

@ -3,5 +3,5 @@
#![allow(soft_unstable)] #![allow(soft_unstable)]
#![test = "4200"] #![test = "4200"]
//~^ ERROR cannot determine resolution for the attribute macro `test` //~^ ERROR cannot determine resolution for the attribute macro `test`
//~^^ ERROR `test` attribute cannot be used at crate level
fn main() {} fn main() {}

View File

@ -6,5 +6,16 @@ LL | #![test = "4200"]
| |
= note: import resolution is stuck, try simplifying macro imports = note: import resolution is stuck, try simplifying macro imports
error: aborting due to previous error error: `test` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-test.rs:4:1
|
LL | #![test = "4200"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[test = "4200"]
|
error: aborting due to 2 previous errors

View File

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

View File

@ -6,5 +6,16 @@ LL | #![test]
| |
= note: import resolution is stuck, try simplifying macro imports = note: import resolution is stuck, try simplifying macro imports
error: aborting due to previous error error: `test` attribute cannot be used at crate level
--> $DIR/issue-28134.rs:4:1
|
LL | #![test]
| ^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[test]
| ~~~~~~~
error: aborting due to 2 previous errors

View File

@ -1,5 +1,6 @@
#![derive(Debug, PartialEq, Eq)] // should be an outer attribute! #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
//~^ ERROR cannot determine resolution for the attribute macro `derive` //~^ ERROR cannot determine resolution for the attribute macro `derive`
//~^^ ERROR `derive` attribute cannot be used at crate level
struct DerivedOn; struct DerivedOn;
fn main() {} fn main() {}

View File

@ -6,5 +6,16 @@ LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
| |
= note: import resolution is stuck, try simplifying macro imports = note: import resolution is stuck, try simplifying macro imports
error: aborting due to previous error error: `derive` attribute cannot be used at crate level
--> $DIR/issue-43927-non-ADT-derive.rs:1:1
|
LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[derive(Debug, PartialEq, Eq)] // should be an outer attribute!
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors