Skip to content

Commit 41f0d80

Browse files
authored
Merge pull request #21215 from ChayimFriedman2/unsized-struct
fix: Don't implement sizedness check via `all_field_tys()`
2 parents 40727e8 + 3172d19 commit 41f0d80

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

crates/hir-ty/src/next_solver/interner.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -690,15 +690,10 @@ impl<'db> inherent::AdtDef<DbInterner<'db>> for AdtDef {
690690
interner: DbInterner<'db>,
691691
sizedness: SizedTraitKind,
692692
) -> Option<EarlyBinder<DbInterner<'db>, Ty<'db>>> {
693-
if self.is_struct() {
694-
let tail_ty = self.all_field_tys(interner).skip_binder().into_iter().last()?;
695-
696-
let constraint_ty = sizedness_constraint_for_ty(interner, sizedness, tail_ty)?;
697-
698-
Some(EarlyBinder::bind(constraint_ty))
699-
} else {
700-
None
701-
}
693+
let tail_ty = self.struct_tail_ty(interner)?;
694+
tail_ty
695+
.map_bound(|tail_ty| sizedness_constraint_for_ty(interner, sizedness, tail_ty))
696+
.transpose()
702697
}
703698

704699
fn destructor(

crates/hir-ty/src/next_solver/util.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,10 @@ pub fn sizedness_constraint_for_ty<'db>(
422422
.next_back()
423423
.and_then(|ty| sizedness_constraint_for_ty(interner, sizedness, ty)),
424424

425-
Adt(adt, args) => {
426-
let tail_ty =
427-
EarlyBinder::bind(adt.all_field_tys(interner).skip_binder().into_iter().last()?)
428-
.instantiate(interner, args);
425+
Adt(adt, args) => adt.struct_tail_ty(interner).and_then(|tail_ty| {
426+
let tail_ty = tail_ty.instantiate(interner, args);
429427
sizedness_constraint_for_ty(interner, sizedness, tail_ty)
430-
}
428+
}),
431429

432430
Placeholder(..) | Bound(..) | Infer(..) => {
433431
panic!("unexpected type `{ty:?}` in sizedness_constraint_for_ty")

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,3 +2229,32 @@ fn test(x: *mut u8) {
22292229
"#,
22302230
);
22312231
}
2232+
2233+
#[test]
2234+
fn unsized_struct() {
2235+
check_types(
2236+
r#"
2237+
//- minicore: sized, phantom_data
2238+
use core::marker::PhantomData;
2239+
2240+
const UI_DEV_CREATE: Ioctl = Ioctl(PhantomData);
2241+
2242+
struct Ioctl<T: ?Sized = NoArgs>(PhantomData<T>);
2243+
2244+
struct NoArgs([u8]);
2245+
2246+
impl<T> Ioctl<T> {
2247+
fn ioctl(self) {}
2248+
}
2249+
2250+
impl Ioctl<NoArgs> {
2251+
fn ioctl(self) -> u32 { 0 }
2252+
}
2253+
2254+
fn main() {
2255+
UI_DEV_CREATE.ioctl();
2256+
// ^^^^^^^^^^^^^^^^^^^^^ u32
2257+
}
2258+
"#,
2259+
);
2260+
}

0 commit comments

Comments
 (0)