Skip to content

Commit cf39dc6

Browse files
Rollup merge of #157139 - scottmcm:abi-range-inclusive, r=oli-obk
compiler: `ops::RangeInclusive` → `range::RangeInclusive` The type's been stable for over 6 weeks now, so let's use it! It's better for cases like this one where it's stored in a data structure. Probably won't be materially faster, but does make the variant slightly smaller and lets some more things be `Copy`. And deletes a whole bunch of parens from things being fields instead of methods 😄 r? compiler
2 parents 1451099 + f7cf361 commit cf39dc6

11 files changed

Lines changed: 36 additions & 32 deletions

File tree

compiler/rustc_abi/src/layout.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::BTreeSet;
22
use std::fmt::{self, Write};
33
use std::ops::Deref;
4+
use std::range::RangeInclusive;
45
use std::{cmp, iter};
56

67
use rustc_hashes::Hash64;
@@ -631,11 +632,13 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
631632
let all_indices = variants.indices();
632633
let needs_disc =
633634
|index: VariantIdx| index != largest_variant_index && !absent(&variants[index]);
634-
let niche_variants = all_indices.clone().find(|v| needs_disc(*v)).unwrap()
635-
..=all_indices.rev().find(|v| needs_disc(*v)).unwrap();
635+
let niche_variants = RangeInclusive {
636+
start: all_indices.clone().find(|v| needs_disc(*v)).unwrap(),
637+
last: all_indices.rev().find(|v| needs_disc(*v)).unwrap(),
638+
};
636639

637640
let count =
638-
(niche_variants.end().index() as u128 - niche_variants.start().index() as u128) + 1;
641+
(niche_variants.last.index() as u128 - niche_variants.start.index() as u128) + 1;
639642

640643
// Use the largest niche in the largest variant.
641644
let niche = variant_layouts[largest_variant_index].largest_niche?;

compiler/rustc_abi/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ use std::fmt;
4040
#[cfg(feature = "nightly")]
4141
use std::iter::Step;
4242
use std::num::{NonZeroUsize, ParseIntError};
43-
use std::ops::{Add, AddAssign, Deref, Mul, RangeFull, RangeInclusive, Sub};
43+
use std::ops::{Add, AddAssign, Deref, Mul, RangeFull, Sub};
44+
use std::range::RangeInclusive;
4445
use std::str::FromStr;
4546

4647
use bitflags::bitflags;
@@ -1958,7 +1959,7 @@ pub enum Variants<FieldIdx: Idx, VariantIdx: Idx> {
19581959
}
19591960

