Skip to content

[5.9][IRGen] Respect optionality of unowned(unsafe) reference properties #65701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/IRGen/GenExistential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ namespace {
class Name##ClassExistentialTypeInfo final \
: public ScalarExistentialTypeInfoBase<Name##ClassExistentialTypeInfo, \
LoadableTypeInfo> { \
bool IsOptional; \
public: \
Name##ClassExistentialTypeInfo( \
ArrayRef<const ProtocolDecl *> storedProtocols, \
Expand All @@ -807,7 +808,8 @@ namespace {
bool isOptional) \
: ScalarExistentialTypeInfoBase(storedProtocols, ty, size, \
spareBits, align, IsTriviallyDestroyable,\
IsCopyable, IsFixedSize) {} \
IsCopyable, IsFixedSize), \
IsOptional(isOptional) {} \
TypeLayoutEntry \
*buildTypeLayoutEntry(IRGenModule &IGM, \
SILType T, \
Expand All @@ -824,6 +826,25 @@ namespace {
else \
return IGM.getUnknownObjectTypeInfo(); \
} \
unsigned getFixedExtraInhabitantCount(IRGenModule &IGM) const override { \
return getValueTypeInfoForExtraInhabitants(IGM) \
.getFixedExtraInhabitantCount(IGM) - IsOptional; \
} \
APInt getFixedExtraInhabitantValue(IRGenModule &IGM, \
unsigned bits, \
unsigned index) const override { \
/* Note that we pass down the original bit-width. */ \
return getValueTypeInfoForExtraInhabitants(IGM) \
.getFixedExtraInhabitantValue(IGM, bits, \
index + IsOptional); \
} \
llvm::Value *getExtraInhabitantIndex(IRGenFunction &IGF, \
Address src, SILType T, \
bool isOutlined) const override { \
return PointerInfo::forHeapObject(IGF.IGM) \
.withNullable(IsNullable_t(IsOptional)) \
.getExtraInhabitantIndex(IGF, src); \
} \
/* FIXME -- Use REF_STORAGE_HELPER and make */ \
/* getValueTypeInfoForExtraInhabitants call llvm_unreachable() */ \
void emitValueRetain(IRGenFunction &IGF, llvm::Value *value, \
Expand Down
28 changes: 28 additions & 0 deletions test/Interpreter/rdar108705703.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test

struct Context {
unowned(unsafe) var x: AnyObject? = nil
}

@inline(never)
func test(x: Context) -> Context? {
return x
}

// CHECK: works
if (test(x: Context()) == nil) {
print("bug")
} else {
print("works")
}

@inline(never)
func test2() -> Context {
var g: [Context] = [Context()]
print(g.endIndex)
return g.removeLast()
}

// CHECK: Context(x: nil)
print(test2())