Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 769c80d

Browse files
committed
Auto merge of rust-lang#15173 - HKalbasi:mir, r=HKalbasi
Fix data layout of reference to nested unsized structs
2 parents b9c3d09 + 302bb3c commit 769c80d

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

crates/hir-ty/src/consteval/tests.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,37 @@ fn type_error() {
24902490
);
24912491
}
24922492

2493+
#[test]
2494+
fn unsized_field() {
2495+
check_number(
2496+
r#"
2497+
//- minicore: coerce_unsized, index, slice, transmute
2498+
use core::mem::transmute;
2499+
2500+
struct Slice([u8]);
2501+
struct Slice2(Slice);
2502+
2503+
impl Slice2 {
2504+
fn as_inner(&self) -> &Slice {
2505+
&self.0
2506+
}
2507+
2508+
fn as_bytes(&self) -> &[u8] {
2509+
&self.as_inner().0
2510+
}
2511+
}
2512+
2513+
const GOAL: u8 = unsafe {
2514+
let x: &[u8] = &[1, 2, 3];
2515+
let x: &Slice2 = transmute(x);
2516+
let x = x.as_bytes();
2517+
x[0] + x[1] + x[2]
2518+
};
2519+
"#,
2520+
6,
2521+
);
2522+
}
2523+
24932524
#[test]
24942525
fn unsized_local() {
24952526
check_fail(

crates/hir-ty/src/layout.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,10 @@ fn struct_tail_erasing_lifetimes(db: &dyn HirDatabase, pointee: Ty) -> Ty {
315315
let data = db.struct_data(*i);
316316
let mut it = data.variant_data.fields().iter().rev();
317317
match it.next() {
318-
Some((f, _)) => field_ty(db, (*i).into(), f, subst),
318+
Some((f, _)) => {
319+
let last_field_ty = field_ty(db, (*i).into(), f, subst);
320+
struct_tail_erasing_lifetimes(db, last_field_ty)
321+
}
319322
None => pointee,
320323
}
321324
}

crates/hir-ty/src/layout/tests.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,24 @@ fn return_position_impl_trait() {
343343
}
344344
}
345345

346+
#[test]
347+
fn unsized_ref() {
348+
size_and_align! {
349+
struct S1([u8]);
350+
struct S2(S1);
351+
struct S3(i32, str);
352+
struct S4(u64, S3);
353+
#[allow(dead_code)]
354+
struct S5 {
355+
field1: u8,
356+
field2: i16,
357+
field_last: S4,
358+
}
359+
360+
struct Goal(&'static S1, &'static S2, &'static S3, &'static S4, &'static S5);
361+
}
362+
}
363+
346364
#[test]
347365
fn enums() {
348366
size_and_align! {

0 commit comments

Comments
 (0)