19601961
// NOTE: This struct is generic over the VariantIdx for rust-analyzer usage.
1961-
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
1962+
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
19621963
#[cfg_attr(feature = "nightly", derive(StableHash))]
19631964
pub enum TagEncoding<VariantIdx: Idx> {
19641965
/// The tag directly stores the discriminant, but possibly with a smaller layout

compiler/rustc_codegen_cranelift/src/discriminant.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
5555
if variant_index != untagged_variant {
5656
let niche = place.place_field(fx, tag_field);
5757
let niche_type = fx.clif_type(niche.layout().ty).unwrap();
58-
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
58+
let niche_value = variant_index.as_u32() - niche_variants.start.as_u32();
5959
let niche_value = (niche_value as u128).wrapping_add(niche_start);
6060
let niche_value = match niche_type {
6161
types::I128 => {
@@ -133,7 +133,7 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
133133
dest.write_cvalue(fx, res);
134134
}
135135
TagEncoding::Niche { untagged_variant, ref niche_variants, niche_start } => {
136-
let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
136+
let relative_max = niche_variants.last.as_u32() - niche_variants.start.as_u32();
137137

138138
// We have a subrange `niche_start..=niche_end` inside `range`.
139139
// If the value of the tag is inside this subrange, it's a
@@ -162,7 +162,7 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
162162
// }
163163
let is_niche = codegen_icmp_imm(fx, IntCC::Equal, tag, niche_start as i128);
164164
let tagged_discr =
165-
fx.bcx.ins().iconst(cast_to, niche_variants.start().as_u32() as i64);
165+
fx.bcx.ins().iconst(cast_to, niche_variants.start.as_u32() as i64);
166166
(is_niche, tagged_discr, 0)
167167
} else {
168168
// The special cases don't apply, so we'll have to go with
@@ -184,7 +184,7 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
184184
relative_discr,
185185
i128::from(relative_max),
186186
);
187-
(is_niche, cast_tag, niche_variants.start().as_u32() as u128)
187+
(is_niche, cast_tag, niche_variants.start.as_u32() as u128)
188188
};
189189

190190
let tagged_discr = if delta == 0 {

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ fn compute_discriminant_value<'ll, 'tcx>(
426426
DiscrResult::Range(min, max)
427427
} else {
428428
let value = (variant_index.as_u32() as u128)
429-
.wrapping_sub(niche_variants.start().as_u32() as u128)
429+
.wrapping_sub(niche_variants.start.as_u32() as u128)
430430
.wrapping_add(niche_start);
431431
let value = tag.size(cx).truncate(value);
432432
DiscrResult::Value(value)

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
494494
// `layout_sanity_check` ensures that we only get here for cases where the discriminant
495495
// value and the variant index match, since that's all `Niche` can encode.
496496

497-
let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
497+
let relative_max = niche_variants.last.as_u32() - niche_variants.start.as_u32();
498498
let niche_start_const = bx.cx().const_uint_big(tag_llty, niche_start);
499499

500500
// We have a subrange `niche_start..=niche_end` inside `range`.
@@ -523,7 +523,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
523523
// }
524524
let is_niche = bx.icmp(IntPredicate::IntEQ, tag, niche_start_const);
525525
let tagged_discr =
526-
bx.cx().const_uint(cast_to, niche_variants.start().as_u32() as u64);
526+
bx.cx().const_uint(cast_to, niche_variants.start.as_u32() as u64);
527527
(is_niche, tagged_discr, 0)
528528
} else {
529529
// Thanks to parameter attributes and load metadata, LLVM already knows
@@ -549,7 +549,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
549549
{
550550
let impossible = niche_start
551551
.wrapping_add(u128::from(untagged_variant.as_u32()))
552-
.wrapping_sub(u128::from(niche_variants.start().as_u32()));
552+
.wrapping_sub(u128::from(niche_variants.start.as_u32()));
553553
let impossible = bx.cx().const_uint_big(tag_llty, impossible);
554554
let ne = bx.icmp(IntPredicate::IntNE, tag, impossible);
555555
bx.assume(ne);
@@ -633,7 +633,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
633633
)
634634
};
635635

636-
(is_niche, cast_tag, niche_variants.start().as_u32() as u128)
636+
(is_niche, cast_tag, niche_variants.start.as_u32() as u128)
637637
};
638638

639639
let tagged_discr = if delta == 0 {

compiler/rustc_codegen_ssa/src/mir/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ pub(super) fn codegen_tag_value<'tcx, V>(
500500
// around the `niche`'s type.
501501
// The easiest way to do that is to do wrapping arithmetic on `u128` and then
502502
// masking off any extra bits that occur because we did the arithmetic with too many bits.
503-
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
503+
let niche_value = variant_index.as_u32() - niche_variants.start.as_u32();
504504
let niche_value = (niche_value as u128).wrapping_add(niche_start);
505505
let niche_value = niche_value & niche_layout.size.unsigned_int_max();
506506

compiler/rustc_const_eval/src/interpret/discriminant.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,16 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
142142
let tag_val = tag_val.to_scalar();
143143
// Compute the variant this niche value/"tag" corresponds to. With niche layout,
144144
// discriminant (encoded in niche/tag) and variant index are the same.
145-
let variants_start = niche_variants.start().as_u32();
146-
let variants_end = niche_variants.end().as_u32();
145+
let variants_start = niche_variants.start.as_u32();
146+
let variants_last = niche_variants.last.as_u32();
147147
let variant = match tag_val.try_to_scalar_int() {
148148
Err(dbg_val) => {
149149
// So this is a pointer then, and casting to an int failed.
150150
// Can only happen during CTFE.
151151
// The niche must be just 0, and the ptr not null, then we know this is
152152
// okay. Everything else, we conservatively reject.
153153
let ptr_valid = niche_start == 0
154-
&& variants_start == variants_end
154+
&& variants_start == variants_last
155155
&& !self.scalar_may_be_null(tag_val)?;
156156
if !ptr_valid {
157157
throw_ub!(InvalidTag(dbg_val))
@@ -169,7 +169,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
169169
let variant_index_relative =
170170
variant_index_relative_val.to_scalar().to_bits(tag_val.layout.size)?;
171171
// Check if this is in the range that indicates an actual discriminant.
172-
if variant_index_relative <= u128::from(variants_end - variants_start) {
172+
if variant_index_relative <= u128::from(variants_last - variants_start) {
173173
let variant_index_relative = u32::try_from(variant_index_relative)
174174
.expect("we checked that this fits into a u32");
175175
// Then computing the absolute variant idx should not overflow any more.
@@ -309,7 +309,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
309309
niche_variants.contains(&variant_index),
310310
"invalid variant index for this enum"
311311
);
312-
let variants_start = niche_variants.start().as_u32();
312+
let variants_start = niche_variants.start.as_u32();
313313
let variant_index_relative = variant_index.as_u32().strict_sub(variants_start);
314314
// We need to use machine arithmetic when taking into account `niche_start`:
315315
// tag_val = variant_index_relative + niche_start_val

compiler/rustc_data_structures/src/stable_hash.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,14 @@ impl<T> StableHash for ::std::mem::Discriminant<T> {
527527
}
528528
}
529529

530-
impl<T> StableHash for ::std::ops::RangeInclusive<T>
530+
impl<T> StableHash for ::std::range::RangeInclusive<T>
531531
where
532532
T: StableHash,
533533
{
534534
#[inline]
535535
fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
536-
self.start().stable_hash(hcx, hasher);
537-
self.end().stable_hash(hcx, hasher);
536+
self.start.stable_hash(hcx, hasher);
537+
self.last.stable_hash(hcx, hasher);
538538
}
539539
}
540540

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ where
10971097
} else {
10981098
VariantIdx::from_u32(0)
10991099
};
1100-
assert_eq!(tagged_variant, *niche_variants.start());
1100+
assert_eq!(tagged_variant, niche_variants.start);
11011101
if *niche_start == 0 {
11021102
// The other variant is encoded as "null", so we can recurse searching for
11031103
// a pointer here. This relies on the fact that the codegen backend

compiler/rustc_public/src/unstable/convert/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! For contributors, please make sure to avoid calling rustc's internal functions and queries.
77
//! These should be done via `rustc_public_bridge` APIs, but it's possible to access ADT fields directly.
88
9-
use std::ops::RangeInclusive;
9+
use std::{ops, range};
1010

1111
use rustc_public_bridge::Tables;
1212
use rustc_public_bridge::context::CompilerCtxt;
@@ -95,16 +95,16 @@ where
9595
}
9696
}
9797

98-
impl<'tcx, T> Stable<'tcx> for RangeInclusive<T>
98+
impl<'tcx, T> Stable<'tcx> for range::RangeInclusive<T>
9999
where
100100
T: Stable<'tcx>,
101101
{
102-
type T = RangeInclusive<T::T>;
102+
type T = ops::RangeInclusive<T::T>;
103103
fn stable<'cx>(
104104
&self,
105105
tables: &mut Tables<'cx, BridgeTys>,
106106
cx: &CompilerCtxt<'cx, BridgeTys>,
107107
) -> Self::T {
108-
RangeInclusive::new(self.start().stable(tables, cx), self.end().stable(tables, cx))
108+
ops::RangeInclusive::new(self.start.stable(tables, cx), self.last.stable(tables, cx))
109109
}
110110
}

0 commit comments

Comments
 (0)