Skip to content

Commit dbd3ef1

Browse files
committed
fixup no_{core,std} handling code
1 parent 0db2eb7 commit dbd3ef1

File tree

8 files changed

+207
-92
lines changed

8 files changed

+207
-92
lines changed

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,20 @@ pub(crate) struct NoCoreParser;
182182
impl<S: Stage> NoArgsAttributeParser<S> for NoCoreParser {
183183
const PATH: &[Symbol] = &[sym::no_core];
184184
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
185-
// FIXME: recursion limit is allowed on all targets and ignored,
186-
// even though it should only be valid on crates of course
185+
// because it's a crate-level attribute, we already warn about it.
186+
// Putting target limitations here would give duplicate warnings
187187
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
188188
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoCore;
189189
const TYPE: AttributeType = AttributeType::CrateLevel;
190190
}
191191

192192
pub(crate) struct NoStdParser;
193+
193194
impl<S: Stage> NoArgsAttributeParser<S> for NoStdParser {
194195
const PATH: &[Symbol] = &[sym::no_std];
195196
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
196-
// FIXME: recursion limit is allowed on all targets and ignored,
197-
// even though it should only be valid on crates of course
197+
// because it's a crate-level attribute, we already warn about it.
198+
// Putting target limitations here would give duplicate warnings
198199
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
199200
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoStd;
200201
const TYPE: AttributeType = AttributeType::CrateLevel;

src/tools/clippy/clippy_utils/src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,17 +2133,11 @@ pub fn std_or_core(cx: &LateContext<'_>) -> Option<&'static str> {
21332133
}
21342134

21352135
pub fn is_no_std_crate(cx: &LateContext<'_>) -> bool {
2136-
cx.tcx
2137-
.hir_attrs(hir::CRATE_HIR_ID)
2138-
.iter()
2139-
.any(|attr| attr.has_name(sym::no_std))
2136+
find_attr!(cx.tcx.hir_attrs(hir::CRATE_HIR_ID), AttributeKind::NoStd(..))
21402137
}
21412138

21422139
pub fn is_no_core_crate(cx: &LateContext<'_>) -> bool {
2143-
cx.tcx
2144-
.hir_attrs(hir::CRATE_HIR_ID)
2145-
.iter()
2146-
.any(|attr| attr.has_name(sym::no_core))
2140+
find_attr!(cx.tcx.hir_attrs(hir::CRATE_HIR_ID), AttributeKind::NoCore(..))
21472141
}
21482142

21492143
/// Check if parent of a hir node is a trait implementation block.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![feature(no_core)]
2+
// these all still apply no_std and then later error
3+
#![no_std = "foo"]
4+
//~^ ERROR malformed `no_std` attribute input
5+
#![no_std("bar")]
6+
//~^ ERROR malformed `no_std` attribute input
7+
#![no_std(foo = "bar")]
8+
//~^ ERROR malformed `no_std` attribute input
9+
#![no_core = "foo"]
10+
//~^ ERROR malformed `no_core` attribute input
11+
#![no_core("bar")]
12+
//~^ ERROR malformed `no_core` attribute input
13+
#![no_core(foo = "bar")]
14+
//~^ ERROR malformed `no_core` attribute input
15+
16+
#[deny(unused_attributes)]
17+
#[no_std]
18+
//~^ ERROR crate-level attribute should be an inner attribute: add an exclamation mark:
19+
#[no_core]
20+
//~^ ERROR crate-level attribute should be an inner attribute: add an exclamation mark:
21+
// to fix compilation
22+
extern crate core;
23+
extern crate std;
24+
25+
fn main() {}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
error[E0565]: malformed `no_std` attribute input
2+
--> $DIR/malformed-no-std.rs:3:1
3+
|
4+
LL | #![no_std = "foo"]
5+
| ^^^^^^^^^^-------^
6+
| | |
7+
| | didn't expect any arguments here
8+
| help: must be of the form: `#![no_std]`
9+
10+
error[E0565]: malformed `no_std` attribute input
11+
--> $DIR/malformed-no-std.rs:5:1
12+
|
13+
LL | #![no_std("bar")]
14+
| ^^^^^^^^^-------^
15+
| | |
16+
| | didn't expect any arguments here
17+
| help: must be of the form: `#![no_std]`
18+
19+
error[E0565]: malformed `no_std` attribute input
20+
--> $DIR/malformed-no-std.rs:7:1
21+
|
22+
LL | #![no_std(foo = "bar")]
23+
| ^^^^^^^^^-------------^
24+
| | |
25+
| | didn't expect any arguments here
26+
| help: must be of the form: `#![no_std]`
27+
28+
error[E0565]: malformed `no_core` attribute input
29+
--> $DIR/malformed-no-std.rs:9:1
30+
|
31+
LL | #![no_core = "foo"]
32+
| ^^^^^^^^^^^-------^
33+
| | |
34+
| | didn't expect any arguments here
35+
| help: must be of the form: `#![no_core]`
36+
37+
error[E0565]: malformed `no_core` attribute input
38+
--> $DIR/malformed-no-std.rs:11:1
39+
|
40+
LL | #![no_core("bar")]
41+
| ^^^^^^^^^^-------^
42+
| | |
43+
| | didn't expect any arguments here
44+
| help: must be of the form: `#![no_core]`
45+
46+
error[E0565]: malformed `no_core` attribute input
47+
--> $DIR/malformed-no-std.rs:13:1
48+
|
49+
LL | #![no_core(foo = "bar")]
50+
| ^^^^^^^^^^-------------^
51+
| | |
52+
| | didn't expect any arguments here
53+
| help: must be of the form: `#![no_core]`
54+
55+
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_std]`
56+
--> $DIR/malformed-no-std.rs:17:1
57+
|
58+
LL | #[no_std]
59+
| ^^^^^^^^^
60+
|
61+
note: This attribute does not have an `!`, which means it is applied to this extern crate
62+
--> $DIR/malformed-no-std.rs:22:1
63+
|
64+
LL | extern crate core;
65+
| ^^^^^^^^^^^^^^^^^^
66+
note: the lint level is defined here
67+
--> $DIR/malformed-no-std.rs:16:8
68+
|
69+
LL | #[deny(unused_attributes)]
70+
| ^^^^^^^^^^^^^^^^^
71+
72+
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_core]`
73+
--> $DIR/malformed-no-std.rs:19:1
74+
|
75+
LL | #[no_core]
76+
| ^^^^^^^^^^
77+
|
78+
note: This attribute does not have an `!`, which means it is applied to this extern crate
79+
--> $DIR/malformed-no-std.rs:22:1
80+
|
81+
LL | extern crate core;
82+
| ^^^^^^^^^^^^^^^^^^
83+
84+
error: aborting due to 8 previous errors
85+
86+
For more information about this error, try `rustc --explain E0565`.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,26 +539,26 @@ mod macro_escape {
539539

540540
#[no_std]
541541
//~^ WARN crate-level attribute should be an inner attribute
542-
//~| HELP add a `!`
543542
mod no_std {
543+
//~^ NOTE This attribute does not have an `!`, which means it is applied to this module
544544
mod inner { #![no_std] }
545-
//~^ WARN crate-level attribute should be in the root module
545+
//~^ WARN the `#![no_std]` attribute can only be used at the crate root
546546

547547
#[no_std] fn f() { }
548548
//~^ WARN crate-level attribute should be an inner attribute
549-
//~| HELP add a `!`
549+
//~| NOTE This attribute does not have an `!`, which means it is applied to this function
550550

551551
#[no_std] struct S;
552552
//~^ WARN crate-level attribute should be an inner attribute
553-
//~| HELP add a `!`
553+
//~| NOTE This attribute does not have an `!`, which means it is applied to this struct
554554

555555
#[no_std] type T = S;
556556
//~^ WARN crate-level attribute should be an inner attribute
557-
//~| HELP add a `!`
557+
//~| NOTE This attribute does not have an `!`, which means it is applied to this type alias
558558

559559
#[no_std] impl S { }
560560
//~^ WARN crate-level attribute should be an inner attribute
561-
//~| HELP add a `!`
561+
//~| NOTE This attribute does not have an `!`, which means it is applied to this implementation block
562562
}
563563

564564
// At time of authorship, #[proc_macro_derive = "2500"] signals error

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.stderr

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,6 @@ help: add a `!`
209209
LL | #![reexport_test_harness_main = "2900"]
210210
| +
211211

212-
warning: crate-level attribute should be an inner attribute
213-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:540:1
214-
|
215-
LL | #[no_std]
216-
| ^^^^^^^^^
217-
|
218-
help: add a `!`
219-
|
220-
LL | #![no_std]
221-
| +
222-
223212
warning: attribute should be applied to an `extern` block with non-Rust ABI
224213
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:695:1
225214
|
@@ -387,56 +376,6 @@ help: add a `!`
387376
LL | #![reexport_test_harness_main = "2900"] impl S { }
388377
| +
389378

390-
warning: crate-level attribute should be in the root module
391-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:544:17
392-
|
393-
LL | mod inner { #![no_std] }
394-
| ^^^^^^^^^^
395-
396-
warning: crate-level attribute should be an inner attribute
397-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:547:5
398-
|
399-
LL | #[no_std] fn f() { }
400-
| ^^^^^^^^^
401-
|
402-
help: add a `!`
403-
|
404-
LL | #![no_std] fn f() { }
405-
| +
406-
407-
warning: crate-level attribute should be an inner attribute
408-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:551:5
409-
|
410-
LL | #[no_std] struct S;
411-
| ^^^^^^^^^
412-
|
413-
help: add a `!`
414-
|
415-
LL | #![no_std] struct S;
416-
| +
417-
418-
warning: crate-level attribute should be an inner attribute
419-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:555:5
420-
|
421-
LL | #[no_std] type T = S;
422-
| ^^^^^^^^^
423-
|
424-
help: add a `!`
425-
|
426-
LL | #![no_std] type T = S;
427-
| +
428-
429-
warning: crate-level attribute should be an inner attribute
430-
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:559:5
431-
|
432-
LL | #[no_std] impl S { }
433-
| ^^^^^^^^^
434-
|
435-
help: add a `!`
436-
|
437-
LL | #![no_std] impl S { }
438-
| +
439-
440379
warning: attribute should be applied to an `extern` block with non-Rust ABI
441380
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:701:17
442381
|
@@ -1095,6 +1034,76 @@ LL | #[macro_escape] impl S { }
10951034
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
10961035
= help: `#[macro_escape]` can be applied to modules, extern crates, and crates
10971036

1037+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_std]`
1038+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:540:1
1039+
|
1040+
LL | #[no_std]
1041+
| ^^^^^^^^^
1042+
|
1043+
note: This attribute does not have an `!`, which means it is applied to this module
1044+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:542:1
1045+
|
1046+
LL | / mod no_std {
1047+
LL | |
1048+
LL | | mod inner { #![no_std] }
1049+
... |
1050+
LL | | }
1051+
| |_^
1052+
1053+
warning: the `#![no_std]` attribute can only be used at the crate root
1054+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:544:17
1055+
|
1056+
LL | mod inner { #![no_std] }
1057+
| ^^^^^^^^^^
1058+
1059+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_std]`
1060+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:547:5
1061+
|
1062+
LL | #[no_std] fn f() { }
1063+
| ^^^^^^^^^
1064+
|
1065+
note: This attribute does not have an `!`, which means it is applied to this function
1066+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:547:15
1067+
|
1068+
LL | #[no_std] fn f() { }
1069+
| ^^^^^^^^^^
1070+
1071+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_std]`
1072+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:551:5
1073+
|
1074+
LL | #[no_std] struct S;
1075+
| ^^^^^^^^^
1076+
|
1077+
note: This attribute does not have an `!`, which means it is applied to this struct
1078+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:551:15
1079+
|
1080+
LL | #[no_std] struct S;
1081+
| ^^^^^^^^^
1082+
1083+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_std]`
1084+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:555:5
1085+
|
1086+
LL | #[no_std] type T = S;
1087+
| ^^^^^^^^^
1088+
|
1089+
note: This attribute does not have an `!`, which means it is applied to this type alias
1090+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:555:15
1091+
|
1092+
LL | #[no_std] type T = S;
1093+
| ^^^^^^^^^^^
1094+
1095+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_std]`
1096+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:559:5
1097+
|
1098+
LL | #[no_std] impl S { }
1099+
| ^^^^^^^^^
1100+
|
1101+
note: This attribute does not have an `!`, which means it is applied to this implementation block
1102+
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:559:15
1103+
|
1104+
LL | #[no_std] impl S { }
1105+
| ^^^^^^^^^^
1106+
10981107
warning: `#[cold]` attribute cannot be used on modules
10991108
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:581:1
11001109
|

tests/ui/lint/unused/unused-attr-duplicate.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,6 @@ note: attribute also specified here
2828
LL | #[no_link]
2929
| ^^^^^^^^^^
3030

31-
error: unused attribute
32-
--> $DIR/unused-attr-duplicate.rs:27:1
33-
|
34-
LL | #![no_std]
35-
| ^^^^^^^^^^ help: remove this attribute
36-
|
37-
note: attribute also specified here
38-
--> $DIR/unused-attr-duplicate.rs:26:1
39-
|
40-
LL | #![no_std]
41-
| ^^^^^^^^^^
42-
4331
error: unused attribute
4432
--> $DIR/unused-attr-duplicate.rs:31:1
4533
|
@@ -304,6 +292,18 @@ LL | #![type_length_limit = "1048576"]
304292
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
305293
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
306294

295+
error: unused attribute
296+
--> $DIR/unused-attr-duplicate.rs:27:1
297+
|
298+
LL | #![no_std]
299+
| ^^^^^^^^^^ help: remove this attribute
300+
|
301+
note: attribute also specified here
302+
--> $DIR/unused-attr-duplicate.rs:26:1
303+
|
304+
LL | #![no_std]
305+
| ^^^^^^^^^^
306+
307307
error: unused attribute
308308
--> $DIR/unused-attr-duplicate.rs:29:1
309309
|

tests/ui/unpretty/exhaustive.hir.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use self::prelude::*;
5555
*/
5656
#[doc = "inner doc attribute"]
5757
#[allow(dead_code, unused_variables)]
58-
#[no_std]
58+
#[attr = NoStd]
5959
mod attributes {
6060

6161
/// outer single-line doc comment

0 commit comments

Comments
 (0)