Skip to content
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

[TypeCheckDeclOverride] Add Fix-Its to "override" mismatch #34003

Open
wants to merge 4 commits 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
210 changes: 195 additions & 15 deletions lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,6 @@ bool swift::isOverrideBasedOnType(const ValueDecl *decl, Type declTy,
/// declaration in a superclass.
static bool areOverrideCompatibleSimple(ValueDecl *decl,
ValueDecl *parentDecl) {
// If the number of argument labels does not match, these overrides cannot
// be compatible.
if (decl->getName().getArgumentNames().size() !=
parentDecl->getName().getArgumentNames().size())
return false;

// If the parent declaration is not in a class (or extension thereof) or
// a protocol, we cannot override it.
if (decl->getDeclContext()->getSelfClassDecl() &&
Expand Down Expand Up @@ -487,6 +481,92 @@ namespace {
};
}

static bool fixMissingParameters(InFlightDiagnostic &diag, ValueDecl *decl,
ValueDecl *baseDecl) {
// Only check class override
if (!(decl->getDeclContext()->getSelfClassDecl() &&
baseDecl->getDeclContext()->getSelfClassDecl())) {
return false;
}

const ParameterList *derivedParams = nullptr;
const ParameterList *baseParams = nullptr;
if ((isa<AbstractFunctionDecl>(decl) &&
isa<AbstractFunctionDecl>(baseDecl)) ||
isa<SubscriptDecl>(baseDecl)) {
derivedParams = getParameterList(const_cast<ValueDecl *>(decl));
baseParams = getParameterList(const_cast<ValueDecl *>(baseDecl));
}

if (!derivedParams && !baseParams) {
return false;
}

int diff = baseParams->size() - derivedParams->size();
if (diff <= 0)
return false;

unsigned numMissing = abs(diff);

SmallString<32> scratch;
llvm::raw_svector_ostream fixIt(scratch);

auto subs = SubstitutionMap::getOverrideSubstitutions(baseDecl, decl,
/*derivedSubs=*/None);

bool isFirst = true;
for (unsigned i = baseParams->size() - numMissing, n = baseParams->size();
i != n; ++i) {
auto *baseParam = baseParams->get(i);
auto baseParamTy = baseParam->getInterfaceType();
if (baseParam->isInOut())
baseParamTy = baseParamTy->getInOutObjectType();
baseParamTy = baseParamTy.subst(subs);

auto argName = baseParam->getArgumentName();
auto paraName = baseParam->getParameterName();

if (isFirst) {
isFirst = false;
fixIt << ", ";
}

auto paraNameStr = paraName.empty() ? " _" : paraName.str();
auto argNameStr = argName.empty() ? " _" : argName.str();
if (paraName == argName) {
LucianoPAlmeida marked this conversation as resolved.
Show resolved Hide resolved
fixIt << argNameStr << ": ";
} else {
fixIt << argNameStr << " " << paraNameStr << ": ";
}

if (baseParam->isInOut())
fixIt << "inout ";

auto type = baseParamTy->getCanonicalType();
if (auto funcTy = dyn_cast<FunctionType>(type)) {
if (!funcTy->isNoEscape()) {
fixIt << "@escaping ";
}
}

if (baseParam->isVariadic()) {
fixIt << baseParam->getVarargBaseTy() << "...";
} else {
fixIt << baseParamTy;
}

if (i + 1 < n)
fixIt << ", ";
}

// The location where to insert stubs.
SourceLoc FixitLocation;
FixitLocation = derivedParams->get(derivedParams->size()-1)->getEndLoc();
diag.fixItInsertAfter(FixitLocation, fixIt.str());

return true;
}

static void diagnoseGeneralOverrideFailure(ValueDecl *decl,
ArrayRef<OverrideMatch> matches,
OverrideCheckingAttempt attempt) {
Expand Down Expand Up @@ -524,15 +604,86 @@ static void diagnoseGeneralOverrideFailure(ValueDecl *decl,
continue;
}

auto diag = diags.diagnose(matchDecl, diag::overridden_near_match_here,
matchDecl->getDescriptiveKind(),
matchDecl->getName());
if (attempt == OverrideCheckingAttempt::BaseName) {
if (decl->getName().getArgumentNames().size()
< matchDecl->getName().getArgumentNames().size()) {
auto diag = diags.diagnose(matchDecl, diag::overridden_near_match_here,
matchDecl->getDescriptiveKind(),
matchDecl->getName());
fixMissingParameters(diag, decl, matchDecl);
} else {
auto diag = diags.diagnose(matchDecl, diag::overridden_near_match_here,
matchDecl->getDescriptiveKind(),
matchDecl->getName());
fixDeclarationName(diag, decl, matchDecl->getName());
}
}
}

static bool partialParameterMatch(const ValueDecl *derivedDecl,
const ValueDecl *baseDecl,
TypeMatchOptions matchMode) {
const ParameterList *derivedParams = nullptr;
const ParameterList *baseParams = nullptr;
if ((isa<AbstractFunctionDecl>(derivedDecl) &&
isa<AbstractFunctionDecl>(baseDecl)) ||
isa<SubscriptDecl>(baseDecl)) {
derivedParams = getParameterList(const_cast<ValueDecl *>(derivedDecl));
baseParams = getParameterList(const_cast<ValueDecl *>(baseDecl));
}

if (!derivedParams && !baseParams) {
return false;
}

if (baseParams->size() == derivedParams->size())
return false;

auto subs = SubstitutionMap::getOverrideSubstitutions(baseDecl, derivedDecl,
/*derivedSubs=*/None);

for (auto i : indices(derivedParams->getArray())) {
auto *baseParam = baseParams->get(i);
auto *derivedParam = derivedParams->get(i);
// Make sure parameter name match
if (baseParam->getParameterName() != derivedParam->getParameterName())
return false;

// Make sure inout-ness and varargs match.
if (baseParam->isInOut() != derivedParam->isInOut() ||
baseParam->isVariadic() != derivedParam->isVariadic()) {
return false;
}

auto baseParamTy = baseParam->getInterfaceType();
baseParamTy = baseParamTy.subst(subs);
auto derivedParamTy = derivedParam->getInterfaceType();

if (baseParam->isInOut() || baseParam->isVariadic()) {
// Inout and vararg parameters must match exactly.
if (baseParamTy->isEqual(derivedParamTy))
continue;
} else {
// Attempt contravariant match.
if (baseParamTy->matchesParameter(derivedParamTy, matchMode))
continue;

// Try once more for a match, using the underlying type of an
// IUO if we're allowing that.
if (baseParam->isImplicitlyUnwrappedOptional() &&
matchMode.contains(TypeMatchFlags::AllowNonOptionalForIUOParam)) {
baseParamTy = baseParamTy->getOptionalObjectType();
if (baseParamTy->matches(derivedParamTy, matchMode))
continue;
}
}

// If there is no match, then we're done.
return false;
}

return true;
}

static bool parameterTypesMatch(const ValueDecl *derivedDecl,
const ValueDecl *baseDecl,
TypeMatchOptions matchMode) {
Expand Down Expand Up @@ -925,6 +1076,25 @@ SmallVector<OverrideMatch, 2> OverrideMatcher::match(
matches.push_back({parentDecl, false});
continue;
}

// collect matches for method_does_not_override
if ((attempt == OverrideCheckingAttempt::BaseName) &&
(decl->getBaseName() == parentDecl->getBaseName() &&
(decl->getName().getArgumentNames().size() <
parentDecl->getName().getArgumentNames().size()) &&
(declFnTy && parentDeclFnTy))) {
auto partiaParamsAndResultMatch = [=]() -> bool {
return partialParameterMatch(decl, parentDecl, matchMode) &&
declFnTy->getResult()->matches(parentDeclFnTy->getResult(),
matchMode);
};

if (declFnTy->matchesFunctionType(parentDeclFnTy, matchMode,
partiaParamsAndResultMatch)) {
matches.push_back({parentDecl, false});
continue;
}
}
}

// If we have more than one match, and any of them was exact, remove all
Expand Down Expand Up @@ -1054,11 +1224,21 @@ bool OverrideMatcher::checkOverride(ValueDecl *baseDecl,
// If the name of our match differs from the name we were looking for,
// complain.
if (decl->getName() != baseDecl->getName()) {
auto diag = diags.diagnose(decl, diag::override_argument_name_mismatch,
isa<ConstructorDecl>(decl),
decl->getName(),
baseDecl->getName());
fixDeclarationName(diag, decl, baseDecl->getName());
if ((decl->getName().getArgumentNames().size()
< baseDecl->getName().getArgumentNames().size())
&& attempt == OverrideCheckingAttempt::BaseName) {
auto diagKind = diag::method_does_not_override;
if (isa<ConstructorDecl>(decl))
diagKind = diag::initializer_does_not_override;
auto diag = diags.diagnose(decl, diagKind);
fixMissingParameters(diag, decl, baseDecl);
} else {
auto diag = diags.diagnose(decl, diag::override_argument_name_mismatch,
isa<ConstructorDecl>(decl),
decl->getName(),
baseDecl->getName());
fixDeclarationName(diag, decl, baseDecl->getName());
}
emittedMatchError = true;
}

Expand Down
16 changes: 16 additions & 0 deletions test/decl/class/override.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ class H : G {
override init(b: Double) {} // expected-error{{initializer does not override a designated initializer from its superclass}} {{none}}
}

class J {
func f1(_: Int, int1: Int) { } // expected-note * {{here}}

func g1(_: Int, int1: inout Int, _ a: Int..., d: String) { } // expected-note * {{here}}

init(a: Int, string: String) {} // expected-note * {{here}}
}

class K: J {
override func f1(_: Int) { } // expected-error{{method does not override any method from its superclass}} {{26-26=, int1: Int}}

override func g1(_: Int) { } // expected-error{{method does not override any method from its superclass}} {{26-26=, int1: inout Int, _ a: Int..., d: String}}

override init(a: Int) {} // expected-error{{declaration 'init(a:)' has different argument labels from any potential overrides}} {{23-23=, string: String}}
}

@objc class IUOTestBaseClass {
@objc func none() {}

Expand Down