Skip to content

Commit 9ae2c85

Browse files
committed
[Typechecker] Use CallArgs for checking duplicate label & add new test cases
1 parent 92d41ef commit 9ae2c85

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
7676
public:
7777
TypeChecker &TC;
7878
const DeclContext *DC;
79-
CallExpr *funcCallExpr = nullptr;
8079

8180
DiagnoseWalker(TypeChecker &TC, const DeclContext *DC, bool isExprStmt)
8281
: IsExprStmt(isExprStmt), TC(TC), DC(DC) {}
@@ -135,10 +134,6 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
135134
}
136135
}
137136

138-
if (auto CE = dyn_cast<CallExpr>(E)) {
139-
funcCallExpr = CE;
140-
}
141-
142137
// Check function calls, looking through implicit conversions on the
143138
// function and inspecting the arguments directly.
144139
if (auto *Call = dyn_cast<ApplyExpr>(E)) {
@@ -255,14 +250,14 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
255250
// FIXME: Duplicate labels on enum payloads should be diagnosed
256251
// when declared, not when called.
257252
bool isEnumCase = false;
258-
if (funcCallExpr) {
259-
auto calledValue = funcCallExpr->getCalledValue();
253+
if (auto CE = dyn_cast_or_null<CallExpr>(Parent.getAsExpr())) {
254+
auto calledValue = CE->getCalledValue();
260255
if (calledValue) {
261256
isEnumCase = isa<EnumElementDecl>(calledValue);
262257
}
263258
}
264259

265-
if (!funcCallExpr || isEnumCase) {
260+
if ((!CallArgs.count(tupleExpr)) || isEnumCase) {
266261
auto diagnose = false;
267262

268263
llvm::SmallDenseSet<Identifier> names;

test/Constraints/tuple.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,15 @@ if case (foo: let x, foo: let y) = zeroTuple { print(x+y) } // expected-error {{
307307

308308
enum BishBash { case bar(foo: Int, foo: String) }
309309
let enumLabelDup: BishBash = .bar(foo: 0, foo: "") // expected-error {{cannot create a tuple with a duplicate element label}}
310+
311+
func dupLabelClosure(_ fn: () -> Void) {}
312+
dupLabelClosure { print((bar: "", bar: 5).bar) } // expected-error {{cannot create a tuple with a duplicate element label}}
313+
314+
struct DupLabelSubscript {
315+
subscript(foo x: Int, foo y: Int) -> Int {
316+
return 0
317+
}
318+
}
319+
320+
let dupLabelSubscriptStruct = DupLabelSubscript()
321+
let _ = dupLabelSubscriptStruct[foo: 5, foo: 5] // ok

test/Parse/strange_interpolation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ print("[\(x, foo: x)]")
3434
// expected-warning@-2{{interpolating multiple values will not form a tuple in Swift 5}}
3535
// expected-note@-3{{insert parentheses to keep current behavior}} {{11-11=(}} {{20-20=)}}
3636

37-
print("[\(foo: x, foo: x)]")
38-
// CHECK-NEXT: [(foo: 1, foo: 1)]
37+
print("[\(foo: x, bar: x)]")
38+
// CHECK-NEXT: [(foo: 1, bar: 1)]
3939
// expected-warning@-2{{interpolating multiple values will not form a tuple in Swift 5}}
4040
// expected-note@-3{{insert parentheses to keep current behavior}} {{11-11=(}} {{25-25=)}}
4141

0 commit comments

Comments
 (0)