Skip to content

Commit c62c289

Browse files
committed
[Diagnostics] Fix fix-it location for labeled tuple splat
In situations like this: ```swift func foo(x: (Int, Int) {} foo(x: 0, 0) ``` Left paren to form a missing tuple should be placed after the label because belongs to the parameter and not the tuple.
1 parent 7e6b4e4 commit c62c289

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4884,8 +4884,28 @@ bool InvalidTupleSplatWithSingleParameterFailure::diagnoseAsError() {
48844884
argExpr->getLoc(), diag::single_tuple_parameter_mismatch_normal,
48854885
choice->getDescriptiveKind(), name, paramTy, subsStr);
48864886

4887+
auto newLeftParenLoc = argExpr->getStartLoc();
4888+
if (auto *TE = dyn_cast<TupleExpr>(argExpr)) {
4889+
auto firstArgLabel = TE->getElementName(0);
4890+
// Cover situations like:
4891+
//
4892+
// func foo<T>(x: T) {}
4893+
// foo(x: 0, 1)
4894+
//
4895+
// Where left paren should be suggested after the label,
4896+
// since the label belongs to the parameter itself.
4897+
if (!firstArgLabel.empty()) {
4898+
auto paramTuple = resolveType(ParamType)->castTo<TupleType>();
4899+
// If the label of the first argument matches the one required
4900+
// by the parameter it would be omitted from the fixed parameter type.
4901+
if (!paramTuple->getElement(0).hasName())
4902+
newLeftParenLoc = Lexer::getLocForEndOfToken(getASTContext().SourceMgr,
4903+
TE->getElementNameLoc(0));
4904+
}
4905+
}
4906+
48874907
diagnostic.highlight(argExpr->getSourceRange())
4888-
.fixItInsertAfter(argExpr->getStartLoc(), "(")
4908+
.fixItInsertAfter(newLeftParenLoc, "(")
48894909
.fixItInsert(argExpr->getEndLoc(), ")");
48904910
return true;
48914911
}

