Skip to content

Commit 0ae2c07

Browse files
committed
[OpaqueValues] Keypath setters observe triviality.
When constructing r-values during keypath setter emission, consider whether the argument's type is trivial and construct the relevant r-value based on that. This worked before without opaque values because the value is always an address.
1 parent aba12b3 commit 0ae2c07

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,9 +3261,11 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
32613261
auto subscriptIndices =
32623262
loadIndexValuesForKeyPathComponent(subSGF, loc, property,
32633263
indexes, indexPtrArg);
3264-
3265-
auto valueOrig = ManagedValue::forBorrowedRValue(valueArg)
3266-
.copy(subSGF, loc);
3264+
3265+
auto valueOrig = valueArgTy.isTrivial(subSGF.F)
3266+
? ManagedValue::forTrivialRValue(valueArg)
3267+
: ManagedValue::forBorrowedRValue(valueArg);
3268+
valueOrig = valueOrig.copy(subSGF, loc);
32673269
auto valueSubst = subSGF.emitOrigToSubstValue(loc, valueOrig,
32683270
AbstractionPattern::getOpaque(),
32693271
propertyType);

test/SILGen/opaque_values_silgen.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,19 @@ public func backDeployingReturningGeneric<T>(_ t: T) throws -> T { t }
662662
func set<Container, Field>(into container: inout Container, at keyPath: WritableKeyPath<Container, Field>, _ value: Field) {
663663
container[keyPath: keyPath] = value
664664
}
665+
666+
// CHECK-LABEL: sil {{.*}}[ossa] @$s20opaque_values_silgen16FormClassKeyPathyyF1QL_C1qSivpADTk : {{.*}} {
667+
// CHECK: bb0([[VALUE:%[^,]+]] :
668+
// CHECK-SAME: [[CONTAINER:%[^,]+]] :
669+
// CHECK: [[CONTAINER_COPY:%[^,]+]] = copy_value [[CONTAINER]]
670+
// CHECK: [[SETTER:%[^,]+]] = class_method [[CONTAINER_COPY]]
671+
// CHECK: [[REGISTER_4:%[^,]+]] = apply [[SETTER]]([[VALUE]], [[CONTAINER_COPY]])
672+
// CHECK: destroy_value [[CONTAINER_COPY]]
673+
// CHECK-LABEL: } // end sil function '$s20opaque_values_silgen16FormClassKeyPathyyF1QL_C1qSivpADTk'
674+
@_silgen_name("FormClassKeyPath")
675+
func FormClassKeyPath() {
676+
class Q {
677+
var q: Int = 0
678+
}
679+
_ = \Q.q
680+
}

0 commit comments

Comments
 (0)