Skip to content

Fix RawSILInstLowering for InitAccessors #67379

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion lib/SILOptimizer/Mandatory/RawSILInstLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ lowerAssignOrInitInstruction(SILBuilderWithScope &b,

SILValue selfRef;
if (isRefSelf) {
// create a begin_borrow if selfValue is not guaranteed
selfRef = b.emitBeginBorrowOperation(loc, selfValue);
} else {
selfRef = b.createBeginAccess(loc, selfValue, SILAccessKind::Modify,
Expand Down Expand Up @@ -337,7 +338,9 @@ lowerAssignOrInitInstruction(SILBuilderWithScope &b,
b.createApply(loc, initFn, SubstitutionMap(), arguments);

if (isRefSelf) {
b.emitEndBorrowOperation(loc, selfRef);
if (selfRef != selfValue) {
b.emitEndBorrowOperation(loc, selfRef);
}
} else {
b.createEndAccess(loc, selfRef, /*aborted=*/false);
}
Expand Down
29 changes: 29 additions & 0 deletions test/SILOptimizer/init_accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

// REQUIRES: asserts

class NSObject {}

struct TestInit {
var x: Int
var y: Int
Expand Down Expand Up @@ -455,3 +457,30 @@ func test_assignments() {
}
}
}

// rdar://112417250 (Crash with macro expansion on generic NSObject subclass)
// self is already borrowed within the initializer.
//
// CHECK-LABEL: sil private [ossa] @$s14init_accessors8testObjCyyF07GenericD9CSubclassL_CyADyxGxcfc : $@convention(method) <T> (@in T, @owned GenericObjCSubclass<T>) -> @owned GenericObjCSubclass<T> {
// CHECK: [[BORROW:%.*]] = load_borrow %{{.*}} : $*GenericObjCSubclass<T>
// CHECK: ref_element_addr [[BORROW]] : $GenericObjCSubclass<T>, #<abstract function>GenericObjCSubclass._value
// CHECK: apply
// CHECK: end_borrow [[BORROW]] : $GenericObjCSubclass<T>
// CHECK-NOT: end_borrow [[BORROW]] : $GenericObjCSubclass<T>
func testObjC() {
class GenericObjCSubclass<T>: NSObject {
var _value: T

var value: T {
init initializes(_value) {
self._value = newValue
}
get { _value }
set { _value = newValue }
}

init(_ value: T) {
self.value = value
}
}
}