Skip to content

Commit 3827b64

Browse files
committed
Update invalid crate attributes, add help message
tidy run update invalid crate attributes, improve error update test outputs de-capitalise error update tests Update invalid crate attributes, add help message Update - generate span without using BytePos Add correct dependancies Update - generate suggestion without BytePos Tidy run update tests Generate Suggestion without BytePos Add all builtin attributes add err builtin inner attr at top of crate fix tests add err builtin inner attr at top of crate tidy fix add err builtin inner attr at top of crate
1 parent 2a9e083 commit 3827b64

12 files changed

+213
-13
lines changed

compiler/rustc_passes/src/check_attr.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -1953,28 +1953,52 @@ fn is_c_like_enum(item: &Item<'_>) -> bool {
19531953
}
19541954
}
19551955

1956+
// FIXME: Fix "Cannot determine resolution" error and remove built-in macros
1957+
// from this check.
19561958
fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
1959+
// Check for builtin attributes at the crate level
1960+
// which were unsuccessfully resolved due to cannot determine
1961+
// resolution for the attribute macro error.
19571962
const ATTRS_TO_CHECK: &[Symbol] = &[
19581963
sym::macro_export,
19591964
sym::repr,
19601965
sym::path,
19611966
sym::automatically_derived,
19621967
sym::start,
19631968
sym::rustc_main,
1969+
sym::derive,
1970+
sym::test,
1971+
sym::test_case,
1972+
sym::global_allocator,
1973+
sym::bench,
19641974
];
19651975

