Skip to content

Commit c4e5233

Browse files
authored
Merge pull request swiftlang#34352 from slavapestov/local-redeclaration-check-source-compat
Sema: Allow duplicate internal parameter names on protocol requirements
2 parents a0a37d2 + 6a5684a commit c4e5233

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,13 +1318,21 @@ static void maybeDiagnoseClassWithoutInitializers(ClassDecl *classDecl) {
13181318
diagnoseClassWithoutInitializers(classDecl);
13191319
}
13201320

1321-
void TypeChecker::checkParameterList(ParameterList *params) {
1321+
void TypeChecker::checkParameterList(ParameterList *params,
1322+
DeclContext *owner) {
13221323
for (auto param: *params) {
13231324
checkDeclAttributes(param);
13241325
}
13251326

1326-
// Check for duplicate parameter names.
1327-
diagnoseDuplicateDecls(*params);
1327+
// For source compatibilty, allow duplicate internal parameter names
1328+
// on protocol requirements.
1329+
//
1330+
// FIXME: Consider turning this into a warning or error if we do
1331+
// another -swift-version.
1332+
if (!isa<ProtocolDecl>(owner->getParent())) {
1333+
// Check for duplicate parameter names.
1334+
diagnoseDuplicateDecls(*params);
1335+
}
13281336
}
13291337

13301338
void TypeChecker::diagnoseDuplicateBoundVars(Pattern *pattern) {
@@ -1769,7 +1777,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
17691777
(void) SD->isSetterMutating();
17701778
(void) SD->getImplInfo();
17711779

1772-
TypeChecker::checkParameterList(SD->getIndices());
1780+
TypeChecker::checkParameterList(SD->getIndices(), SD);
17731781

17741782
checkDefaultArguments(SD->getIndices());
17751783

@@ -2329,7 +2337,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
23292337

23302338
checkAccessControl(FD);
23312339

2332-
TypeChecker::checkParameterList(FD->getParameters());
2340+
TypeChecker::checkParameterList(FD->getParameters(), FD);
23332341
}
23342342

23352343
TypeChecker::checkDeclAttributes(FD);
@@ -2440,7 +2448,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
24402448
TypeChecker::checkDeclAttributes(EED);
24412449

24422450
if (auto *PL = EED->getParameterList()) {
2443-
TypeChecker::checkParameterList(PL);
2451+
TypeChecker::checkParameterList(PL, EED);
24442452

24452453
checkDefaultArguments(PL);
24462454
}
@@ -2596,7 +2604,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
25962604
}
25972605

25982606
TypeChecker::checkDeclAttributes(CD);
2599-
TypeChecker::checkParameterList(CD->getParameters());
2607+
TypeChecker::checkParameterList(CD->getParameters(), CD);
26002608

26012609
// Check whether this initializer overrides an initializer in its
26022610
// superclass.

lib/Sema/TypeCheckStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,7 @@ TypeCheckFunctionBodyRequest::evaluate(Evaluator &evaluator,
20612061
}
20622062

20632063
bool TypeChecker::typeCheckClosureBody(ClosureExpr *closure) {
2064-
TypeChecker::checkParameterList(closure->getParameters());
2064+
TypeChecker::checkParameterList(closure->getParameters(), closure);
20652065

20662066
BraceStmt *body = closure->getBody();
20672067

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ void typeCheckDecl(Decl *D);
448448

449449
void addImplicitDynamicAttribute(Decl *D);
450450
void checkDeclAttributes(Decl *D);
451-
void checkParameterList(ParameterList *params);
451+
void checkParameterList(ParameterList *params, DeclContext *owner);
452452

453453
void diagnoseDuplicateBoundVars(Pattern *pattern);
454454

test/Sema/redeclaration-checking.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,17 @@ func stmtTest() {
9898
func fullNameTest() {
9999
let x = 123 // expected-warning {{never used}}
100100
func x() {}
101+
}
102+
103+
// For source compatibility, allow duplicate parameter labels on
104+
// protocol requirements.
105+
protocol SillyProtocol {
106+
init(x: Int, x: Int)
107+
init(a x: Int, b x: Int)
108+
109+
func foo(x: Int, x: Int)
110+
func foo(a x: Int, b x: Int)
111+
112+
subscript(x: Int, x: Int) -> Int { get }
113+
subscript(a x: Int, b x: Int) -> Int { get }
101114
}

0 commit comments

Comments
 (0)