Skip to content

Commit d419826

Browse files
Merge pull request #67665 from nate-chandler/opaque-values/20230801/4/keypath-setter-takes-new-value-direct
[OpaqueValues] Skip temporary when setting the value at a key-path.
2 parents b14c34a + 5352263 commit d419826

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,7 @@ namespace {
23032303

23042304
auto setValue =
23052305
std::move(value).getAsSingleValue(SGF, origType, loweredTy);
2306-
if (!setValue.getType().isAddress()) {
2306+
if (SGF.useLoweredAddresses() && !setValue.getType().isAddress()) {
23072307
setValue = setValue.materialize(SGF, loc);
23082308
}
23092309

test/SILGen/opaque_values_silgen.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,18 @@ func giveKeyPathString() {
647647
@backDeployed(before: SwiftStdlib 5.8)
648648
public func backDeployingReturningGeneric<T>(_ t: T) throws -> T { t }
649649
#endif
650+
651+
// CHECK-LABEL: sil {{.*}}[ossa] @SetIntoContainerAtKeyPath : {{.*}} {
652+
// CHECK: bb0([[CONTAINER_ADDR:%[^,]+]] : {{.*}}, [[KP:%[^,]+]] : {{.*}}, [[VALUE:%[^,]+]] :
653+
// CHECK: [[KP_COPY:%[^,]+]] = copy_value [[KP]] : $WritableKeyPath<Container, Field>
654+
// CHECK: [[VALUE_COPY:%[^,]+]] = copy_value [[VALUE]] : $Field
655+
// CHECK: [[CONTAINER_ACCESS:%[^,]+]] = begin_access [modify] [unknown] [[CONTAINER_ADDR]] : $*Container
656+
// CHECK: [[SETTER:%[^,]+]] = function_ref @swift_setAtWritableKeyPath
657+
// CHECK: apply [[SETTER]]<Container, Field>([[CONTAINER_ACCESS]], [[KP_COPY]], [[VALUE_COPY]])
658+
// CHECK: end_access [[CONTAINER_ACCESS]] : $*Container
659+
// CHECK: destroy_value [[KP_COPY]]
660+
// CHECK-LABEL: } // end sil function 'SetIntoContainerAtKeyPath'
661+
@_silgen_name("SetIntoContainerAtKeyPath")
662+
func set<Container, Field>(into container: inout Container, at keyPath: WritableKeyPath<Container, Field>, _ value: Field) {
663+
container[keyPath: keyPath] = value
664+
}

0 commit comments

Comments
 (0)