Skip to content

[Sema] FixIt for override func that should be override var and vice-versa #63858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3475,8 +3475,12 @@ ERROR(override_of_non_open,none,

ERROR(method_does_not_override,none,
"method does not override any method from its %select{parent protocol|superclass}0", (bool))
NOTE(override_property_not_method,none,
"did you mean to override the property %0", (Identifier))
ERROR(property_does_not_override,none,
"property does not override any property from its %select{parent protocol|superclass}0", (bool))
NOTE(override_method_not_property,none,
"did you mean to override the method %0", (Identifier))
ERROR(subscript_does_not_override,none,
"subscript does not override any subscript from its %select{parent protocol|superclass}0", (bool))
ERROR(initializer_does_not_override,none,
Expand Down
59 changes: 59 additions & 0 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2674,6 +2674,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
} else {
VD->diagnose(diag::property_does_not_override, isClassContext)
.highlight(OA->getLocation());
diagnoseOverridenProperty(VD, DC, OA);
}
OA->setInvalid();
}
Expand Down Expand Up @@ -2742,6 +2743,32 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}
}

static void diagnoseOverridenProperty(VarDecl *VD, DeclContext *DC,
OverrideAttr *OA) {
auto declName = VD->getBaseName();
auto declType = VD->getInterfaceType()->getRValueType();

auto inherited = DC->getSelfClassDecl()->getInherited();
if (!inherited.empty()) {
auto superClassType = inherited.getResolvedType(0);
auto lookupResult =
TypeChecker::lookupMember(DC, superClassType, DeclNameRef(declName));
for (auto &candidate : lookupResult) {
auto *valueDecl = candidate.getValueDecl();
if (auto funcDecl = dyn_cast<FuncDecl>(valueDecl)) {
if (funcDecl->getBaseName() == declName &&
funcDecl->getParameters()->size() == 0) {
std::string funcDeclTypeStr = " -> " + declType.getString();
VD->diagnose(diag::override_method_not_property,
funcDecl->getBaseName().getIdentifier())
.highlight(OA->getLocation())
.fixItReplace(getFixItLocForVarToLet(VD), "func");
}
}
}
}
}

bool checkBoundInOutVarDecl(PatternBindingDecl *pbd, unsigned patternIndex,
const Pattern *p, VarDecl *vd) {
// If our var decl doesn't have an initial value, error. We always want an
Expand Down Expand Up @@ -3714,6 +3741,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
} else {
FD->diagnose(diag::method_does_not_override, isClassContext)
.highlight(OA->getLocation());
diagnoseOverridenMethod(FD, DC, OA);
}
OA->setInvalid();
}
Expand Down Expand Up @@ -3804,6 +3832,37 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
TypeChecker::checkObjCImplementation(FD);
}

static void diagnoseOverridenMethod(FuncDecl *FD, DeclContext *DC,
OverrideAttr *OA) {
if (FD->getParameters()->size() == 0) {
auto declName = FD->getBaseName();
auto declRetType = FD->getResultInterfaceType()->getRValueType();

auto inherited = DC->getSelfClassDecl()->getInherited();
if (!inherited.empty()) {
auto superClassType = inherited.getResolvedType(0);
auto lookupResult = TypeChecker::lookupMember(DC, superClassType,
DeclNameRef(declName));
for (auto &candidate : lookupResult) {
auto *valueDecl = candidate.getValueDecl();
if (auto vardecl = dyn_cast<VarDecl>(valueDecl)) {
if (vardecl->getBaseName() == declName) {
std::string varDeclTypeStr = ": " + declRetType.getString();
FD->diagnose(diag::override_property_not_method,
vardecl->getBaseName().getIdentifier())
.highlight(OA->getLocation())
.fixItReplace(FD->getFuncLoc(), "var")
.fixItReplaceChars(
FD->getParameters()->getLParenLoc(),
FD->getBody()->getLBraceLoc().getAdvancedLoc(-1),
varDeclTypeStr);
}
}
}
}
}
}

void visitModuleDecl(ModuleDecl *) { }

void visitEnumCaseDecl(EnumCaseDecl *ECD) {
Expand Down
17 changes: 17 additions & 0 deletions test/decl/inherit/override.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,20 @@ class ObjCSub : ObjCSuper {

@objc(method3:withInt:) func method3(_ x: Sub, with y: Int) { } // expected-error{{method3(_:with:)' with Objective-C selector 'method3:withInt:' conflicts with method 'method3(_:withInt:)' from superclass 'ObjCSuper' with the same Objective-C selector}}
}

class C {
var v1: Bool {
return false
}

func f1() -> Int { }
}

class D: C {
override func v1() -> Bool { } // expected-error{{method does not override any method from its superclass}}
// expected-note@-1{{did you mean to override the property 'v1'}} {{12-16=var}} {{19-29=: Bool}}

override var f1: Int { // expected-error{{property does not override any property from its superclass}}
return 0 // expected-note@-1{{did you mean to override the method 'f1'}} {{12-15=func}}
}
}