From 88192ffb7072e8b86c392ef41e4e8958e98f1f46 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 20 Feb 2024 16:40:36 -0800 Subject: [PATCH] SILGen: Unwrap `@moveOnly` wrapped values before yielding them. --- lib/SILGen/SILGenApply.cpp | 14 +++++++++++-- test/SILGen/yield_borrowing.swift | 20 +++++++++++++++++++ .../moveonly_borrowing_switch.swift | 10 ---------- 3 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 test/SILGen/yield_borrowing.swift diff --git a/lib/SILGen/SILGenApply.cpp b/lib/SILGen/SILGenApply.cpp index 6c099141765..22e31224ea1 100644 --- a/lib/SILGen/SILGenApply.cpp +++ b/lib/SILGen/SILGenApply.cpp @@ -6011,8 +6011,18 @@ void SILGenFunction::emitRawYield(SILLocation loc, JumpDest unwindDest, bool isUniqueYield) { SmallVector yieldValues; - for (auto arg : yieldArgs) - yieldValues.push_back(arg.getValue()); + for (auto arg : yieldArgs) { + auto value = arg.getValue(); + if (value->getType().isMoveOnlyWrapped()) { + if (value->getType().isAddress()) { + value = B.createMoveOnlyWrapperToCopyableAddr(loc, value); + } else { + value = B.createGuaranteedMoveOnlyWrapperToCopyableValue(loc, value); + } + } + + yieldValues.push_back(value); + } // The normal continuation block. auto resumeBB = createBasicBlock(); diff --git a/test/SILGen/yield_borrowing.swift b/test/SILGen/yield_borrowing.swift new file mode 100644 index 00000000000..7d845d86c27 --- /dev/null +++ b/test/SILGen/yield_borrowing.swift @@ -0,0 +1,20 @@ +// RUN: %target-swift-emit-silgen -verify %s +struct Foo { + var x: String + + var y: Foo { + borrowing _read { + yield self + } + } +} + +struct BigFoo { + var x: Any + + var y: BigFoo { + borrowing _read { + yield self + } + } +} diff --git a/test/SILOptimizer/moveonly_borrowing_switch.swift b/test/SILOptimizer/moveonly_borrowing_switch.swift index b7a25d4144d..a0710e61f54 100644 --- a/test/SILOptimizer/moveonly_borrowing_switch.swift +++ b/test/SILOptimizer/moveonly_borrowing_switch.swift @@ -299,10 +299,6 @@ extension List { } } -/* - TODO: type mismatch because of `@moveOnly` wrapper. yield needs to peel it - off - var head: Element { _read { switch self { @@ -313,7 +309,6 @@ extension List { } } } -*/ } extension ChonkyList { @@ -335,10 +330,6 @@ extension ChonkyList { } } -/* - TODO: type mismatch because of `@moveOnly` wrapper. yield needs to peel it - off - var head: Element { _read { switch self { @@ -349,6 +340,5 @@ extension ChonkyList { } } } -*/ }