Skip to content

Commit e7f0ca0

Browse files
committed
Sema: Synthesize materializeForSet for lazy properties
Fixes <rdar://problem/25707968>.
1 parent 46e3590 commit e7f0ca0

File tree

9 files changed

+77
-5
lines changed

9 files changed

+77
-5
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,8 +830,8 @@ namespace {
830830

831831
// If the declaration is dynamically dispatched through a class,
832832
// we have to use materializeForSet.
833-
if (isa<ClassDecl>(decl->getDeclContext())) {
834-
if (decl->isFinal())
833+
if (auto *classDecl = dyn_cast<ClassDecl>(decl->getDeclContext())) {
834+
if (decl->isFinal() || classDecl->isFinal())
835835
return false;
836836

837837
return true;

lib/Sema/CodeSynthesis.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,8 @@ void TypeChecker::completeLazyVarImplementation(VarDecl *VD) {
15761576
"variable not validated yet");
15771577
assert(!VD->isStatic() && "Static vars are already lazy on their own");
15781578

1579+
maybeAddMaterializeForSet(VD, *this);
1580+
15791581
// Create the storage property as an optional of VD's type.
15801582
SmallString<64> NameBuf = VD->getName().str();
15811583
NameBuf += ".storage";

test/SILGen/materializeForSet.swift

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,49 @@ struct TuxedoPanda : Panda { }
459459
// CHECK: }
460460

461461

462+
// Test for materializeForSet vs lazy properties of structs.
463+
464+
struct LazyStructProperty {
465+
lazy var cat: Int = 5
466+
}
467+
468+
// CHECK-LABEL: sil hidden @_TF17materializeForSet31inoutAccessOfLazyStructPropertyFT1lRVS_18LazyStructProperty_T_
469+
// CHECK: function_ref @_TFV17materializeForSet18LazyStructPropertyg3catSi
470+
// CHECK: function_ref @_TFV17materializeForSet18LazyStructPropertys3catSi
471+
func inoutAccessOfLazyStructProperty(l: inout LazyStructProperty) {
472+
increment(&l.cat)
473+
}
474+
475+
// Test for materializeForSet vs lazy properties of classes.
476+
477+
// CHECK-LABEL: sil hidden [transparent] @_TFC17materializeForSet17LazyClassPropertym3catSi
478+
479+
class LazyClassProperty {
480+
lazy var cat: Int = 5
481+
}
482+
483+
// CHECK-LABEL: sil hidden @_TF17materializeForSet30inoutAccessOfLazyClassPropertyFT1lRCS_17LazyClassProperty_T_
484+
// CHECK: class_method {{.*}} : $LazyClassProperty, #LazyClassProperty.cat!materializeForSet.1
485+
func inoutAccessOfLazyClassProperty(l: inout LazyClassProperty) {
486+
increment(&l.cat)
487+
}
488+
489+
// Test for materializeForSet vs lazy properties of final classes.
490+
491+
final class LazyFinalClassProperty {
492+
lazy var cat: Int = 5
493+
}
494+
495+
// CHECK-LABEL: sil hidden @_TF17materializeForSet35inoutAccessOfLazyFinalClassPropertyFT1lRCS_22LazyFinalClassProperty_T_
496+
// CHECK: function_ref @_TFC17materializeForSet22LazyFinalClassPropertyg3catSi
497+
// CHECK: function_ref @_TFC17materializeForSet22LazyFinalClassPropertys3catSi
498+
func inoutAccessOfLazyFinalClassProperty(l: inout LazyFinalClassProperty) {
499+
increment(&l.cat)
500+
}
501+
502+
462503
// CHECK-LABEL: sil_witness_table hidden Bill: Totalled module materializeForSet {
463504
// CHECK: method #Totalled.total!getter.1: @_TTWV17materializeForSet4BillS_8TotalledS_FS1_g5totalSi
464505
// CHECK: method #Totalled.total!setter.1: @_TTWV17materializeForSet4BillS_8TotalledS_FS1_s5totalSi
465506
// CHECK: method #Totalled.total!materializeForSet.1: @_TTWV17materializeForSet4BillS_8TotalledS_FS1_m5totalSi
466507
// CHECK: }
467-

test/multifile/synthesized-accessors/one-module-internal/library.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ final class Beer {
2424
set { }
2525
}
2626
}
27+
28+
class LazyCat {
29+
lazy var purrs: Int = 10
30+
}

test/multifile/synthesized-accessors/one-module-internal/main.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@ protocol Beverage {
1919

2020
extension Beer : Beverage {}
2121

22+
protocol PurrExtractor {
23+
var purrs: Int { get set }
24+
}
25+
26+
extension LazyCat : PurrExtractor {}
27+
2228
// Dummy statement
2329
_ = ()

test/multifile/synthesized-accessors/one-module-public/library.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ public struct FishAndChips {
1818
}
1919
}
2020

21-
final class Beer {
22-
var abv: Int {
21+
public final class Beer {
22+
public var abv: Int {
2323
get { return 7 }
2424
set { }
2525
}
2626
}
27+
28+
public class LazyCat {
29+
public lazy var purrs: Int = 10
30+
}

test/multifile/synthesized-accessors/one-module-public/main.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@ protocol Beverage {
1919

2020
extension Beer : Beverage {}
2121

22+
protocol PurrExtractor {
23+
var purrs: Int { get set }
24+
}
25+
26+
extension LazyCat : PurrExtractor {}
27+
2228
// Dummy statement
2329
_ = ()

test/multifile/synthesized-accessors/two-modules/library.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ public final class Beer {
2424
set { }
2525
}
2626
}
27+
28+
public class LazyCat {
29+
public lazy var purrs: Int = 10
30+
}

test/multifile/synthesized-accessors/two-modules/main.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@ protocol Beverage {
2323

2424
extension Beer : Beverage {}
2525

26+
protocol PurrExtractor {
27+
var purrs: Int { get set }
28+
}
29+
30+
extension LazyCat : PurrExtractor {}
31+
2632
// Dummy statement
2733
_ = ()

0 commit comments

Comments
 (0)