19661976
for attr in attrs {
1967-
for attr_to_check in ATTRS_TO_CHECK {
1968-
if attr.has_name(*attr_to_check) {
1969-
tcx.sess
1970-
.struct_span_err(
1977+
// This function should only be called with crate attributes
1978+
// which are inner attributes always but lets check to make sure
1979+
if attr.style == AttrStyle::Inner {
1980+
for attr_to_check in ATTRS_TO_CHECK {
1981+
if attr.has_name(*attr_to_check) {
1982+
let mut err = tcx.sess.struct_span_err(
19711983
attr.span,
19721984
&format!(
19731985
"`{}` attribute cannot be used at crate level",
19741986
attr_to_check.to_ident_string()
19751987
),
1976-
)
1977-
.emit();
1988+
);
1989+
// Only emit an error with a suggestion if we can create a
1990+
// string out of the attribute span
1991+
if let Ok(src) = tcx.sess.source_map().span_to_snippet(attr.span) {
1992+
let replacement = src.replace("#!", "#");
1993+
err.span_suggestion_verbose(
1994+
attr.span,
1995+
"perhaps you meant to use an outer attribute",
1996+
replacement,
1997+
rustc_errors::Applicability::MachineApplicable,
1998+
);
1999+
}
2000+
err.emit()
2001+
}
19782002
}
19792003
}
19802004
}

src/test/ui/derives/issue-36617.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
#![derive(Copy)] //~ ERROR cannot determine resolution for the attribute macro `derive`
2+
//~^ ERROR `derive` attribute cannot be used at crate level
3+
4+
#![test]//~ ERROR cannot determine resolution for the attribute macro `test`
5+
//~^ ERROR `test` attribute cannot be used at crate level
6+
7+
#![test_case]//~ ERROR cannot determine resolution for the attribute macro `test_case`
8+
//~^ ERROR `test_case` attribute cannot be used at crate level
9+
10+
#![bench]//~ ERROR cannot determine resolution for the attribute macro `bench`
11+
//~^ ERROR `bench` attribute cannot be used at crate level
12+
13+
#![global_allocator]//~ ERROR cannot determine resolution for the attribute macro `global_allocator`
14+
//~^ ERROR `global_allocator` attribute cannot be used at crate level
215

316
fn main() {}

src/test/ui/derives/issue-36617.stderr

+88-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,92 @@ LL | #![derive(Copy)]
66
|
77
= note: import resolution is stuck, try simplifying macro imports
88

9-
error: aborting due to previous error
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+
41+
error: `derive` attribute cannot be used at crate level
42+
--> $DIR/issue-36617.rs:1:1
43+
|
44+
LL | #![derive(Copy)]
45+
| ^^^^^^^^^^^^^^^^
46+
|
47+
help: perhaps you meant to use an outer attribute
48+
|
49+
LL | #[derive(Copy)]
50+
| ~~~~~~~~~~~~~~~
51+
52+
error: `test` attribute cannot be used at crate level
53+
--> $DIR/issue-36617.rs:4:1
54+
|
55+
LL | #![test]
56+
| ^^^^^^^^
57+
|
58+
help: perhaps you meant to use an outer attribute
59+
|
60+
LL | #[test]
61+
| ~~~~~~~
62+
63+
error: `test_case` attribute cannot be used at crate level
64+
--> $DIR/issue-36617.rs:7:1
65+
|
66+
LL | #![test_case]
67+
| ^^^^^^^^^^^^^
68+
|
69+
help: perhaps you meant to use an outer attribute
70+
|
71+
LL | #[test_case]
72+
| ~~~~~~~~~~~~
73+
74+
error: `bench` attribute cannot be used at crate level
75+
--> $DIR/issue-36617.rs:10:1
76+
|
77+
LL | #![bench]
78+
| ^^^^^^^^^
79+
|
80+
help: perhaps you meant to use an outer attribute
81+
|
82+
LL | #[bench]
83+
| ~~~~~~~~
84+
85+
error: `global_allocator` attribute cannot be used at crate level
86+
--> $DIR/issue-36617.rs:13:1
87+
|
88+
LL | #![global_allocator]
89+
| ^^^^^^^^^^^^^^^^^^^^
90+
|
91+
help: perhaps you meant to use an outer attribute
92+
|
93+
LL | #[global_allocator]
94+
| ~~~~~~~~~~~~~~~~~~~
95+
96+
error: aborting due to 10 previous errors
1097

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

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

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

src/test/ui/feature-gates/issue-43106-gating-of-bench.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,16 @@ LL | #![bench = "4100"]
66
|
77
= note: import resolution is stuck, try simplifying macro imports
88

9-
error: aborting due to previous error
9+
error: `bench` attribute cannot be used at crate level
10+
--> $DIR/issue-43106-gating-of-bench.rs:7:1
11+
|
12+
LL | #![bench = "4100"]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
help: perhaps you meant to use an outer attribute
16+
|
17+
LL | #[bench = "4100"]
18+
|
19+
20+
error: aborting due to 2 previous errors
1021

src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs-error.stderr

+30
Original file line numberDiff line numberDiff line change
@@ -129,36 +129,66 @@ error: `macro_export` attribute cannot be used at crate level
129129
|
130130
LL | #![macro_export]
131131
| ^^^^^^^^^^^^^^^^
132+
|
133+
help: perhaps you meant to use an outer attribute
134+
|
135+
LL | #[macro_export]
136+
|
132137

133138
error: `rustc_main` attribute cannot be used at crate level
134139
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:14:1
135140
|
136141
LL | #![rustc_main]
137142
| ^^^^^^^^^^^^^^
143+
|
144+
help: perhaps you meant to use an outer attribute
145+
|
146+
LL | #[rustc_main]
147+
| ~~~~~~~~~~~~~
138148

139149
error: `start` attribute cannot be used at crate level
140150
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:16:1
141151
|
142152
LL | #![start]
143153
| ^^^^^^^^^
154+
|
155+
help: perhaps you meant to use an outer attribute
156+
|
157+
LL | #[start]
158+
|
144159

145160
error: `repr` attribute cannot be used at crate level
146161
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:18:1
147162
|
148163
LL | #![repr()]
149164
| ^^^^^^^^^^
165+
|
166+
help: perhaps you meant to use an outer attribute
167+
|
168+
LL | #[repr()]
169+
|
150170

151171
error: `path` attribute cannot be used at crate level
152172
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:20:1
153173
|
154174
LL | #![path = "3800"]
155175
| ^^^^^^^^^^^^^^^^^
176+
|
177+
help: perhaps you meant to use an outer attribute
178+
|
179+
LL | #[path = "3800"]
180+
|
156181

157182
error: `automatically_derived` attribute cannot be used at crate level
158183
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:22:1
159184
|
160185
LL | #![automatically_derived]
161186
| ^^^^^^^^^^^^^^^^^^^^^^^^^
187+
|
188+
help: perhaps you meant to use an outer attribute
189+
|
190+
LL | #[automatically_derived]
191+
|
162192

163193
error[E0518]: attribute should be applied to function or closure
164194
--> $DIR/issue-43106-gating-of-builtin-attrs-error.rs:36:17

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
#![allow(soft_unstable)]
44
#![test = "4200"]
55
//~^ ERROR cannot determine resolution for the attribute macro `test`
6-
6+
//~^^ ERROR `test` attribute cannot be used at crate level
77
fn main() {}

src/test/ui/feature-gates/issue-43106-gating-of-test.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,16 @@ LL | #![test = "4200"]
66
|
77
= note: import resolution is stuck, try simplifying macro imports
88

9-
error: aborting due to previous error
9+
error: `test` attribute cannot be used at crate level
10+
--> $DIR/issue-43106-gating-of-test.rs:4:1
11+
|
12+
LL | #![test = "4200"]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
help: perhaps you meant to use an outer attribute
16+
|
17+
LL | #[test = "4200"]
18+
|
19+
20+
error: aborting due to 2 previous errors
1021

src/test/ui/imports/issue-28134.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

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

src/test/ui/imports/issue-28134.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,16 @@ LL | #![test]
66
|
77
= note: import resolution is stuck, try simplifying macro imports
88

9-
error: aborting due to previous error
9+
error: `test` attribute cannot be used at crate level
10+
--> $DIR/issue-28134.rs:4:1
11+
|
12+
LL | #![test]
13+
| ^^^^^^^^
14+
|
15+
help: perhaps you meant to use an outer attribute
16+
|
17+
LL | #[test]
18+
| ~~~~~~~
19+
20+
error: aborting due to 2 previous errors
1021

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
22
//~^ ERROR cannot determine resolution for the attribute macro `derive`
3+
//~^^ ERROR `derive` attribute cannot be used at crate level
34
struct DerivedOn;
45

56
fn main() {}

src/test/ui/span/issue-43927-non-ADT-derive.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,16 @@ LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
66
|
77
= note: import resolution is stuck, try simplifying macro imports
88

9-
error: aborting due to previous error
9+
error: `derive` attribute cannot be used at crate level
10+
--> $DIR/issue-43927-non-ADT-derive.rs:1:1
11+
|
12+
LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
help: perhaps you meant to use an outer attribute
16+
|
17+
LL | #[derive(Debug, PartialEq, Eq)] // should be an outer attribute!
18+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
20+
error: aborting due to 2 previous errors
1021

0 commit comments

Comments
 (0)