Skip to content

Commit 9642c0e

Browse files
committed
Auto merge of #146494 - jdonszelmann:rollup-0bbwwwf, r=jdonszelmann
Rollup of 5 pull requests Successful merges: - #146389 (Convert `no_std` and `no_core` to the new attribute infrastructure) - #146403 (sort array trait implementation suggestions correctly) - #146452 (Improve `alloc::Layout` coverage) - #146477 (Improve `core::char` coverage) - #146481 (Improve `core::hash` coverage) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4ba1cf9 + ec0f3bd commit 9642c0e

File tree

19 files changed

+345
-102
lines changed

19 files changed

+345
-102
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);

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use rustc_middle::ty::print::{
2727
with_forced_trimmed_paths,
2828
};
2929
use rustc_middle::ty::{
30-
self, TraitRef, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
31-
Upcast,
30+
self, GenericArgKind, TraitRef, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
31+
TypeVisitableExt, Upcast,
3232
};
3333
use rustc_middle::{bug, span_bug};
3434
use rustc_span::{BytePos, DUMMY_SP, STDLIB_STABLE_CRATES, Span, Symbol, sym};
@@ -2316,7 +2316,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
23162316
cand
23172317
})
23182318
.collect();
2319-
impl_candidates.sort_by_key(|cand| (cand.similarity, cand.trait_ref.to_string()));
2319+
impl_candidates.sort_by_key(|cand| {
2320+
// When suggesting array types, sort them by the length of the array, not lexicographically (#135098)
2321+
let len = if let GenericArgKind::Type(ty) = cand.trait_ref.args[0].kind()
2322+
&& let ty::Array(_, len) = ty.kind()
2323+
{
2324+
// Deprioritize suggestions for parameterized arrays.
2325+
len.try_to_target_usize(self.tcx).unwrap_or(u64::MAX)
2326+
} else {
2327+
0
2328+
};
2329+
2330+
(cand.similarity, len, cand.trait_ref.to_string())
2331+
});
23202332
let mut impl_candidates: Vec<_> =
23212333
impl_candidates.into_iter().map(|cand| cand.trait_ref).collect();
23222334
impl_candidates.dedup();

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/hash/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ fn test_writer_hasher() {
5353
assert_eq!(hash(&5_u16), 5);
5454
assert_eq!(hash(&5_u32), 5);
5555
assert_eq!(hash(&5_u64), 5);
56+
assert_eq!(hash(&5_u128), 5);
5657
assert_eq!(hash(&5_usize), 5);
5758

5859
assert_eq!(hash(&5_i8), 5);
5960
assert_eq!(hash(&5_i16), 5);
6061
assert_eq!(hash(&5_i32), 5);
6162
assert_eq!(hash(&5_i64), 5);
63+
assert_eq!(hash(&5_i128), 5);
6264
assert_eq!(hash(&5_isize), 5);
6365

6466
assert_eq!(hash(&false), 0);
@@ -85,6 +87,17 @@ fn test_writer_hasher() {
8587
let ptr = ptr::without_provenance_mut::<i32>(5_usize);
8688
assert_eq!(hash(&ptr), 5);
8789

90+
// Use a newtype to test the `Hash::hash_slice` default implementation.
91+
struct Byte(u8);
92+
93+
impl Hash for Byte {
94+
fn hash<H: Hasher>(&self, state: &mut H) {
95+
state.write_u8(self.0)
96+
}
97+
}
98+
99+
assert_eq!(hash(&[Byte(b'a')]), 97 + 1);
100+
88101
if cfg!(miri) {
89102
// Miri cannot hash pointers
90103
return;

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)]

0 commit comments

Comments
 (0)