Skip to content

Commit dd5a677

Browse files
author
Harlan Haskins
committed
[ParseableInterfaces] Handle lazy vars
For lazy vars, we need to make public the top-level entry point, and the fact that the lazy storage contributes to the layout of a type (if it’s fixed layout, or if the type is not resilient.) We also shouldn’t ever print `lazy` in a parseable interface. Since we need to parse these identifiers, give them a new name, `$__lazy_storage_$_{propname}`, which is parseable only in parseable interfaces so it doesn’t conflict with other properties.
1 parent b81c491 commit dd5a677

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ PrintOptions PrintOptions::printParseableInterfaceFile() {
178178
result.PrintAccess = true;
179179

180180
result.ExcludeAttrList = {DAK_ImplicitlyUnwrappedOptional, DAK_AccessControl,
181-
DAK_SetterAccess};
181+
DAK_SetterAccess, DAK_Lazy};
182182

183183
return result;
184184
}

lib/Sema/CodeSynthesis.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,9 @@ void swift::completeLazyVarImplementation(VarDecl *VD) {
13461346
assert(!VD->isStatic() && "Static vars are already lazy on their own");
13471347

13481348
// Create the storage property as an optional of VD's type.
1349-
SmallString<64> NameBuf = VD->getName().str();
1350-
NameBuf += ".storage";
1349+
SmallString<64> NameBuf;
1350+
NameBuf += "$__lazy_storage_$_";
1351+
NameBuf += VD->getName().str();
13511352
auto StorageName = Context.getIdentifier(NameBuf);
13521353
auto StorageTy = OptionalType::get(VD->getType());
13531354
auto StorageInterfaceTy = OptionalType::get(VD->getInterfaceType());
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -typecheck -module-name Test -emit-parseable-module-interface-path %t/Test.swiftinterface %s
4+
// RUN: %FileCheck %s < %t/Test.swiftinterface --check-prefix CHECK --check-prefix NONRESILIENT
5+
// RUN: %target-swift-frontend -build-module-from-parseable-interface %t/Test.swiftinterface -o %t/Test.swiftmodule
6+
// RUN: %target-swift-frontend -emit-module -o /dev/null -merge-modules -emit-parseable-module-interface-path - %t/Test.swiftmodule -module-name Test | %FileCheck %s --check-prefix CHECK --check-prefix NONRESILIENT
7+
8+
// RUN: %target-swift-frontend -typecheck -module-name TestResilient -emit-parseable-module-interface-path %t/TestResilient.swiftinterface -enable-resilience %s
9+
// RUN: %FileCheck %s < %t/TestResilient.swiftinterface --check-prefix CHECK --check-prefix RESILIENT
10+
11+
// RUN: %target-swift-frontend -build-module-from-parseable-interface %t/TestResilient.swiftinterface -o %t/TestResilient.swiftmodule
12+
// RUN: %target-swift-frontend -emit-module -o /dev/null -merge-modules -emit-parseable-module-interface-path - %t/TestResilient.swiftmodule -module-name TestResilient | %FileCheck %s --check-prefix CHECK --check-prefix RESILIENT
13+
14+
// CHECK: @_fixed_layout public struct HasLazyVarsFixedLayout {
15+
// CHECK-NEXT: public var foo: [[INT:(Swift\.)?Int]] {
16+
// CHECK-NEXT: mutating get
17+
// CHECK-NEXT: set
18+
// CHECK-NEXT: }
19+
// CHECK: private var $__lazy_storage_$_foo: [[INT]]?
20+
// CHECK-NOT: private var bar
21+
// CHECK: private var $__lazy_storage_$_bar: [[INT]]?
22+
// CHECK-NEXT: }
23+
@_fixed_layout
24+
public struct HasLazyVarsFixedLayout {
25+
public lazy var foo: Int = 0
26+
private lazy var bar: Int = 0
27+
}
28+
29+
// CHECK: public struct HasLazyVars {
30+
// CHECK-NEXT: public var foo: [[INT:(Swift\.)?Int]] {
31+
// CHECK-NEXT: mutating get
32+
// CHECK-NEXT: set
33+
// CHECK-NEXT: }
34+
// NONRESILIENT: private var $__lazy_storage_$_foo: [[INT]]?
35+
// CHECK-NOT: private var bar
36+
// NONRESILIENT: private var $__lazy_storage_$_bar: [[INT]]?
37+
// CHECK-NEXT: }
38+
public struct HasLazyVars {
39+
public lazy var foo: Int = 0
40+
private lazy var bar: Int = 0
41+
}

test/api-digester/Outputs/Cake-abi.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ cake1: Var fixedLayoutStruct.b in a non-resilient type changes position from 0 t
6262
cake1: Var fixedLayoutStruct2.BecomeFixedBinaryOrder is now a stored property
6363
cake1: Var fixedLayoutStruct2.NoLongerWithFixedBinaryOrder is no longer a stored property
6464
cake2: EnumElement FrozenKind.AddedCase is added to a non-resilient type
65+
cake2: Var fixedLayoutStruct.$__lazy_storage_$_lazy_d is added to a non-resilient type
6566
cake2: Var fixedLayoutStruct.c is added to a non-resilient type
66-
cake2: Var fixedLayoutStruct.lazy_d.storage is added to a non-resilient type
6767

6868
/* Conformance changes */
6969
cake1: Func ObjCProtocol.addOptional() is now an optional requirement

0 commit comments

Comments
 (0)