Skip to content

Commit 374026f

Browse files
authored
Merge pull request #82380 from atrick/nonescapable-accessor-on-trivial
Add Feature: NonescapableAccessorOnTrivial
2 parents 17c1fbd + cc357f4 commit 374026f

File tree

6 files changed

+75
-6
lines changed

6 files changed

+75
-6
lines changed

include/swift/AST/Types.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,14 @@ class alignas(1 << TypeAlignInBits) TypeBase
700700
/// Returns true if this contextual type is (Escapable && !isNoEscape).
701701
bool mayEscape() { return !isNoEscape() && isEscapable(); }
702702

703+
/// Returns true if this contextual type satisfies a conformance to
704+
/// BitwiseCopyable.
705+
bool isBitwiseCopyable();
706+
707+
/// Returns true if this type satisfies a conformance to BitwiseCopyable in
708+
/// the given generic signature.
709+
bool isBitwiseCopyable(GenericSignature sig);
710+
703711
/// Are values of this type essentially just class references,
704712
/// possibly with some sort of additional information?
705713
///

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,9 @@ EXPERIMENTAL_FEATURE(DefaultIsolationPerFile, false)
530530
/// Enable @_lifetime attribute
531531
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(Lifetimes, true)
532532

533+
/// Enable UnsafeMutablePointer.mutableSpan
534+
EXPERIMENTAL_FEATURE(NonescapableAccessorOnTrivial, true)
535+
533536
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
534537
#undef EXPERIMENTAL_FEATURE
535538
#undef UPCOMING_FEATURE

lib/AST/ConformanceLookup.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,3 +963,21 @@ bool TypeBase::isEscapable(GenericSignature sig) {
963963
}
964964
return contextTy->isEscapable();
965965
}
966+
967+
bool TypeBase::isBitwiseCopyable() {
968+
auto &ctx = getASTContext();
969+
auto *bitwiseCopyableProtocol =
970+
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
971+
if (!bitwiseCopyableProtocol) {
972+
return false;
973+
}
974+
return (bool)checkConformance(this, bitwiseCopyableProtocol);
975+
}
976+
977+
bool TypeBase::isBitwiseCopyable(GenericSignature sig) {
978+
Type contextTy = this;
979+
if (sig) {
980+
contextTy = sig.getGenericEnvironment()->mapTypeIntoContext(contextTy);
981+
}
982+
return contextTy->isBitwiseCopyable();
983+
}

lib/AST/FeatureSet.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,25 @@ static bool usesFeatureLifetimeDependenceMutableAccessors(Decl *decl) {
331331
return false;
332332
}
333333
auto var = cast<VarDecl>(decl);
334-
if (!var->isGetterMutating()) {
334+
return var->isGetterMutating() && !var->getTypeInContext()->isEscapable();
335+
}
336+
337+
static bool usesFeatureNonescapableAccessorOnTrivial(Decl *decl) {
338+
if (!isa<VarDecl>(decl)) {
335339
return false;
336340
}
337-
if (auto dc = var->getDeclContext()) {
338-
if (auto nominal = dc->getSelfNominalTypeDecl()) {
339-
auto sig = nominal->getGenericSignature();
340-
return !var->getInterfaceType()->isEscapable(sig);
341-
}
341+
auto var = cast<VarDecl>(decl);
342+
if (!var->hasParsedAccessors()) {
343+
return false;
344+
}
345+
// Check for properties that are both non-Copyable and non-Escapable
346+
// (MutableSpan).
347+
if (var->getTypeInContext()->isNoncopyable()
348+
&& !var->getTypeInContext()->isEscapable()) {
349+
auto selfTy = var->getDeclContext()->getSelfTypeInContext();
350+
// Consider 'self' trivial if it is BitwiseCopyable and Escapable
351+
// (UnsafeMutableBufferPointer).
352+
return selfTy->isBitwiseCopyable() && selfTy->isEscapable();
342353
}
343354
return false;
344355
}

test/ModuleInterface/Inputs/lifetime_dependence.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,21 @@ extension Container {
7777
}
7878
}
7979
}
80+
81+
// Test feature guard: NonescapableAccessorOnTrivial
82+
extension UnsafeMutableBufferPointer where Element: ~Copyable {
83+
public var span: Span<Element> {
84+
@lifetime(borrow self)
85+
@_alwaysEmitIntoClient
86+
get {
87+
unsafe Span(_unsafeElements: self)
88+
}
89+
}
90+
public var mutableSpan: MutableSpan<Element> {
91+
@lifetime(borrow self)
92+
@_alwaysEmitIntoClient
93+
get {
94+
unsafe MutableSpan(_unsafeElements: self)
95+
}
96+
}
97+
}

test/ModuleInterface/lifetime_dependence_test.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -emit-module \
44
// RUN: -enable-experimental-feature LifetimeDependence \
5+
// RUN: -suppress-warnings \
56
// RUN: -o %t/lifetime_dependence.swiftmodule \
67
// RUN: -emit-module-interface-path %t/lifetime_dependence.swiftinterface \
78
// RUN: %S/Inputs/lifetime_dependence.swift
@@ -41,3 +42,13 @@ import lifetime_dependence
4142
// CHECK: extension lifetime_dependence.Container {
4243
// CHECK-NEXT: #if compiler(>=5.3) && $NonescapableTypes && $LifetimeDependence
4344
// CHECK-NEXT: public var storage: lifetime_dependence.BufferView {
45+
46+
// CHECK-LABEL: extension Swift.UnsafeMutableBufferPointer where Element : ~Copyable {
47+
// CHECK: #if compiler(>=5.3) && $LifetimeDependence
48+
// CHECK: public var span: Swift.Span<Element> {
49+
// CHECK: @lifetime(borrow self)
50+
// CHECK: @_alwaysEmitIntoClient get {
51+
// CHECK: #if compiler(>=5.3) && $LifetimeDependence && $NonescapableAccessorOnTrivial
52+
// CHECK: public var mutableSpan: Swift.MutableSpan<Element> {
53+
// CHECK: @lifetime(borrow self)
54+
// CHECK: @_alwaysEmitIntoClient get {

0 commit comments

Comments
 (0)