File tree Expand file tree Collapse file tree 3 files changed +45
-1
lines changed Expand file tree Collapse file tree 3 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -7776,3 +7776,27 @@ bool UnsupportedRuntimeCheckedCastFailure::diagnoseAsError() {
7776
7776
.fixItReplace (getCastRange (), " as" );
7777
7777
return true ;
7778
7778
}
7779
+
7780
+ bool InvalidWeakAttributeUse::diagnoseAsError () {
7781
+ auto *pattern =
7782
+ dyn_cast_or_null<NamedPattern>(getAnchor ().dyn_cast <Pattern *>());
7783
+ if (!pattern)
7784
+ return false ;
7785
+
7786
+ auto *var = pattern->getDecl ();
7787
+ auto varType = getType (var);
7788
+
7789
+ auto diagnostic =
7790
+ emitDiagnosticAt (var, diag::invalid_ownership_not_optional,
7791
+ ReferenceOwnership::Weak, varType);
7792
+
7793
+ auto typeRange = var->getTypeSourceRangeForDiagnostics ();
7794
+ if (varType->hasSimpleTypeRepr ()) {
7795
+ diagnostic.fixItInsertAfter (typeRange.End , " ?" );
7796
+ } else {
7797
+ diagnostic.fixItInsert (typeRange.Start , " (" )
7798
+ .fixItInsertAfter (typeRange.End , " )?" );
7799
+ }
7800
+
7801
+ return true ;
7802
+ }
Original file line number Diff line number Diff line change @@ -2589,6 +2589,25 @@ class InvalidMemberRefOnProtocolMetatype final : public FailureDiagnostic {
2589
2589
bool diagnoseAsError () override ;
2590
2590
};
2591
2591
2592
+ // / Diagnose situations where `weak` attribute is used with a non-optional type
2593
+ // / in declaration e.g. `weak var x: <Type>`:
2594
+ // /
2595
+ // / \code
2596
+ // / class X {
2597
+ // / }
2598
+ // /
2599
+ // / weak var x: X = ...
2600
+ // / \endcode
2601
+ // /
2602
+ // / `weak` declaration is required to use an optional type e.g. `X?`.
2603
+ class InvalidWeakAttributeUse final : public FailureDiagnostic {
2604
+ public:
2605
+ InvalidWeakAttributeUse (const Solution &solution, ConstraintLocator *locator)
2606
+ : FailureDiagnostic(solution, locator) {}
2607
+
2608
+ bool diagnoseAsError () override ;
2609
+ };
2610
+
2592
2611
} // end namespace constraints
2593
2612
} // end namespace swift
2594
2613
Original file line number Diff line number Diff line change @@ -1992,7 +1992,8 @@ AllowInvalidStaticMemberRefOnProtocolMetatype::create(
1992
1992
1993
1993
bool AllowNonOptionalWeak::diagnose (const Solution &solution,
1994
1994
bool asNote) const {
1995
- return false ;
1995
+ InvalidWeakAttributeUse failure (solution, getLocator ());
1996
+ return failure.diagnose (asNote);
1996
1997
}
1997
1998
1998
1999
AllowNonOptionalWeak *AllowNonOptionalWeak::create (ConstraintSystem &cs,
You can’t perform that action at this time.
0 commit comments