Skip to content

Commit db53fc7

Browse files
authored
[Sema] Diagnose wrapped property if its projected value property conflicts with lazy variable storage property (swiftlang#33152)
1 parent 5fffeb8 commit db53fc7

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current) const {
704704
// If both declarations are implicit, we do not diagnose anything
705705
// as it would lead to misleading diagnostics and it's likely that
706706
// there's nothing actionable about it due to its implicit nature.
707-
// One special case for this is property wrappers.
707+
// Special case for this is property wrappers or lazy variables.
708708
//
709709
// Otherwise, if 'current' is implicit, then we diagnose 'other'
710710
// since 'other' is a redeclaration of 'current'. Similarly, if
@@ -719,6 +719,22 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current) const {
719719
VD->getOriginalWrappedProperty()) {
720720
declToDiagnose = originalWrappedProperty;
721721
}
722+
723+
// If 'current' is a synthesized lazy storage variable and
724+
// 'other' is synthesized projected value variable, then
725+
// diagnose the wrapped property.
726+
//
727+
// TODO: We should probably emit a diagnostic note on the lazy
728+
// variable as well, but there is currently no way to grab the
729+
// lazy property from its backing storage.
730+
if (VD->isLazyStorageProperty()) {
731+
if (auto otherVar = dyn_cast<VarDecl>(other)) {
732+
if (auto originalWrappedProperty =
733+
otherVar->getOriginalWrappedProperty()) {
734+
declToDiagnose = originalWrappedProperty;
735+
}
736+
}
737+
}
722738
}
723739
} else {
724740
declToDiagnose = current->isImplicit() ? other : current;

test/decl/var/property_wrappers.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,14 @@ struct TestInvalidRedeclaration3 {
874874
var _foo1 = 123 // expected-error {{invalid redeclaration of synthesized property '_foo1'}}
875875
}
876876

877+
// Diagnose when wrapped property uses the name we use for lazy variable storage property.
878+
struct TestInvalidRedeclaration4 {
879+
@WrapperWithProjectedValue var __lazy_storage_$_foo: Int
880+
// expected-error@-1 {{invalid redeclaration of synthesized property '$__lazy_storage_$_foo'}}
881+
// expected-note@-2 {{'$__lazy_storage_$_foo' synthesized for property wrapper projected value}}
882+
lazy var foo = 1
883+
}
884+
877885
// ---------------------------------------------------------------------------
878886
// Closures in initializers
879887
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)