diff --git a/lib/SILGen/SILGenLValue.cpp b/lib/SILGen/SILGenLValue.cpp index 7f95ca62a1f9..9f3992f5aa7b 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 000000000000..ed53f75deb3c --- /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) +}