Skip to content

Commit 4929f38

Browse files
committed
Remove most manual LayoutData creations and move them to rustc_abi
...either as: - methods on LayoutCalculator, for faillible operations; - constructors on LayoutData, for infaillible ones.
1 parent 7769e46 commit 4929f38

File tree

1 file changed

+20
-78
lines changed

1 file changed

+20
-78
lines changed

crates/hir-ty/src/layout.rs

Lines changed: 20 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use hir_def::{
1515
use la_arena::{Idx, RawIdx};
1616
use rustc_abi::AddressSpace;
1717
use rustc_hashes::Hash64;
18-
use rustc_index::{IndexSlice, IndexVec};
18+
use rustc_index::IndexVec;
1919

2020
use triomphe::Arc;
2121

@@ -190,7 +190,8 @@ pub fn layout_of_ty_query(
190190
let dl = &*target;
191191
let cx = LayoutCx::new(dl);
192192
let ty = normalize(db, trait_env.clone(), ty);
193-
let result = match ty.kind(Interner) {
193+
let kind = ty.kind(Interner);
194+
let result = match kind {
194195
TyKind::Adt(AdtId(def), subst) => {
195196
if let hir_def::AdtId::StructId(s) = def {
196197
let data = db.struct_data(*s);
@@ -216,7 +217,7 @@ pub fn layout_of_ty_query(
216217
valid_range: WrappingRange { start: 0, end: 0x10FFFF },
217218
},
218219
),
219-
chalk_ir::Scalar::Int(i) => scalar(
220+
chalk_ir::Scalar::Int(i) => Layout::scalar(dl, scalar_unit(
220221
dl,
221222
Primitive::Int(
222223
match i {
@@ -229,8 +230,8 @@ pub fn layout_of_ty_query(
229230
},
230231
true,
231232
),
232-
),
233-
chalk_ir::Scalar::Uint(i) => scalar(
233+
)),
234+
chalk_ir::Scalar::Uint(i) => Layout::scalar(dl, scalar_unit(
234235
dl,
235236
Primitive::Int(
236237
match i {
@@ -243,16 +244,16 @@ pub fn layout_of_ty_query(
243244
},
244245
false,
245246
),
246-
),
247-
chalk_ir::Scalar::Float(f) => scalar(
247+
)),
248+
chalk_ir::Scalar::Float(f) => Layout::scalar(dl, scalar_unit(
248249
dl,
249250
Primitive::Float(match f {
250251
FloatTy::F16 => Float::F16,
251252
FloatTy::F32 => Float::F32,
252253
FloatTy::F64 => Float::F64,
253254
FloatTy::F128 => Float::F128,
254255
}),
255-
),
256+
)),
256257
},
257258
TyKind::Tuple(len, tys) => {
258259
let kind = if *len == 0 { StructKind::AlwaysSized } else { StructKind::MaybeUnsized };
@@ -268,56 +269,16 @@ pub fn layout_of_ty_query(
268269
TyKind::Array(element, count) => {
269270
let count = try_const_usize(db, count).ok_or(LayoutError::HasErrorConst)? as u64;
270271
let element = db.layout_of_ty(element.clone(), trait_env)?;
271-
let size = element
272-
.size
273-
.checked_mul(count, dl)
274-
.ok_or(LayoutError::BadCalc(LayoutCalculatorError::SizeOverflow))?;
275-
276-
let backend_repr = BackendRepr::Memory { sized: true };
277-
278-
let largest_niche = if count != 0 { element.largest_niche } else { None };
279-
let uninhabited = if count != 0 { element.uninhabited } else { false };
280-
281-
Layout {
282-
variants: Variants::Single { index: struct_variant_idx() },
283-
fields: FieldsShape::Array { stride: element.size, count },
284-
backend_repr,
285-
largest_niche,
286-
uninhabited,
287-
align: element.align,
288-
size,
289-
max_repr_align: None,
290-
unadjusted_abi_align: element.align.abi,
291-
randomization_seed: Hash64::ZERO,
292-
}
272+
cx.calc.array_like::<_, _, ()>(&element, Some(count))?
293273
}
294274
TyKind::Slice(element) => {
295275
let element = db.layout_of_ty(element.clone(), trait_env)?;
296-
Layout {
297-
variants: Variants::Single { index: struct_variant_idx() },
298-
fields: FieldsShape::Array { stride: element.size, count: 0 },
299-
backend_repr: BackendRepr::Memory { sized: false },
300-
largest_niche: None,
301-
uninhabited: false,
302-
align: element.align,
303-
size: Size::ZERO,
304-
max_repr_align: None,
305-
unadjusted_abi_align: element.align.abi,
306-
randomization_seed: Hash64::ZERO,
307-
}
276+
cx.calc.array_like::<_, _, ()>(&element, None)?
277+
}
278+
TyKind::Str => {
279+
let element = scalar_unit(dl, Primitive::Int(Integer::I8, false));
280+
cx.calc.array_like::<_, _, ()>(&Layout::scalar(dl, element), None)?
308281
}
309-
TyKind::Str => Layout {
310-
variants: Variants::Single { index: struct_variant_idx() },
311-
fields: FieldsShape::Array { stride: Size::from_bytes(1), count: 0 },
312-
backend_repr: BackendRepr::Memory { sized: false },
313-
largest_niche: None,
314-
uninhabited: false,
315-
align: dl.i8_align,
316-
size: Size::ZERO,
317-
max_repr_align: None,
318-
unadjusted_abi_align: dl.i8_align.abi,
319-
randomization_seed: Hash64::ZERO,
320-
},
321282
// Potentially-wide pointers.
322283
TyKind::Ref(_, _, pointee) | TyKind::Raw(_, pointee) => {
323284
let mut data_ptr = scalar_unit(dl, Primitive::Pointer(AddressSpace::DATA));
@@ -355,17 +316,12 @@ pub fn layout_of_ty_query(
355316
};
356317

357318
// Effectively a (ptr, meta) tuple.
358-
cx.calc.scalar_pair(data_ptr, metadata)
319+
LayoutData::scalar_pair(dl, data_ptr, metadata)
359320
}
360-
TyKind::FnDef(_, _) => layout_of_unit(&cx)?,
361-
TyKind::Never => cx.calc.layout_of_never_type(),
362-
TyKind::Dyn(_) | TyKind::Foreign(_) => {
363-
let mut unit = layout_of_unit(&cx)?;
364-
match &mut unit.backend_repr {
365-
BackendRepr::Memory { sized } => *sized = false,
366-
_ => return Err(LayoutError::Unknown),
367-
}
368-
unit
321+
TyKind::Never => LayoutData::never_type(dl),
322+
TyKind::FnDef(..) | TyKind::Dyn(_) | TyKind::Foreign(_) => {
323+
let sized = matches!(kind, TyKind::FnDef(..));
324+
LayoutData::unit(dl, sized)
369325
}
370326
TyKind::Function(_) => {
371327
let mut ptr = scalar_unit(dl, Primitive::Pointer(dl.instruction_address_space));
@@ -434,16 +390,6 @@ pub fn layout_of_ty_recover(
434390
Err(LayoutError::RecursiveTypeWithoutIndirection)
435391
}
436392

437-
fn layout_of_unit(cx: &LayoutCx<'_>) -> Result<Layout, LayoutError> {
438-
cx.calc
439-
.univariant::<RustcFieldIdx, RustcEnumVariantIdx, &&Layout>(
440-
IndexSlice::empty(),
441-
&ReprOptions::default(),
442-
StructKind::AlwaysSized,
443-
)
444-
.map_err(Into::into)
445-
}
446-
447393
fn struct_tail_erasing_lifetimes(db: &dyn HirDatabase, pointee: Ty) -> Ty {
448394
match pointee.kind(Interner) {
449395
TyKind::Adt(AdtId(hir_def::AdtId::StructId(i)), subst) => {
@@ -474,9 +420,5 @@ fn scalar_unit(dl: &TargetDataLayout, value: Primitive) -> Scalar {
474420
Scalar::Initialized { value, valid_range: WrappingRange::full(value.size(dl)) }
475421
}
476422

477-
fn scalar(dl: &TargetDataLayout, value: Primitive) -> Layout {
478-
Layout::scalar(dl, scalar_unit(dl, value))
479-
}
480-
481423
#[cfg(test)]
482424
mod tests;

0 commit comments

Comments
 (0)