Normalize when computing offset_of for slice tail

This commit is contained in:
Michael Goulet 2024-08-05 15:53:37 -04:00
parent 85b5e42d5e
commit d9dd5509dc
2 changed files with 48 additions and 2 deletions

View file

@ -404,8 +404,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
code: traits::ObligationCauseCode<'tcx>,
) {
if !ty.references_error() {
let tail =
self.tcx.struct_tail_with_normalize(ty, |ty| self.normalize(span, ty), || {});
let tail = self.tcx.struct_tail_with_normalize(
ty,
|ty| {
if self.next_trait_solver() {
self.try_structurally_resolve_type(span, ty)
} else {
self.normalize(span, ty)
}
},
|| {},
);
// Sized types have static alignment, and so do slices.
if tail.is_trivially_sized(self.tcx) || matches!(tail.kind(), ty::Slice(..)) {
// Nothing else is required here.

View file

@ -0,0 +1,37 @@
//@ revisions: current next
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[next] compile-flags: -Znext-solver
//@ run-pass
#![feature(offset_of_slice)]
use std::mem::offset_of;
trait Mirror {
type Assoc: ?Sized;
}
impl<T: ?Sized> Mirror for T {
type Assoc = T;
}
#[repr(C)]
struct S {
a: u8,
b: (u8, u8),
c: <[i32] as Mirror>::Assoc,
}
#[repr(C)]
struct T {
x: i8,
y: S,
}
type Tup = (i16, <[i32] as Mirror>::Assoc);
fn main() {
assert_eq!(offset_of!(S, c), 4);
assert_eq!(offset_of!(T, y), 4);
assert_eq!(offset_of!(T, y.c), 8);
assert_eq!(offset_of!(Tup, 1), 4);
}