@@ -2130,6 +2130,27 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
2130
2130
} else {
2131
2131
VD->diagnose (diag::property_does_not_override, isClassContext)
2132
2132
.highlight (OA->getLocation ());
2133
+ // If the override is a property-decl, check if there exists
2134
+ // a no-args func-decl in the base class, if yes, provide a fix-it.
2135
+ auto declName = VD->getBaseName ();
2136
+ auto declType = VD->getResultTypeRepr ();
2137
+ auto inherited = DC->getSelfClassDecl ()->getInherited ();
2138
+ // If there is an inherited context, perform member lookup.
2139
+ if (!inherited.empty ()) {
2140
+ auto superClass = inherited.front ().getType ();
2141
+ auto lookupResult = superClass->getAnyNominal ()->lookupDirect (declName);
2142
+
2143
+ for (auto & candidate : lookupResult) {
2144
+ // If the parameter list is empty, compare the return types
2145
+ // of the candidates with the declaration type.
2146
+ if (getParameterList (candidate)->size () == 0 ) {
2147
+ auto candidateReturnType = candidate->getResultTypeRepr ();
2148
+ if (candidateReturnType->getKind () == declType->getKind ())
2149
+ VD->diagnose (diag::override_property_in_lieu_of_method, declName.getIdentifier ())
2150
+ .fixItReplace (VD->Decl ::getSourceRangeIncludingAttrs (), " override func " + declName.getIdentifier ().str ().str () + " () -> " + VD->getInterfaceType ()->getRValueType ().getString () + " { <#code#> }" );
2151
+ }
2152
+ }
2153
+ }
2133
2154
}
2134
2155
OA->setInvalid ();
2135
2156
}
@@ -3158,6 +3179,27 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
3158
3179
} else {
3159
3180
FD->diagnose (diag::method_does_not_override, isClassContext)
3160
3181
.highlight (OA->getLocation ());
3182
+ // If the override is a no-args fun-decl, check if there exists
3183
+ // a no-args var-decl in the base class, if yes, provide a fix-it.
3184
+ if (FD->getParameters ()->size () == 0 ) {
3185
+ auto declName = FD->getBaseName ();
3186
+ auto declReturnType = FD->getResultInterfaceType ()->getRValueType ();
3187
+ auto inherited = DC->getSelfClassDecl ()->getInherited ();
3188
+ // If there is an inherited context, perform member lookup.
3189
+ if (!inherited.empty ()) {
3190
+ auto superClass = inherited.front ().getType ();
3191
+ auto lookupResult = superClass->getAnyNominal ()->lookupDirect (declName);
3192
+ // Compare the equality of return types for each candidate.
3193
+ for (auto & candidate : lookupResult) {
3194
+ auto candidateType = candidate->getInterfaceType ()->getRValueType ();
3195
+ if (candidateType->isEqual (declReturnType))
3196
+ FD->diagnose (diag::override_method_in_lieu_of_property, declName.getIdentifier ())
3197
+ .fixItRemove (FD->getBodySourceRange ())
3198
+ .fixItReplace (FD->getSourceRangeIncludingAttrs (),
3199
+ " override var " + declName.getIdentifier ().str ().str () + " : " + declReturnType.getString () + " { <#code#> }" );
3200
+ }
3201
+ }
3202
+ }
3161
3203
}
3162
3204
OA->setInvalid ();
3163
3205
}
0 commit comments