Skip to content

Commit 573e565

Browse files
authored
Unrolled build for #146389
Rollup merge of #146389 - jdonszelmann:no-std, r=oli-obk Convert `no_std` and `no_core` to the new attribute infrastructure r? ```@oli-obk``` Also added a test for these, since we didn't have any and I was kind of surprised new diagnostics didn't break anything hehe
2 parents 4ba1cf9 + dbd3ef1 commit 573e565

File tree

12 files changed

+241
-91
lines changed

12 files changed

+241
-91
lines changed

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,27 @@ impl<S: Stage> SingleAttributeParser<S> for PatternComplexityLimitParser {
176176
})
177177
}
178178
}
179+
180+
pub(crate) struct NoCoreParser;
181+
182+
impl<S: Stage> NoArgsAttributeParser<S> for NoCoreParser {
183+
const PATH: &[Symbol] = &[sym::no_core];
184+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
185+
// because it's a crate-level attribute, we already warn about it.
186+
// Putting target limitations here would give duplicate warnings
187+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
188+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoCore;
189+
const TYPE: AttributeType = AttributeType::CrateLevel;
190+
}
191+
192+
pub(crate) struct NoStdParser;
193+
194+
impl<S: Stage> NoArgsAttributeParser<S> for NoStdParser {
195+
const PATH: &[Symbol] = &[sym::no_std];
196+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
197+
// because it's a crate-level attribute, we already warn about it.
198+
// Putting target limitations here would give duplicate warnings
199+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
200+
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NoStd;
201+
const TYPE: AttributeType = AttributeType::CrateLevel;
202+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use crate::attributes::codegen_attrs::{
2525
};
2626
use crate::attributes::confusables::ConfusablesParser;
2727
use crate::attributes::crate_level::{
28-
CrateNameParser, MoveSizeLimitParser, PatternComplexityLimitParser, RecursionLimitParser,
29-
TypeLengthLimitParser,
28+
CrateNameParser, MoveSizeLimitParser, NoCoreParser, NoStdParser, PatternComplexityLimitParser,
29+
RecursionLimitParser, TypeLengthLimitParser,
3030
};
3131
use crate::attributes::deprecation::DeprecationParser;
3232
use crate::attributes::dummy::DummyParser;
@@ -223,8 +223,10 @@ attribute_parsers!(
223223
Single<WithoutArgs<MacroEscapeParser>>,
224224
Single<WithoutArgs<MarkerParser>>,
225225
Single<WithoutArgs<MayDangleParser>>,
226+
Single<WithoutArgs<NoCoreParser>>,
226227
Single<WithoutArgs<NoImplicitPreludeParser>>,
227228
Single<WithoutArgs<NoMangleParser>>,
229+
Single<WithoutArgs<NoStdParser>>,
228230
Single<WithoutArgs<NonExhaustiveParser>>,
229231
Single<WithoutArgs<ParenSugarParser>>,
230232
Single<WithoutArgs<PassByValueParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,12 +579,18 @@ pub enum AttributeKind {
579579
/// Represents `#[naked]`
580580
Naked(Span),
581581

582+
/// Represents `#[no_core]`
583+
NoCore(Span),
584+
582585
/// Represents `#[no_implicit_prelude]`
583586
NoImplicitPrelude(Span),
584587

585588
/// Represents `#[no_mangle]`
586589
NoMangle(Span),
587590

591+
/// Represents `#[no_std]`
592+
NoStd(Span),
593+
588594
/// Represents `#[non_exhaustive]`
589595
NonExhaustive(Span),
590596

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ impl AttributeKind {
6464
MoveSizeLimit { .. } => No,
6565
MustUse { .. } => Yes,
6666
Naked(..) => No,
67+
NoCore(..) => No,
6768
NoImplicitPrelude(..) => No,
68-
NoMangle(..) => Yes, // Needed for rustdoc
69+
NoMangle(..) => Yes, // Needed for rustdoc
70+
NoStd(..) => No,
6971
NonExhaustive(..) => Yes, // Needed for rustdoc
7072
Optimize(..) => No,
7173
ParenSugar(..) => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
274274
| AttributeKind::MoveSizeLimit { .. }
275275
| AttributeKind::TypeLengthLimit { .. }
276276
| AttributeKind::PatternComplexityLimit { .. }
277+
| AttributeKind::NoCore { .. }
278+
| AttributeKind::NoStd { .. }
277279
) => { /* do nothing */ }
278280
Attribute::Unparsed(attr_item) => {
279281
style = Some(attr_item.style);

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

0 commit comments

Comments
 (0)