Files
swift-mirror/test/IRGen/optimized_offset_of_empty.swift
Erik Eckstein c11408ea2f IRGen: Don't let the offset of an empty field be undef.
If the field address comes from a struct_element_addr which is a result of an optimized `MemoryLayout<S>.offset(of: \.field)` we cannot return undef.
We have to be consistent with `offset(of:)`, which returns 0.
Therefore we need to return the base address of the struct.

rdar://117265274
2024-03-11 15:21:03 +01:00

46 lines
715 B
Swift

// RUN: %empty-directory(%t)
// RUN: %target-build-swift -module-name=test %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s
// RUN: %target-build-swift -module-name=test -O %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s
// REQUIRES: executable_test
enum E {
case A
}
struct X {}
struct S {
let i: Int
let e: E
let x: X
}
var gg: Int? = 27
@inline(never)
func getOffsetE() {
gg = MemoryLayout<S>.offset(of: \.e)
}
@inline(never)
func getOffsetX() {
gg = MemoryLayout<S>.offset(of: \.x)
}
getOffsetE()
// CHECK: Optional(0)
print(gg as Any)
gg = 27
getOffsetX()
// CHECK: Optional(0)
print(gg as Any)