test/Constraints/tuple_arguments.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ do {
7474
generic((3, 4))
7575

7676
genericLabeled(x: 3)
77-
genericLabeled(x: 3, 4) // expected-error {{global function 'genericLabeled' expects a single parameter of type 'T' [with T = (Int, Int)]}} {{18-18=(}} {{25-25=)}}
77+
genericLabeled(x: 3, 4) // expected-error {{global function 'genericLabeled' expects a single parameter of type 'T' [with T = (Int, Int)]}} {{20-20=(}} {{25-25=)}}
7878
genericLabeled(x: (3))
7979
genericLabeled(x: (3, 4))
8080

@@ -261,7 +261,7 @@ do {
261261
s.generic((3, 4))
262262

263263
s.genericLabeled(x: 3)
264-
s.genericLabeled(x: 3, 4) // expected-error {{instance method 'genericLabeled' expects a single parameter of type 'T' [with T = (Int, Int)]}} {{20-20=(}} {{27-27=)}}
264+
s.genericLabeled(x: 3, 4) // expected-error {{instance method 'genericLabeled' expects a single parameter of type 'T' [with T = (Int, Int)]}} {{22-22=(}} {{27-27=)}}
265265
s.genericLabeled(x: (3))
266266
s.genericLabeled(x: (3, 4))
267267

@@ -395,7 +395,7 @@ do {
395395
s.mutatingGeneric((3, 4))
396396

397397
s.mutatingGenericLabeled(x: 3)
398-
s.mutatingGenericLabeled(x: 3, 4) // expected-error {{instance method 'mutatingGenericLabeled' expects a single parameter of type 'T' [with T = (Int, Int)]}} {{28-28=(}} {{35-35=)}}
398+
s.mutatingGenericLabeled(x: 3, 4) // expected-error {{instance method 'mutatingGenericLabeled' expects a single parameter of type 'T' [with T = (Int, Int)]}} {{30-30=(}} {{35-35=)}}
399399
s.mutatingGenericLabeled(x: (3))
400400
s.mutatingGenericLabeled(x: (3, 4))
401401

@@ -932,7 +932,7 @@ do {
932932
_ = GenericInit(3, 4) // expected-error {{initializer expects a single parameter of type 'T' [with T = (Int, Int)]}} {{19-19=(}} {{23-23=)}}
933933
_ = GenericInit((3, 4))
934934

935-
_ = GenericInitLabeled(x: 3, 4) // expected-error {{initializer expects a single parameter of type 'T' [with T = (Int, Int)]}} {{26-26=(}} {{33-33=)}}
935+
_ = GenericInitLabeled(x: 3, 4) // expected-error {{initializer expects a single parameter of type 'T' [with T = (Int, Int)]}} {{28-28=(}} {{33-33=)}}
936936
_ = GenericInitLabeled(x: (3, 4))
937937

938938
_ = GenericInitTwo(3, 4)
@@ -1060,7 +1060,7 @@ do {
10601060
_ = s1[(3.0, 4.0)]
10611061

10621062
let s1a = GenericSubscriptLabeled<(Double, Double)>()
1063-
_ = s1a [x: 3.0, 4.0] // expected-error {{subscript expects a single parameter of type '(Double, Double)'}} {{12-12=(}} {{23-23=)}}
1063+
_ = s1a [x: 3.0, 4.0] // expected-error {{subscript expects a single parameter of type '(Double, Double)'}} {{14-14=(}} {{23-23=)}}
10641064
_ = s1a [x: (3.0, 4.0)]
10651065

10661066
let s2 = GenericSubscriptTwo<Double>()
@@ -1072,7 +1072,7 @@ do {
10721072
_ = s3[(3.0, 4.0)]
10731073

10741074
let s3a = GenericSubscriptLabeledTuple<Double>()
1075-
_ = s3a[x: 3.0, 4.0] // expected-error {{subscript expects a single parameter of type '(Double, Double)'}} {{11-11=(}} {{22-22=)}}
1075+
_ = s3a[x: 3.0, 4.0] // expected-error {{subscript expects a single parameter of type '(Double, Double)'}} {{13-13=(}} {{22-22=)}}
10761076
_ = s3a[x: (3.0, 4.0)]
10771077
}
10781078

@@ -1130,7 +1130,7 @@ do {
11301130
_ = GenericEnum.one(3, 4) // expected-error {{enum case 'one' expects a single parameter of type 'T' [with T = (Int, Int)]}} {{23-23=(}} {{27-27=)}}
11311131
_ = GenericEnum.one((3, 4))
11321132

1133-
_ = GenericEnum.labeled(x: 3, 4) // expected-error {{enum case 'labeled' expects a single parameter of type 'T' [with T = (Int, Int)]}} {{27-27=(}} {{34-34=)}}
1133+
_ = GenericEnum.labeled(x: 3, 4) // expected-error {{enum case 'labeled' expects a single parameter of type 'T' [with T = (Int, Int)]}} {{29-29=(}} {{34-34=)}}
11341134
_ = GenericEnum.labeled(x: (3, 4))
11351135
_ = GenericEnum.labeled(3, 4) // expected-error {{enum case 'labeled' expects a single parameter of type 'T' [with T = (Int, Int)]}} {{27-27=(}} {{31-31=)}}
11361136
_ = GenericEnum.labeled((3, 4)) // expected-error {{missing argument label 'x:' in call}}
@@ -1265,7 +1265,7 @@ do {
12651265
sTwo.requirement(3.0, 4.0) // expected-error {{instance method 'requirement' expects a single parameter of type 'GenericConforms<(Double, Double)>.Element' (aka '(Double, Double)')}} {{20-20=(}} {{28-28=)}}
12661266
sTwo.requirement((3.0, 4.0))
12671267

1268-
sTwo.requirementLabeled(x: 3.0, 4.0) // expected-error {{instance method 'requirementLabeled' expects a single parameter of type 'GenericConforms<(Double, Double)>.Element' (aka '(Double, Double)')}} {{27-27=(}} {{38-38=)}}
1268+
sTwo.requirementLabeled(x: 3.0, 4.0) // expected-error {{instance method 'requirementLabeled' expects a single parameter of type 'GenericConforms<(Double, Double)>.Element' (aka '(Double, Double)')}} {{29-29=(}} {{38-38=)}}
12691269
sTwo.requirementLabeled(x: (3.0, 4.0))
12701270
}
12711271

0 commit comments

Comments
 (0)