Skip to content

Commit d9dd550

Browse files
Normalize when computing offset_of for slice tail
1 parent 85b5e42 commit d9dd550

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
404404
code: traits::ObligationCauseCode<'tcx>,
405405
) {
406406
if !ty.references_error() {
407-
let tail =
408-
self.tcx.struct_tail_with_normalize(ty, |ty| self.normalize(span, ty), || {});
407+
let tail = self.tcx.struct_tail_with_normalize(
408+
ty,
409+
|ty| {
410+
if self.next_trait_solver() {
411+
self.try_structurally_resolve_type(span, ty)
412+
} else {
413+
self.normalize(span, ty)
414+
}
415+
},
416+
|| {},
417+
);
409418
// Sized types have static alignment, and so do slices.
410419
if tail.is_trivially_sized(self.tcx) || matches!(tail.kind(), ty::Slice(..)) {
411420
// Nothing else is required here.

Diff for: tests/ui/offset-of/offset-of-slice-normalized.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//@ revisions: current next
2+
//@ ignore-compare-mode-next-solver (explicit revisions)
3+
//@[next] compile-flags: -Znext-solver
4+
//@ run-pass
5+
6+
#![feature(offset_of_slice)]
7+
8+
use std::mem::offset_of;
9+
10+
trait Mirror {
11+
type Assoc: ?Sized;
12+
}
13+
impl<T: ?Sized> Mirror for T {
14+
type Assoc = T;
15+
}
16+
17+
#[repr(C)]
18+
struct S {
19+
a: u8,
20+
b: (u8, u8),
21+
c: <[i32] as Mirror>::Assoc,
22+
}
23+
24+
#[repr(C)]
25+
struct T {
26+
x: i8,
27+
y: S,
28+
}
29+
30+
type Tup = (i16, <[i32] as Mirror>::Assoc);
31+
32+
fn main() {
33+
assert_eq!(offset_of!(S, c), 4);
34+
assert_eq!(offset_of!(T, y), 4);
35+
assert_eq!(offset_of!(T, y.c), 8);
36+
assert_eq!(offset_of!(Tup, 1), 4);
37+
}

0 commit comments

Comments
 (0)