Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1953,28 +1953,52 @@ 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]) {
// 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] = &[
sym::macro_export,
sym::repr,
sym::path,
sym::automatically_derived,
sym::start,
sym::rustc_main,
sym::derive,
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
sym::test,
sym::test_case,
sym::global_allocator,
sym::bench,
];

for attr in attrs {
for attr_to_check in ATTRS_TO_CHECK {
if attr.has_name(*attr_to_check) {
tcx.sess
.struct_span_err(
// This function should only be called with crate attributes
// which are inner attributes always but lets check to make sure
if attr.style == AttrStyle::Inner {
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,
&format!(
"`{}` attribute cannot be used at crate level",
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()
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/derives/issue-36617.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#![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() {}
89 changes: 88 additions & 1 deletion src/test/ui/derives/issue-36617.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,92 @@ LL | #![derive(Copy)]
|
= 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

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

#![bench = "4100"]
//~^ ERROR cannot determine resolution for the attribute macro `bench`

//~^^ ERROR `bench` attribute cannot be used at crate level
fn main() {}
13 changes: 12 additions & 1 deletion src/test/ui/feature-gates/issue-43106-gating-of-bench.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ LL | #![bench = "4100"]
|
= 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

Original file line number Diff line number Diff line change
Expand Up @@ -129,36 +129,66 @@ error: `macro_export` attribute cannot be used at crate level
|
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
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:14:1
|
LL | #![rustc_main]
| ^^^^^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[rustc_main]
| ~~~~~~~~~~~~~

error: `start` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:16:1
|
LL | #![start]
| ^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[start]
|

error: `repr` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:18:1
|
LL | #![repr()]
| ^^^^^^^^^^
|
help: perhaps you meant to use an outer attribute
|
LL | #[repr()]
|

error: `path` attribute cannot be used at crate level
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:20:1
|
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
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:22:1
|
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
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:36:17
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/feature-gates/issue-43106-gating-of-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
#![allow(soft_unstable)]
#![test = "4200"]
//~^ ERROR cannot determine resolution for the attribute macro `test`

//~^^ ERROR `test` attribute cannot be used at crate level
fn main() {}
13 changes: 12 additions & 1 deletion src/test/ui/feature-gates/issue-43106-gating-of-test.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ LL | #![test = "4200"]
|
= 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

1 change: 1 addition & 0 deletions src/test/ui/imports/issue-28134.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

#![allow(soft_unstable)]
#![test] //~ ERROR cannot determine resolution for the attribute macro `test`
//~^ ERROR 4:1: 4:9: `test` attribute cannot be used at crate level
13 changes: 12 additions & 1 deletion src/test/ui/imports/issue-28134.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ LL | #![test]
|
= 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

1 change: 1 addition & 0 deletions src/test/ui/span/issue-43927-non-ADT-derive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
//~^ ERROR cannot determine resolution for the attribute macro `derive`
//~^^ ERROR `derive` attribute cannot be used at crate level
struct DerivedOn;

fn main() {}
13 changes: 12 additions & 1 deletion src/test/ui/span/issue-43927-non-ADT-derive.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,16 @@ LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
|
= 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