Skip to content

Commit 1c437aa

Browse files
authored
Merge pull request #85991 from meg-gupta/lifetimeflagscp
[6.3] Fix swiftinterface printing of accessors and inferred lifetime dependencies when Lifetimes feature is enabled
2 parents 6e5ba7a + 81feb76 commit 1c437aa

File tree

3 files changed

+64
-15
lines changed

3 files changed

+64
-15
lines changed

lib/AST/FeatureSet.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,36 +147,42 @@ UNINTERESTING_FEATURE(SendingArgsAndResults)
147147
UNINTERESTING_FEATURE(CheckImplementationOnly)
148148
UNINTERESTING_FEATURE(CheckImplementationOnlyStrict)
149149

150-
static bool findUnderscoredLifetimeAttr(Decl *decl) {
151-
auto hasUnderscoredLifetimeAttr = [](Decl *decl) {
150+
static bool findLifetimeAttr(Decl *decl, bool findUnderscored) {
151+
auto hasLifetimeAttr = [&](Decl *decl) {
152152
if (!decl->getAttrs().hasAttribute<LifetimeAttr>()) {
153153
return false;
154154
}
155155
// Since we ban mixing @lifetime and @_lifetime on the same decl, checking
156156
// any one LifetimeAttr on the decl is sufficient.
157157
// FIXME: Implement the ban.
158-
return decl->getAttrs().getAttribute<LifetimeAttr>()->isUnderscored();
158+
if (findUnderscored) {
159+
return decl->getAttrs().getAttribute<LifetimeAttr>()->isUnderscored();
160+
}
161+
return !decl->getAttrs().getAttribute<LifetimeAttr>()->isUnderscored();
159162
};
160163

161164
switch (decl->getKind()) {
162165
case DeclKind::Var: {
163166
auto *var = cast<VarDecl>(decl);
164-
return llvm::any_of(var->getAllAccessors(), hasUnderscoredLifetimeAttr);
167+
return llvm::any_of(var->getAllAccessors(), hasLifetimeAttr);
165168
}
166169
default:
167-
return hasUnderscoredLifetimeAttr(decl);
170+
return hasLifetimeAttr(decl);
168171
}
169172
}
170173

171174
static bool usesFeatureLifetimeDependence(Decl *decl) {
172-
if (decl->getAttrs().hasAttribute<LifetimeAttr>()) {
173-
if (findUnderscoredLifetimeAttr(decl)) {
174-
// Experimental feature Lifetimes will guard the decl.
175-
return false;
176-
}
175+
if (findLifetimeAttr(decl, /*findUnderscored*/ false)) {
177176
return true;
178177
}
179178

179+
// Guard inferred lifetime dependencies with LifetimeDependence if it was
180+
// enabled.
181+
if (!decl->getASTContext().LangOpts.hasFeature(Feature::LifetimeDependence)) {
182+
return false;
183+
}
184+
185+
// Check for inferred lifetime dependencies
180186
if (auto *afd = dyn_cast<AbstractFunctionDecl>(decl)) {
181187
return afd->getInterfaceType()
182188
->getAs<AnyFunctionType>()
@@ -189,7 +195,25 @@ static bool usesFeatureLifetimeDependence(Decl *decl) {
189195
}
190196

191197
static bool usesFeatureLifetimes(Decl *decl) {
192-
return findUnderscoredLifetimeAttr(decl);
198+
if (findLifetimeAttr(decl, /*findUnderscored*/ true)) {
199+
return true;
200+
}
201+
202+
// Guard inferred lifetime dependencies with Lifetimes if it was enabled.
203+
if (!decl->getASTContext().LangOpts.hasFeature(Feature::Lifetimes)) {
204+
return false;
205+
}
206+
207+
// Check for inferred lifetime dependencies
208+
if (auto *afd = dyn_cast<AbstractFunctionDecl>(decl)) {
209+
return afd->getInterfaceType()
210+
->getAs<AnyFunctionType>()
211+
->hasLifetimeDependencies();
212+
}
213+
if (auto *varDecl = dyn_cast<VarDecl>(decl)) {
214+
return !varDecl->getTypeInContext()->isEscapable();
215+
}
216+
return false;
193217
}
194218

195219
static bool usesFeatureInoutLifetimeDependence(Decl *decl) {

test/ModuleInterface/Inputs/lifetime_underscored_dependence.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,15 @@ extension Container {
7777
}
7878
}
7979
}
80+
81+
public struct RigidArray : ~Copyable {
82+
@usableFromInline let _ptr: UnsafeRawBufferPointer
83+
84+
public var span: RawSpan {
85+
@_lifetime(borrow self)
86+
get {
87+
return RawSpan(_unsafeBytes: _ptr)
88+
}
89+
}
90+
}
91+

test/ModuleInterface/lifetime_underscored_dependence_test.swift

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

33
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -emit-module \
44
// RUN: -enable-experimental-feature Lifetimes \
5-
// RUN: -enable-experimental-feature Lifetimes \
65
// RUN: -o %t/lifetime_underscored_dependence.swiftmodule \
76
// RUN: -emit-module-interface-path %t/lifetime_underscored_dependence.swiftinterface \
87
// RUN: %S/Inputs/lifetime_underscored_dependence.swift
@@ -105,8 +104,22 @@ import lifetime_underscored_dependence
105104
// CHECK:}
106105
// CHECK:#endif
107106

108-
// Check that an implicitly dependent variable accessor is guarded by LifetimeDependence.
109-
//
110107
// CHECK: extension lifetime_underscored_dependence.Container {
111-
// CHECK-NEXT: #if compiler(>=5.3) && $LifetimeDependence
108+
// CHECK-NEXT: #if compiler(>=5.3) && $Lifetimes
112109
// CHECK-NEXT: public var storage: lifetime_underscored_dependence.BufferView {
110+
111+
// CHECK: public struct RigidArray : ~Swift.Copyable {
112+
// CHECK: @usableFromInline
113+
// CHECK: internal let _ptr: Swift.UnsafeRawBufferPointer
114+
// CHECK: #if compiler(>=5.3) && $Lifetimes
115+
// CHECK: public var span: Swift.RawSpan {
116+
// CHECK: @_lifetime(borrow self)
117+
// CHECK: get
118+
// CHECK: }
119+
// CHECK: #else
120+
// CHECK: public var span: Swift.RawSpan {
121+
// CHECK: @lifetime(borrow self)
122+
// CHECK: get
123+
// CHECK: }
124+
// CHECK: #endif
125+
// CHECK: }

0 commit comments

Comments
 (0)