Skip to content

Commit 8b05b0b

Browse files
authored
For 'lazy', make "cannot override with a stored property" a warning (#13308)
Previous versions of Swift accidentally treated lazy properties as computed properties because of how they were implemented. Now that we check this correctly, we've broken source compatibility. Downgrade the error to a warning in this case. (Arguably we could /allow/ overriding with a stored property. The original concerns were that you could accidentally end up with extra storage you didn't need, and that observing accessors would behave differently based on whether or not the property was overriding. But there's at least no ambiguity for 'lazy', which can't have observing accessors today.) rdar://problem/35870371 (cherry picked from commit 7c707ce)
1 parent fc37967 commit 8b05b0b

File tree

4 files changed

+455
-2
lines changed

4 files changed

+455
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,8 @@ ERROR(override_property_type_mismatch,none,
18771877
(Identifier, Type, Type))
18781878
ERROR(override_with_stored_property,none,
18791879
"cannot override with a stored property %0", (Identifier))
1880+
WARNING(override_with_stored_property_warn,none,
1881+
"cannot override with a stored property %0", (Identifier))
18801882
ERROR(observing_readonly_property,none,
18811883
"cannot observe read-only property %0; it can't change", (Identifier))
18821884
ERROR(override_mutable_with_readonly_property,none,

lib/Sema/TypeCheckDecl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6623,7 +6623,15 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
66236623

66246624
// Make sure that the overriding property doesn't have storage.
66256625
if (overrideASD->hasStorage() && !overrideASD->hasObservers()) {
6626-
TC.diagnose(overrideASD, diag::override_with_stored_property,
6626+
auto diagID = diag::override_with_stored_property;
6627+
if (!TC.Context.isSwiftVersionAtLeast(5) &&
6628+
overrideASD->getAttrs().hasAttribute<LazyAttr>()) {
6629+
// Swift 4.0 had a bug where lazy properties were considered
6630+
// computed by the time of this check. Downgrade this diagnostic to
6631+
// a warning.
6632+
diagID = diag::override_with_stored_property_warn;
6633+
}
6634+
TC.diagnose(overrideASD, diagID,
66276635
overrideASD->getBaseName().getIdentifier());
66286636
TC.diagnose(baseASD, diag::property_override_here);
66296637
return true;

0 commit comments

Comments
 (0)