Skip to content

Commit 16288b8

Browse files
committed
Auto merge of #146492 - jdonszelmann:rollup-ea91wde, r=jdonszelmann
Rollup of 4 pull requests Successful merges: - #146389 (Convert `no_std` and `no_core` to the new attribute infrastructure) - #146452 (Improve `alloc::Layout` coverage) - #146473 (Revert "Constify SystemTime methods") - #146477 (Improve `core::char` coverage) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4ba1cf9 + 7078e76 commit 16288b8

File tree

26 files changed

+377
-256
lines changed

26 files changed

+377
-256
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);

library/coretests/tests/alloc.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ fn layout_array_edge_cases() {
5555
}
5656
}
5757

58+
#[test]
59+
fn layout_errors() {
60+
let layout = Layout::new::<[u8; 2]>();
61+
// Should error if the alignment is not a power of two.
62+
assert!(layout.align_to(3).is_err());
63+
64+
// The remaining assertions ensure that the methods error on arithmetic overflow as the
65+
// alignment cannot overflow `isize`.
66+
let size = layout.size();
67+
let size_max = isize::MAX as usize;
68+
let align_max = size_max / size;
69+
70+
assert!(layout.align_to(size_max + 1).is_err());
71+
72+
assert!(layout.repeat(align_max).is_ok());
73+
assert!(layout.repeat(align_max + 1).is_err());
74+
75+
assert!(layout.repeat_packed(align_max).is_ok());
76+
assert!(layout.repeat_packed(align_max + 1).is_err());
77+
78+
let next = Layout::from_size_align(size_max, 1).unwrap();
79+
assert!(layout.extend(next).is_err());
80+
}
81+
5882
#[test]
5983
fn layout_debug_shows_log2_of_alignment() {
6084
// `Debug` is not stable, but here's what it does right now

library/coretests/tests/char.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ fn test_escape_default() {
220220
}
221221
assert_eq!(string('\n'), "\\n");
222222
assert_eq!(string('\r'), "\\r");
223+
assert_eq!(string('\t'), "\\t");
223224
assert_eq!(string('\''), "\\'");
224225
assert_eq!(string('"'), "\\\"");
225226
assert_eq!(string(' '), " ");
@@ -417,3 +418,45 @@ fn eu_iterator_specializations() {
417418
check('\u{12340}');
418419
check('\u{10FFFF}');
419420
}
421+
422+
#[test]
423+
#[should_panic]
424+
fn test_from_digit_radix_too_high() {
425+
let _ = char::from_digit(0, 37);
426+
}
427+
428+
#[test]
429+
fn test_from_digit_invalid_radix() {
430+
assert!(char::from_digit(10, 9).is_none());
431+
}
432+
433+
#[test]
434+
#[should_panic]
435+
fn test_to_digit_radix_too_low() {
436+
let _ = 'a'.to_digit(1);
437+
}
438+
439+
#[test]
440+
#[should_panic]
441+
fn test_to_digit_radix_too_high() {
442+
let _ = 'a'.to_digit(37);
443+
}
444+
445+
#[test]
446+
fn test_as_ascii_invalid() {
447+
assert!('❤'.as_ascii().is_none());
448+
}
449+
450+
#[test]
451+
#[should_panic]
452+
fn test_encode_utf8_raw_buffer_too_small() {
453+
let mut buf = [0u8; 1];
454+
let _ = char::encode_utf8_raw('ß'.into(), &mut buf);
455+
}
456+
457+
#[test]
458+
#[should_panic]
459+
fn test_encode_utf16_raw_buffer_too_small() {
460+
let mut buf = [0u16; 1];
461+
let _ = char::encode_utf16_raw('𐐷'.into(), &mut buf);
462+
}

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(bool_to_result)]
1414
#![feature(bstr)]
1515
#![feature(cfg_target_has_reliable_f16_f128)]
16+
#![feature(char_internals)]
1617
#![feature(char_max_len)]
1718
#![feature(clone_to_uninit)]
1819
#![feature(const_cmp)]

library/std/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@
284284
#![feature(core_float_math)]
285285
#![feature(decl_macro)]
286286
#![feature(deprecated_suggestion)]
287-
#![feature(derive_const)]
288287
#![feature(doc_cfg)]
289288
#![feature(doc_cfg_hide)]
290289
#![feature(doc_masked)]
@@ -332,11 +331,7 @@
332331
#![feature(cfg_select)]
333332
#![feature(char_internals)]
334333
#![feature(clone_to_uninit)]
335-
#![feature(const_cmp)]
336334
#![feature(const_convert)]
337-
#![feature(const_ops)]
338-
#![feature(const_option_ops)]
339-
#![feature(const_try)]
340335
#![feature(core_intrinsics)]
341336
#![feature(core_io_borrowed_buf)]
342337
#![feature(drop_guard)]

library/std/src/sys/pal/hermit/time.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,8 @@ impl Timespec {
2525
Timespec { t: timespec { tv_sec, tv_nsec } }
2626
}
2727

28-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
29-
const fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
30-
// FIXME: const PartialOrd
31-
let mut cmp = self.t.tv_sec - other.t.tv_sec;
32-
if cmp == 0 {
33-
cmp = self.t.tv_nsec as i64 - other.t.tv_nsec as i64;
34-
}
35-
36-
if cmp >= 0 {
28+
fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
29+
if self >= other {
3730
Ok(if self.t.tv_nsec >= other.t.tv_nsec {
3831
Duration::new(
3932
(self.t.tv_sec - other.t.tv_sec) as u64,
@@ -53,22 +46,20 @@ impl Timespec {
5346
}
5447
}
5548

56-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
57-
const fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
49+
fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
5850
let mut secs = self.t.tv_sec.checked_add_unsigned(other.as_secs())?;
5951

6052
// Nano calculations can't overflow because nanos are <1B which fit
6153
// in a u32.
62-
let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
63-
if nsec >= NSEC_PER_SEC as u32 {
64-
nsec -= NSEC_PER_SEC as u32;
54+
let mut nsec = other.subsec_nanos() + u32::try_from(self.t.tv_nsec).unwrap();
55+
if nsec >= NSEC_PER_SEC.try_into().unwrap() {
56+
nsec -= u32::try_from(NSEC_PER_SEC).unwrap();
6557
secs = secs.checked_add(1)?;
6658
}
6759
Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
6860
}
6961

70-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
71-
const fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
62+
fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
7263
let mut secs = self.t.tv_sec.checked_sub_unsigned(other.as_secs())?;
7364

7465
// Similar to above, nanos can't overflow.
@@ -222,18 +213,15 @@ impl SystemTime {
222213
SystemTime(time)
223214
}
224215

225-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
226-
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
216+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
227217
self.0.sub_timespec(&other.0)
228218
}
229219

230-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
231-
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
220+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
232221
Some(SystemTime(self.0.checked_add_duration(other)?))
233222
}
234223

235-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
236-
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
224+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
237225
Some(SystemTime(self.0.checked_sub_duration(other)?))
238226
}
239227
}

0 commit comments

Comments
 (0)