Skip to content

Commit 442d58e

Browse files
committed
Simplify TypeChecker::fixAbstractFunctionNames and eliminate a silly crash.
Don't crash here when we need to add '_' to a parameter list. However, while we're here, stop trying to emit Fix-Its for implicit declarations and stop worrying about introducing a ':' after the name for functions such as func foo(Int) { ... } because they're no longer permitted. Fixes rdar://problem/21333445. Swift SVN r30195
1 parent 6d77b99 commit 442d58e

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7160,6 +7160,10 @@ static void validateAttributes(TypeChecker &TC, Decl *D) {
71607160
void TypeChecker::fixAbstractFunctionNames(InFlightDiagnostic &diag,
71617161
AbstractFunctionDecl *func,
71627162
DeclName targetName) {
7163+
// There is no reasonable way to fix an implicitly-generated function.
7164+
if (func->isImplicit())
7165+
return;
7166+
71637167
auto name = func->getFullName();
71647168

71657169
// Fix the name of the function itself.
@@ -7183,7 +7187,6 @@ void TypeChecker::fixAbstractFunctionNames(InFlightDiagnostic &diag,
71837187

71847188
// Find the location to update or insert.
71857189
SourceLoc loc;
7186-
bool needColon = false;
71877190
if (tuplePattern) {
71887191
auto origPattern = tuplePattern->getElement(i).getPattern();
71897192
if (auto param = cast_or_null<ParamDecl>(origPattern->getSingleVar())) {
@@ -7215,7 +7218,6 @@ void TypeChecker::fixAbstractFunctionNames(InFlightDiagnostic &diag,
72157218
}
72167219

72177220
if (param->isImplicit()) {
7218-
needColon = true;
72197221
loc = origPattern->getLoc();
72207222
} else {
72217223
continue;
@@ -7225,35 +7227,25 @@ void TypeChecker::fixAbstractFunctionNames(InFlightDiagnostic &diag,
72257227
if (auto any = dyn_cast<AnyPattern>(
72267228
origPattern->getSemanticsProvidingPattern())) {
72277229
if (any->isImplicit()) {
7228-
needColon = true;
72297230
loc = origPattern->getLoc();
72307231
} else {
7231-
needColon = false;
72327232
loc = any->getLoc();
72337233
}
72347234
} else {
72357235
loc = origPattern->getLoc();
7236-
needColon = true;
72377236
}
72387237
} else if (auto paren = dyn_cast<ParenPattern>(pattern)) {
72397238
loc = paren->getSubPattern()->getLoc();
7240-
needColon = true;
7241-
7242-
// FIXME: Representation doesn't let us fix this easily.
7243-
if (targetArg.empty())
7244-
continue;
7245-
72467239
} else {
72477240
loc = pattern->getLoc();
7248-
needColon = true;
72497241
}
72507242

7251-
assert(!targetArg.empty() && "Must have a name here");
7252-
llvm::SmallString<8> replacement;
7253-
replacement += targetArg.str();
7254-
if (needColon)
7255-
replacement += ": ";
7256-
7243+
StringRef replacement;
7244+
if (targetArg.empty())
7245+
replacement = "_";
7246+
else
7247+
replacement = targetArg.str();
7248+
72577249
diag.fixItInsert(loc, replacement);
72587250
}
72597251

test/decl/protocol/req/name_mismatch.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ protocol LabeledRequirement {
3333
struct UnlabeledWitness : LabeledRequirement {
3434
func method(x _: Loadable) {}
3535
}
36+
37+
// rdar://problem/21333445
38+
protocol P2 {
39+
init(_ : Int) // expected-note{{requirement 'init' declared here}}
40+
}
41+
42+
struct XP2 : P2 { // expected-error{{initializer 'init(foo:)' has different argument names from those required by protocol 'P2' ('init')}}
43+
let foo: Int
44+
}

0 commit comments

Comments
 (0)