Skip to content

Commit 8df27d0

Browse files
committed
Remove some superfluous type parameters from layout.rs.
Specifically remove V, which can always be VariantIdx, and F, which can always be Layout.
1 parent 21f6839 commit 8df27d0

File tree

7 files changed

+148
-149
lines changed

7 files changed

+148
-149
lines changed

compiler/rustc_abi/src/layout.rs

+71-75
Large diffs are not rendered by default.

compiler/rustc_abi/src/lib.rs

+59-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::ops::{Add, AddAssign, Mul, RangeInclusive, Sub};
88
use std::str::FromStr;
99

1010
use bitflags::bitflags;
11+
use rustc_data_structures::intern::Interned;
1112
#[cfg(feature = "nightly")]
1213
use rustc_data_structures::stable_hasher::StableOrd;
1314
use rustc_index::vec::{Idx, IndexVec};
@@ -1257,9 +1258,9 @@ impl Abi {
12571258

12581259
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
12591260
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
1260-
pub enum Variants<V: Idx> {
1261+
pub enum Variants {
12611262
/// Single enum variants, structs/tuples, unions, and all non-ADTs.
1262-
Single { index: V },
1263+
Single { index: VariantIdx },
12631264

12641265
/// Enum-likes with more than one inhabited variant: each variant comes with
12651266
/// a *discriminant* (usually the same as the variant index but the user can
@@ -1269,15 +1270,15 @@ pub enum Variants<V: Idx> {
12691270
/// For enums, the tag is the sole field of the layout.
12701271
Multiple {
12711272
tag: Scalar,
1272-
tag_encoding: TagEncoding<V>,
1273+
tag_encoding: TagEncoding,
12731274
tag_field: usize,
1274-
variants: IndexVec<V, LayoutS<V>>,
1275+
variants: IndexVec<VariantIdx, LayoutS>,
12751276
},
12761277
}
12771278

12781279
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
12791280
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
1280-
pub enum TagEncoding<V: Idx> {
1281+
pub enum TagEncoding {
12811282
/// The tag directly stores the discriminant, but possibly with a smaller layout
12821283
/// (so converting the tag to the discriminant can require sign extension).
12831284
Direct,
@@ -1292,7 +1293,11 @@ pub enum TagEncoding<V: Idx> {
12921293
/// For example, `Option<(usize, &T)>` is represented such that
12931294
/// `None` has a null pointer for the second tuple field, and
12941295
/// `Some` is the identity function (with a non-null reference).
1295-
Niche { untagged_variant: V, niche_variants: RangeInclusive<V>, niche_start: u128 },
1296+
Niche {
1297+
untagged_variant: VariantIdx,
1298+
niche_variants: RangeInclusive<VariantIdx>,
1299+
niche_start: u128,
1300+
},
12961301
}
12971302

12981303
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
@@ -1379,9 +1384,14 @@ impl Niche {
13791384
}
13801385
}
13811386

1387+
rustc_index::newtype_index! {
1388+
#[derive(HashStable_Generic)]
1389+
pub struct VariantIdx {}
1390+
}
1391+
13821392
#[derive(PartialEq, Eq, Hash, Clone)]
13831393
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
1384-
pub struct LayoutS<V: Idx> {
1394+
pub struct LayoutS {
13851395
/// Says where the fields are located within the layout.
13861396
pub fields: FieldsShape,
13871397

@@ -1392,7 +1402,7 @@ pub struct LayoutS<V: Idx> {
13921402
///
13931403
/// To access all fields of this layout, both `fields` and the fields of the active variant
13941404
/// must be taken into account.
1395-
pub variants: Variants<V>,
1405+
pub variants: Variants,
13961406

13971407
/// The `abi` defines how this data is passed between functions, and it defines
13981408
/// value restrictions via `valid_range`.
@@ -1411,13 +1421,13 @@ pub struct LayoutS<V: Idx> {
14111421
pub size: Size,
14121422
}
14131423

1414-
impl<V: Idx> LayoutS<V> {
1424+
impl LayoutS {
14151425
pub fn scalar<C: HasDataLayout>(cx: &C, scalar: Scalar) -> Self {
14161426
let largest_niche = Niche::from_scalar(cx, Size::ZERO, scalar);
14171427
let size = scalar.size(cx);
14181428
let align = scalar.align(cx);
14191429
LayoutS {
1420-
variants: Variants::Single { index: V::new(0) },
1430+
variants: Variants::Single { index: VariantIdx::new(0) },
14211431
fields: FieldsShape::Primitive,
14221432
abi: Abi::Scalar(scalar),
14231433
largest_niche,
@@ -1427,7 +1437,7 @@ impl<V: Idx> LayoutS<V> {
14271437
}
14281438
}
14291439

1430-
impl<V: Idx> fmt::Debug for LayoutS<V> {
1440+
impl fmt::Debug for LayoutS {
14311441
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14321442
// This is how `Layout` used to print before it become
14331443
// `Interned<LayoutS>`. We print it like this to avoid having to update
@@ -1444,6 +1454,43 @@ impl<V: Idx> fmt::Debug for LayoutS<V> {
14441454
}
14451455
}
14461456

1457+
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
1458+
#[rustc_pass_by_value]
1459+
pub struct Layout<'a>(pub Interned<'a, LayoutS>);
1460+
1461+
impl<'a> fmt::Debug for Layout<'a> {
1462+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1463+
// See comment on `<LayoutS as Debug>::fmt` above.
1464+
self.0.0.fmt(f)
1465+
}
1466+
}
1467+
1468+
impl<'a> Layout<'a> {
1469+
pub fn fields(self) -> &'a FieldsShape {
1470+
&self.0.0.fields
1471+
}
1472+
1473+
pub fn variants(self) -> &'a Variants {
1474+
&self.0.0.variants
1475+
}
1476+
1477+
pub fn abi(self) -> Abi {
1478+
self.0.0.abi
1479+
}
1480+
1481+
pub fn largest_niche(self) -> Option<Niche> {
1482+
self.0.0.largest_niche
1483+
}
1484+
1485+
pub fn align(self) -> AbiAndPrefAlign {
1486+
self.0.0.align
1487+
}
1488+
1489+
pub fn size(self) -> Size {
1490+
self.0.0.size
1491+
}
1492+
}
1493+
14471494
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
14481495
pub enum PointerKind {
14491496
/// Most general case, we know no restrictions to tell LLVM.
@@ -1479,7 +1526,7 @@ pub enum InitKind {
14791526
UninitMitigated0x01Fill,
14801527
}
14811528

1482-
impl<V: Idx> LayoutS<V> {
1529+
impl LayoutS {
14831530
/// Returns `true` if the layout corresponds to an unsized type.
14841531
pub fn is_unsized(&self) -> bool {
14851532
self.abi.is_unsized()

compiler/rustc_middle/src/arena.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
macro_rules! arena_types {
99
($macro:path) => (
1010
$macro!([
11-
[] layout: rustc_target::abi::LayoutS<rustc_target::abi::VariantIdx>,
11+
[] layout: rustc_target::abi::LayoutS,
1212
[] fn_abi: rustc_target::abi::call::FnAbi<'tcx, rustc_middle::ty::Ty<'tcx>>,
1313
// AdtDef are interned and compared by address
1414
[decode] adt_def: rustc_middle::ty::AdtDefData,

compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub struct CtxtInterners<'tcx> {
142142
const_: InternedSet<'tcx, ConstData<'tcx>>,
143143
const_allocation: InternedSet<'tcx, Allocation>,
144144
bound_variable_kinds: InternedSet<'tcx, List<ty::BoundVariableKind>>,
145-
layout: InternedSet<'tcx, LayoutS<VariantIdx>>,
145+
layout: InternedSet<'tcx, LayoutS>,
146146
adt_def: InternedSet<'tcx, AdtDefData>,
147147
}
148148

@@ -1586,7 +1586,7 @@ direct_interners! {
15861586
region: mk_region(RegionKind<'tcx>): Region -> Region<'tcx>,
15871587
const_: mk_const_internal(ConstData<'tcx>): Const -> Const<'tcx>,
15881588
const_allocation: intern_const_alloc(Allocation): ConstAllocation -> ConstAllocation<'tcx>,
1589-
layout: intern_layout(LayoutS<VariantIdx>): Layout -> Layout<'tcx>,
1589+
layout: intern_layout(LayoutS): Layout -> Layout<'tcx>,
15901590
adt_def: intern_adt_def(AdtDefData): AdtDef -> AdtDef<'tcx>,
15911591
}
15921592

compiler/rustc_target/src/abi/mod.rs

+2-46
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ pub use Primitive::*;
33

44
use crate::json::{Json, ToJson};
55

6-
use std::fmt;
76
use std::ops::Deref;
87

9-
use rustc_data_structures::intern::Interned;
108
use rustc_macros::HashStable_Generic;
119

1210
pub mod call;
@@ -19,48 +17,6 @@ impl ToJson for Endian {
1917
}
2018
}
2119

22-
rustc_index::newtype_index! {
23-
#[derive(HashStable_Generic)]
24-
pub struct VariantIdx {}
25-
}
26-
27-
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
28-
#[rustc_pass_by_value]
29-
pub struct Layout<'a>(pub Interned<'a, LayoutS<VariantIdx>>);
30-
31-
impl<'a> fmt::Debug for Layout<'a> {
32-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33-
// See comment on `<LayoutS as Debug>::fmt` above.
34-
self.0.0.fmt(f)
35-
}
36-
}
37-
38-
impl<'a> Layout<'a> {
39-
pub fn fields(self) -> &'a FieldsShape {
40-
&self.0.0.fields
41-
}
42-
43-
pub fn variants(self) -> &'a Variants<VariantIdx> {
44-
&self.0.0.variants
45-
}
46-
47-
pub fn abi(self) -> Abi {
48-
self.0.0.abi
49-
}
50-
51-
pub fn largest_niche(self) -> Option<Niche> {
52-
self.0.0.largest_niche
53-
}
54-
55-
pub fn align(self) -> AbiAndPrefAlign {
56-
self.0.0.align
57-
}
58-
59-
pub fn size(self) -> Size {
60-
self.0.0.size
61-
}
62-
}
63-
6420
/// The layout of a type, alongside the type itself.
6521
/// Provides various type traversal APIs (e.g., recursing into fields).
6622
///
@@ -75,8 +31,8 @@ pub struct TyAndLayout<'a, Ty> {
7531
}
7632

7733
impl<'a, Ty> Deref for TyAndLayout<'a, Ty> {
78-
type Target = &'a LayoutS<VariantIdx>;
79-
fn deref(&self) -> &&'a LayoutS<VariantIdx> {
34+
type Target = &'a LayoutS;
35+
fn deref(&self) -> &&'a LayoutS {
8036
&self.layout.0.0
8137
}
8238
}

compiler/rustc_ty_utils/src/layout.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ fn invert_mapping(map: &[u32]) -> Vec<u32> {
7878
fn univariant_uninterned<'tcx>(
7979
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
8080
ty: Ty<'tcx>,
81-
fields: &[TyAndLayout<'_>],
81+
fields: &[Layout<'_>],
8282
repr: &ReprOptions,
8383
kind: StructKind,
84-
) -> Result<LayoutS<VariantIdx>, LayoutError<'tcx>> {
84+
) -> Result<LayoutS, LayoutError<'tcx>> {
8585
let dl = cx.data_layout();
8686
let pack = repr.pack;
8787
if pack.is_some() && repr.align.is_some() {
@@ -106,7 +106,7 @@ fn layout_of_uncached<'tcx>(
106106
};
107107
let scalar = |value: Primitive| tcx.intern_layout(LayoutS::scalar(cx, scalar_unit(value)));
108108

109-
let univariant = |fields: &[TyAndLayout<'_>], repr: &ReprOptions, kind| {
109+
let univariant = |fields: &[Layout<'_>], repr: &ReprOptions, kind| {
110110
Ok(tcx.intern_layout(univariant_uninterned(cx, ty, fields, repr, kind)?))
111111
};
112112
debug_assert!(!ty.has_non_region_infer());
@@ -272,7 +272,7 @@ fn layout_of_uncached<'tcx>(
272272
ty::Closure(_, ref substs) => {
273273
let tys = substs.as_closure().upvar_tys();
274274
univariant(
275-
&tys.map(|ty| cx.layout_of(ty)).collect::<Result<Vec<_>, _>>()?,
275+
&tys.map(|ty| Ok(cx.layout_of(ty)?.layout)).collect::<Result<Vec<_>, _>>()?,
276276
&ReprOptions::default(),
277277
StructKind::AlwaysSized,
278278
)?
@@ -283,7 +283,7 @@ fn layout_of_uncached<'tcx>(
283283
if tys.len() == 0 { StructKind::AlwaysSized } else { StructKind::MaybeUnsized };
284284

285285
univariant(
286-
&tys.iter().map(|k| cx.layout_of(k)).collect::<Result<Vec<_>, _>>()?,
286+
&tys.iter().map(|k| Ok(cx.layout_of(k)?.layout)).collect::<Result<Vec<_>, _>>()?,
287287
&ReprOptions::default(),
288288
kind,
289289
)?
@@ -412,7 +412,7 @@ fn layout_of_uncached<'tcx>(
412412
.map(|v| {
413413
v.fields
414414
.iter()
415-
.map(|field| cx.layout_of(field.ty(tcx, substs)))
415+
.map(|field| Ok(cx.layout_of(field.ty(tcx, substs))?.layout))
416416
.collect::<Result<Vec<_>, _>>()
417417
})
418418
.collect::<Result<IndexVec<VariantIdx, _>, _>>()?;
@@ -630,23 +630,21 @@ fn generator_layout<'tcx>(
630630
// `info.variant_fields` already accounts for the reserved variants, so no need to add them.
631631
let max_discr = (info.variant_fields.len() - 1) as u128;
632632
let discr_int = Integer::fit_unsigned(max_discr);
633-
let discr_int_ty = discr_int.to_ty(tcx, false);
634633
let tag = Scalar::Initialized {
635634
value: Primitive::Int(discr_int, false),
636635
valid_range: WrappingRange { start: 0, end: max_discr },
637636
};
638637
let tag_layout = cx.tcx.intern_layout(LayoutS::scalar(cx, tag));
639-
let tag_layout = TyAndLayout { ty: discr_int_ty, layout: tag_layout };
640638

641639
let promoted_layouts = ineligible_locals
642640
.iter()
643641
.map(|local| subst_field(info.field_tys[local]))
644642
.map(|ty| tcx.mk_maybe_uninit(ty))
645-
.map(|ty| cx.layout_of(ty));
643+
.map(|ty| Ok(cx.layout_of(ty)?.layout));
646644
let prefix_layouts = substs
647645
.as_generator()
648646
.prefix_tys()
649-
.map(|ty| cx.layout_of(ty))
647+
.map(|ty| Ok(cx.layout_of(ty)?.layout))
650648
.chain(iter::once(Ok(tag_layout)))
651649
.chain(promoted_layouts)
652650
.collect::<Result<Vec<_>, _>>()?;
@@ -715,7 +713,9 @@ fn generator_layout<'tcx>(
715713
let mut variant = univariant_uninterned(
716714
cx,
717715
ty,
718-
&variant_only_tys.map(|ty| cx.layout_of(ty)).collect::<Result<Vec<_>, _>>()?,
716+
&variant_only_tys
717+
.map(|ty| Ok(cx.layout_of(ty)?.layout))
718+
.collect::<Result<Vec<_>, _>>()?,
719719
&ReprOptions::default(),
720720
StructKind::Prefixed(prefix_size, prefix_align.abi),
721721
)?;

src/librustdoc/html/render/print_item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_middle::ty::layout::LayoutError;
1010
use rustc_middle::ty::{self, Adt, TyCtxt};
1111
use rustc_span::hygiene::MacroKind;
1212
use rustc_span::symbol::{kw, sym, Symbol};
13-
use rustc_target::abi::{LayoutS, Primitive, TagEncoding, VariantIdx, Variants};
13+
use rustc_target::abi::{LayoutS, Primitive, TagEncoding, Variants};
1414
use std::cmp::Ordering;
1515
use std::fmt;
1616
use std::rc::Rc;
@@ -1887,7 +1887,7 @@ fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) {
18871887
}
18881888

18891889
fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
1890-
fn write_size_of_layout(w: &mut Buffer, layout: &LayoutS<VariantIdx>, tag_size: u64) {
1890+
fn write_size_of_layout(w: &mut Buffer, layout: &LayoutS, tag_size: u64) {
18911891
if layout.abi.is_unsized() {
18921892
write!(w, "(unsized)");
18931893
} else {

0 commit comments

Comments
 (0)