Skip to content

Commit 6bfabbe

Browse files
authored
Rollup merge of rust-lang#107163 - mikebenfield:parameters-pr, r=TaKO8Ki
Remove some superfluous type parameters from layout.rs. Specifically remove V, which can always be VariantIdx, and F, which can always be Layout.
2 parents 6f88493 + 8df27d0 commit 6bfabbe

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};
@@ -1250,9 +1251,9 @@ impl Abi {
12501251

12511252
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
12521253
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
1253-
pub enum Variants<V: Idx> {
1254+
pub enum Variants {
12541255
/// Single enum variants, structs/tuples, unions, and all non-ADTs.
1255-
Single { index: V },
1256+
Single { index: VariantIdx },
12561257

12571258
/// Enum-likes with more than one inhabited variant: each variant comes with
12581259
/// a *discriminant* (usually the same as the variant index but the user can
@@ -1262,15 +1263,15 @@ pub enum Variants<V: Idx> {
12621263
/// For enums, the tag is the sole field of the layout.
12631264
Multiple {
12641265
tag: Scalar,
1265-
tag_encoding: TagEncoding<V>,
1266+
tag_encoding: TagEncoding,
12661267
tag_field: usize,
1267-
variants: IndexVec<V, LayoutS<V>>,
1268+
variants: IndexVec<VariantIdx, LayoutS>,
12681269
},
12691270
}
12701271

12711272
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
12721273
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
1273-
pub enum TagEncoding<V: Idx> {
1274+
pub enum TagEncoding {
12741275
/// The tag directly stores the discriminant, but possibly with a smaller layout
12751276
/// (so converting the tag to the discriminant can require sign extension).
12761277
Direct,
@@ -1285,7 +1286,11 @@ pub enum TagEncoding<V: Idx> {
12851286
/// For example, `Option<(usize, &T)>` is represented such that
12861287
/// `None` has a null pointer for the second tuple field, and
12871288
/// `Some` is the identity function (with a non-null reference).
1288-
Niche { untagged_variant: V, niche_variants: RangeInclusive<V>, niche_start: u128 },
1289+
Niche {
1290+
untagged_variant: VariantIdx,
1291+
niche_variants: RangeInclusive<VariantIdx>,
1292+
niche_start: u128,
1293+
},
12891294
}
12901295

12911296
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
@@ -1372,9 +1377,14 @@ impl Niche {
13721377
}
13731378
}
13741379

1380+
rustc_index::newtype_index! {
1381+
#[derive(HashStable_Generic)]
1382+
pub struct VariantIdx {}
1383+
}
1384+
13751385
#[derive(PartialEq, Eq, Hash, Clone)]
13761386
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
1377-
pub struct LayoutS<V: Idx> {
1387+
pub struct LayoutS {
13781388
/// Says where the fields are located within the layout.
13791389
pub fields: FieldsShape,
13801390

@@ -1385,7 +1395,7 @@ pub struct LayoutS<V: Idx> {
13851395
///
13861396
/// To access all fields of this layout, both `fields` and the fields of the active variant
13871397
/// must be taken into account.
1388-
pub variants: Variants<V>,
1398+
pub variants: Variants,
13891399

13901400
/// The `abi` defines how this data is passed between functions, and it defines
13911401
/// value restrictions via `valid_range`.
@@ -1404,13 +1414,13 @@ pub struct LayoutS<V: Idx> {
14041414
pub size: Size,
14051415
}
14061416

1407-
impl<V: Idx> LayoutS<V> {
1417+
impl LayoutS {
14081418
pub fn scalar<C: HasDataLayout>(cx: &C, scalar: Scalar) -> Self {
14091419
let largest_niche = Niche::from_scalar(cx, Size::ZERO, scalar);
14101420
let size = scalar.size(cx);
14111421
let align = scalar.align(cx);
14121422
LayoutS {
1413-
variants: Variants::Single { index: V::new(0) },
1423+
variants: Variants::Single { index: VariantIdx::new(0) },
14141424
fields: FieldsShape::Primitive,
14151425
abi: Abi::Scalar(scalar),
14161426
largest_niche,
@@ -1420,7 +1430,7 @@ impl<V: Idx> LayoutS<V> {
14201430
}
14211431
}
14221432

1423-
impl<V: Idx> fmt::Debug for LayoutS<V> {
1433+
impl fmt::Debug for LayoutS {
14241434
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14251435
// This is how `Layout` used to print before it become
14261436
// `Interned<LayoutS>`. We print it like this to avoid having to update
@@ -1437,6 +1447,43 @@ impl<V: Idx> fmt::Debug for LayoutS<V> {
14371447
}
14381448
}
14391449

1450+
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
1451+
#[rustc_pass_by_value]
1452+
pub struct Layout<'a>(pub Interned<'a, LayoutS>);
1453+
1454+
impl<'a> fmt::Debug for Layout<'a> {
1455+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1456+
// See comment on `<LayoutS as Debug>::fmt` above.
1457+
self.0.0.fmt(f)
1458+
}
1459+
}
1460+
1461+
impl<'a> Layout<'a> {
1462+
pub fn fields(self) -> &'a FieldsShape {
1463+
&self.0.0.fields
1464+
}
1465+
1466+
pub fn variants(self) -> &'a Variants {
1467+
&self.0.0.variants
1468+
}
1469+
1470+
pub fn abi(self) -> Abi {
1471+
self.0.0.abi
1472+
}
1473+
1474+
pub fn largest_niche(self) -> Option<Niche> {
1475+
self.0.0.largest_niche
1476+
}
1477+
1478+
pub fn align(self) -> AbiAndPrefAlign {
1479+
self.0.0.align
1480+
}
1481+
1482+
pub fn size(self) -> Size {
1483+
self.0.0.size
1484+
}
1485+
}
1486+
14401487
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
14411488
pub enum PointerKind {
14421489
/// Shared reference. `frozen` indicates the absence of any `UnsafeCell`.
@@ -1464,7 +1511,7 @@ pub enum InitKind {
14641511
UninitMitigated0x01Fill,
14651512
}
14661513

1467-
impl<V: Idx> LayoutS<V> {
1514+
impl LayoutS {
14681515
/// Returns `true` if the layout corresponds to an unsized type.
14691516
pub fn is_unsized(&self) -> bool {
14701517
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
@@ -149,7 +149,7 @@ pub struct CtxtInterners<'tcx> {
149149
const_: InternedSet<'tcx, ConstData<'tcx>>,
150150
const_allocation: InternedSet<'tcx, Allocation>,
151151
bound_variable_kinds: InternedSet<'tcx, List<ty::BoundVariableKind>>,
152-
layout: InternedSet<'tcx, LayoutS<VariantIdx>>,
152+
layout: InternedSet<'tcx, LayoutS>,
153153
adt_def: InternedSet<'tcx, AdtDefData>,
154154
external_constraints: InternedSet<'tcx, ExternalConstraintsData<'tcx>>,
155155
}
@@ -1520,7 +1520,7 @@ direct_interners! {
15201520
region: mk_region(RegionKind<'tcx>): Region -> Region<'tcx>,
15211521
const_: mk_const_internal(ConstData<'tcx>): Const -> Const<'tcx>,
15221522
const_allocation: intern_const_alloc(Allocation): ConstAllocation -> ConstAllocation<'tcx>,
1523-
layout: intern_layout(LayoutS<VariantIdx>): Layout -> Layout<'tcx>,
1523+
layout: intern_layout(LayoutS): Layout -> Layout<'tcx>,
15241524
adt_def: intern_adt_def(AdtDefData): AdtDef -> AdtDef<'tcx>,
15251525
external_constraints: intern_external_constraints(ExternalConstraintsData<'tcx>): ExternalConstraints -> ExternalConstraints<'tcx>,
15261526
}

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].ty))
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;
@@ -1833,7 +1833,7 @@ fn document_non_exhaustive(w: &mut Buffer, item: &clean::Item) {
18331833
}
18341834

18351835
fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
1836-
fn write_size_of_layout(w: &mut Buffer, layout: &LayoutS<VariantIdx>, tag_size: u64) {
1836+
fn write_size_of_layout(w: &mut Buffer, layout: &LayoutS, tag_size: u64) {
18371837
if layout.abi.is_unsized() {
18381838
write!(w, "(unsized)");
18391839
} else {

0 commit comments

Comments
 (0)