Avoid unnecessary insertion of mark_unresolved_noncopyable_value

SILGen inserts mark_unresolved_noncopyable_value at the introducer in some cases and at uses in other cases. This inconsistency can causes insertion of a redundant mark* instruction which crashes the move-only checker.

This change avoids inserting a redundant mark* instruction.
This commit is contained in:
Meghana Gupta
2025-10-20 13:04:20 -07:00
parent 9fed0413f0
commit 7c25466b21
2 changed files with 33 additions and 4 deletions

View File

@@ -807,10 +807,16 @@ void SILGenFunction::emitCaptures(SILLocation loc,
// If we have a mutable binding for a 'let', such as 'self' in an
// 'init' method, load it.
if (val->getType().isMoveOnly()) {
val = B.createMarkUnresolvedNonCopyableValueInst(
loc, val,
MarkUnresolvedNonCopyableValueInst::CheckKind::
NoConsumeOrAssign);
auto *moveOnlyIntroducer =
dyn_cast_or_null<MarkUnresolvedNonCopyableValueInst>(val);
if (!moveOnlyIntroducer || moveOnlyIntroducer->getCheckKind() !=
MarkUnresolvedNonCopyableValueInst::
CheckKind::NoConsumeOrAssign) {
val = B.createMarkUnresolvedNonCopyableValueInst(
loc, val,
MarkUnresolvedNonCopyableValueInst::CheckKind::
NoConsumeOrAssign);
}
}
val = emitLoad(loc, val, tl, SGFContext(), IsNotTake).forward(*this);
}

View File

@@ -0,0 +1,23 @@
// RUN: %target-swift-frontend -emit-sil -verify -enable-experimental-feature AddressableParameters %s
// REQUIRES: swift_feature_AddressableParameters
public struct Container<Element: ~Copyable >: ~Copyable {
var _storage: UnsafeMutableBufferPointer<Element>
var _count: Int
public subscript(index: Int) -> Element {
@_addressableSelf
_read {
precondition(index >= 0 && index < _count, "Index out of bounds")
yield _storage.baseAddress.unsafelyUnwrapped.advanced(by: index).pointee
}
_modify {
precondition(index >= 0 && index < _count, "Index out of bounds")
yield &_storage.baseAddress.unsafelyUnwrapped.advanced(by: index).pointee
}
}
}
extension Container: Copyable where Element: Copyable {}