From 56714a1c127444381657f31e9c5fd9d2f5e5565a Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Thu, 6 Nov 2025 15:12:48 -0800 Subject: [PATCH] Fix emitLoadOfLValue for +0 values produced from a physical component Create a copy of the projected +0 value based on SGFContext. --- lib/SILGen/SILGenLValue.cpp | 6 +++--- test/SILGen/borrow_accessor_unit.swift | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/SILGen/borrow_accessor_unit.swift diff --git a/lib/SILGen/SILGenLValue.cpp b/lib/SILGen/SILGenLValue.cpp index 7f95ca62a1f90..9f3992f5aa7bd 100644 --- a/lib/SILGen/SILGenLValue.cpp +++ b/lib/SILGen/SILGenLValue.cpp @@ -5530,9 +5530,9 @@ RValue SILGenFunction::emitLoadOfLValue(SILLocation loc, LValue &&src, projection = emitLoad(loc, projection.getValue(), origFormalType, substFormalType, rvalueTL, C, IsNotTake, isBaseGuaranteed); - } else if (isReadAccessResultOwned(src.getAccessKind()) && - !projection.isPlusOneOrTrivial(*this)) { - + } else if (!projection.isPlusOneOrTrivial(*this) && + (!C.isImmediatePlusZeroOk() && + !(C.isGuaranteedPlusZeroOk() && isBaseGuaranteed))) { // Before we copy, if we have a move only wrapped value, unwrap the // value using a guaranteed moveonlywrapper_to_copyable. if (projection.getType().isMoveOnlyWrapped()) { diff --git a/test/SILGen/borrow_accessor_unit.swift b/test/SILGen/borrow_accessor_unit.swift new file mode 100644 index 0000000000000..ed53f75deb3c5 --- /dev/null +++ b/test/SILGen/borrow_accessor_unit.swift @@ -0,0 +1,26 @@ +// RUN:%target-swift-frontend -emit-silgen %s -enable-experimental-feature BorrowAndMutateAccessors + +// REQUIRES: swift_feature_BorrowAndMutateAccessors + +public final class Klass {} + +public struct Wrapper { + var _k: Klass + + var k: Klass { + borrow { + return _k + } + mutate { + return &_k + } + } +} + +@inline(never) +func blackhole(_ t: T) { } + +func inoutTest(_ w: inout Wrapper) { + let k = w.k + blackhole(k) +}