Skip to content

Commit 1fabaaf

Browse files
committed
Fix SILFunctionType of borrow accessors returning trivial types
1 parent 65a59a5 commit 1fabaaf

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,9 +1452,11 @@ class DestructureResults {
14521452
ResultConvention convention;
14531453

14541454
if (isBorrowOrMutateAccessor) {
1455-
if ((hasSelfWithAddressType && !substResultTL.isTrivial()) ||
1456-
isFormallyReturnedIndirectly(origType, substType,
1457-
substResultTLForConvention)) {
1455+
if (substResultTL.isTrivial()) {
1456+
convention = ResultConvention::Unowned;
1457+
} else if (hasSelfWithAddressType ||
1458+
isFormallyReturnedIndirectly(origType, substType,
1459+
substResultTLForConvention)) {
14581460
assert(Convs.getResult(substResultTLForConvention) ==
14591461
ResultConvention::Guaranteed);
14601462
convention = ResultConvention::GuaranteedAddress;

test/SILGen/borrow_accessor.swift

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
// REQUIRES: swift_feature_BorrowAndMutateAccessors
44

5-
public class Klass {}
5+
public class Klass {
6+
var id: Int = 0
7+
}
68

79
func getKlass() -> Klass {
810
return Klass()
@@ -90,6 +92,18 @@ public struct Wrapper {
9092
}
9193
}
9294

95+
var nested_get3: Int {
96+
borrow {
97+
return _s.getKlass.id
98+
}
99+
}
100+
101+
var nested_get4: Int {
102+
borrow {
103+
return s_get.borrowKlass.id
104+
}
105+
}
106+
93107
var nested_read1: Klass {
94108
borrow {
95109
return _s.readKlass // expected-error{{invalid return value from borrow accessor}} // expected-note{{borrow accessors can return either literals, stored properties or computed properties that have borrow accessors}}
@@ -461,6 +475,41 @@ func nctest() {
461475
// CHECK: return [[REG4]]
462476
// CHECK: }
463477

478+
// CHECK: sil hidden [ossa] @$s15borrow_accessor7WrapperV11nested_get3Sivb : $@convention(method) (@guaranteed Wrapper) -> Int {
479+
// CHECK: bb0([[REG0:%.*]] : @guaranteed $Wrapper):
480+
// CHECK: [[REG2:%.*]] = struct_extract [[REG0]], #Wrapper._s
481+
// CHECK: [[REG3:%.*]] = copy_value [[REG2]]
482+
// CHECK: [[REG4:%.*]] = begin_borrow [[REG3]]
483+
// CHECK: [[REG5:%.*]] = function_ref @$s15borrow_accessor1SV8getKlassAA0D0Cvg : $@convention(method) (@guaranteed S) -> @owned Klass
484+
// CHECK: [[REG6:%.*]] = apply [[REG5]]([[REG4]]) : $@convention(method) (@guaranteed S) -> @owned Klass
485+
// CHECK: end_borrow [[REG4]]
486+
// CHECK: destroy_value [[REG3]]
487+
// CHECK: [[REG9:%.*]] = begin_borrow [[REG6]]
488+
// CHECK: [[REG10:%.*]] = class_method [[REG9]], #Klass.id!getter : (Klass) -> () -> Int, $@convention(method) (@guaranteed Klass) -> Int
489+
// CHECK: [[REG11:%.*]] = apply [[REG10]]([[REG9]]) : $@convention(method) (@guaranteed Klass) -> Int
490+
// CHECK: end_borrow [[REG9]]
491+
// CHECK: destroy_value [[REG6]]
492+
// CHECK: return [[REG11]]
493+
// CHECK: }
494+
495+
// CHECK: sil hidden [ossa] @$s15borrow_accessor7WrapperV11nested_get4Sivb : $@convention(method) (@guaranteed Wrapper) -> Int {
496+
// CHECK: bb0([[REG0:%.*]] : @guaranteed $Wrapper):
497+
// CHECK: [[REG2:%.*]] = function_ref @$s15borrow_accessor7WrapperV5s_getAA1SVvg : $@convention(method) (@guaranteed Wrapper) -> @owned S
498+
// CHECK: [[REG3:%.*]] = apply [[REG2]]([[REG0]]) : $@convention(method) (@guaranteed Wrapper) -> @owned S
499+
// CHECK: [[REG4:%.*]] = begin_borrow [[REG3]]
500+
// CHECK: [[REG5:%.*]] = function_ref @$s15borrow_accessor1SV0A5KlassAA0C0Cvb : $@convention(method) (@guaranteed S) -> @guaranteed Klass
501+
// CHECK: [[REG6:%.*]] = apply [[REG5]]([[REG4]]) : $@convention(method) (@guaranteed S) -> @guaranteed Klass
502+
// CHECK: [[REG7:%.*]] = copy_value [[REG6]]
503+
// CHECK: end_borrow [[REG4]]
504+
// CHECK: destroy_value [[REG3]]
505+
// CHECK: [[REG10:%.*]] = begin_borrow [[REG7]]
506+
// CHECK: [[REG11:%.*]] = class_method [[REG10]], #Klass.id!getter : (Klass) -> () -> Int, $@convention(method) (@guaranteed Klass) -> Int
507+
// CHECK: [[REG12:%.*]] = apply [[REG11]]([[REG10]]) : $@convention(method) (@guaranteed Klass) -> Int
508+
// CHECK: end_borrow [[REG10]]
509+
// CHECK: destroy_value [[REG7]]
510+
// CHECK: return [[REG12]]
511+
// CHECK: }
512+
464513
// CHECK: sil hidden [ossa] @$s15borrow_accessor7WrapperV6nestedAA5KlassCvb : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass {
465514
// CHECK: bb0([[REG0:%.*]] : @guaranteed $Wrapper):
466515
// CHECK: [[REG2:%.*]] = function_ref @$s15borrow_accessor7WrapperV1kAA5KlassCvb : $@convention(method) (@guaranteed Wrapper) -> @guaranteed Klass

0 commit comments

Comments
 (0)