Skip to content

Commit 0dc8633

Browse files
committed
[Diagnostics] Extend single parameter splat diagnostic to report substitutions
1 parent aaae13e commit 0dc8633

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,11 +3389,11 @@ ERROR(nested_tuple_parameter_destructuring,none,
33893389
"nested tuple parameter %0 of function %1 "
33903390
"does not support destructuring", (Type, Type))
33913391
ERROR(single_tuple_parameter_mismatch_special,none,
3392-
"%0 expects a single parameter of type %1",
3393-
(DescriptiveDeclKind, Type))
3392+
"%0 expects a single parameter of type %1%2",
3393+
(DescriptiveDeclKind, Type, StringRef))
33943394
ERROR(single_tuple_parameter_mismatch_normal,none,
3395-
"%0 %1 expects a single parameter of type %2",
3396-
(DescriptiveDeclKind, DeclBaseName, Type))
3395+
"%0 %1 expects a single parameter of type %2%3",
3396+
(DescriptiveDeclKind, DeclBaseName, Type, StringRef))
33973397
ERROR(unknown_single_tuple_parameter_mismatch,none,
33983398
"single parameter of type %0 is expected in call", (Type))
33993399

lib/Sema/CSDiagnostics.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,25 +3507,45 @@ bool InvalidTupleSplatWithSingleParameterFailure::diagnoseAsError() {
35073507
if (!argExpr)
35083508
return false;
35093509

3510+
using Substitution = std::pair<GenericTypeParamType *, Type>;
3511+
llvm::SetVector<Substitution> substitutions;
35103512
auto paramTy = ParamType.transform([&](Type type) -> Type {
35113513
if (auto *typeVar = type->getAs<TypeVariableType>()) {
3512-
auto *GP = typeVar->getImpl().getGenericParameter();
3513-
return GP ? GP : resolveType(typeVar);
3514+
type = resolveType(typeVar);
3515+
3516+
if (auto *GP = typeVar->getImpl().getGenericParameter()) {
3517+
substitutions.insert(std::make_pair(GP, type));
3518+
return GP;
3519+
}
35143520
}
35153521

35163522
return type;
35173523
});
35183524

35193525
DeclBaseName name = choice->getBaseName();
35203526

3527+
std::string subsStr;
3528+
if (!substitutions.empty()) {
3529+
subsStr += " [where ";
3530+
interleave(
3531+
substitutions,
3532+
[&subsStr](const Substitution &substitution) {
3533+
subsStr += substitution.first->getString();
3534+
subsStr += " = ";
3535+
subsStr += substitution.second->getString();
3536+
},
3537+
[&subsStr] { subsStr += ", "; });
3538+
subsStr += ']';
3539+
}
3540+
35213541
auto diagnostic =
35223542
name.isSpecial()
35233543
? emitDiagnostic(argExpr->getLoc(),
35243544
diag::single_tuple_parameter_mismatch_special,
3525-
choice->getDescriptiveKind(), paramTy)
3526-
: emitDiagnostic(argExpr->getLoc(),
3527-
diag::single_tuple_parameter_mismatch_normal,
3528-
choice->getDescriptiveKind(), name, paramTy);
3545+
choice->getDescriptiveKind(), paramTy, subsStr)
3546+
: emitDiagnostic(
3547+
argExpr->getLoc(), diag::single_tuple_parameter_mismatch_normal,
3548+
choice->getDescriptiveKind(), name, paramTy, subsStr);
35293549

35303550
diagnostic.highlight(argExpr->getSourceRange())
35313551
.fixItInsertAfter(argExpr->getStartLoc(), "(")

test/Constraints/keyword_arguments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ func acceptTuple1<T>(_ x: (T, Bool)) { }
399399

400400
acceptTuple1(produceTuple1())
401401
acceptTuple1((1, false))
402-
acceptTuple1(1, false) // expected-error {{global function 'acceptTuple1' expects a single parameter of type '(T, Bool)'}} {{14-14=(}} {{22-22=)}}
402+
acceptTuple1(1, false) // expected-error {{global function 'acceptTuple1' expects a single parameter of type '(T, Bool)' [where T = Int]}} {{14-14=(}} {{22-22=)}}
403403

404404
func acceptTuple2<T>(_ input : T) -> T { return input }
405405
var tuple1 = (1, "hello")

test/Constraints/tuple_arguments.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ do {
8080
genericTwo(3, 4)
8181
genericTwo((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
8282

83-
genericTuple(3, 4) // expected-error {{global function 'genericTuple' expects a single parameter of type '(T, U)'}} {{16-16=(}} {{20-20=)}}
83+
genericTuple(3, 4) // expected-error {{global function 'genericTuple' expects a single parameter of type '(T, U)' [where T = Int, U = Int]}} {{16-16=(}} {{20-20=)}}
8484
genericTuple((3, 4))
8585
}
8686

@@ -101,7 +101,7 @@ do {
101101
genericTwo((a, b)) // expected-error {{missing argument for parameter #2 in call}}
102102
genericTwo(d) // expected-error {{missing argument for parameter #2 in call}}
103103

104-
genericTuple(a, b) // expected-error {{global function 'genericTuple' expects a single parameter of type '(T, U)'}} {{16-16=(}} {{20-20=)}}
104+
genericTuple(a, b) // expected-error {{global function 'genericTuple' expects a single parameter of type '(T, U)' [where T = Int, U = Int]}} {{16-16=(}} {{20-20=)}}
105105
genericTuple((a, b))
106106
genericTuple(d)
107107
}
@@ -123,7 +123,7 @@ do {
123123
genericTwo((a, b)) // expected-error {{missing argument for parameter #2 in call}}
124124
genericTwo(d) // expected-error {{missing argument for parameter #2 in call}}
125125

126-
genericTuple(a, b) // expected-error {{global function 'genericTuple' expects a single parameter of type '(T, U)'}} {{16-16=(}} {{20-20=)}}
126+
genericTuple(a, b) // expected-error {{global function 'genericTuple' expects a single parameter of type '(T, U)' [where T = Int, U = Int]}} {{16-16=(}} {{20-20=)}}
127127
genericTuple((a, b))
128128
genericTuple(d)
129129
}
@@ -267,7 +267,7 @@ do {
267267
s.genericTwo(3, 4)
268268
s.genericTwo((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
269269

270-
s.genericTuple(3, 4) // expected-error {{instance method 'genericTuple' expects a single parameter of type '(T, U)'}} {{18-18=(}} {{22-22=)}}
270+
s.genericTuple(3, 4) // expected-error {{instance method 'genericTuple' expects a single parameter of type '(T, U)' [where T = Int, U = Int]}} {{18-18=(}} {{22-22=)}}
271271
s.genericTuple((3, 4))
272272
}
273273

@@ -289,7 +289,7 @@ do {
289289
s.genericTwo((a, b)) // expected-error {{missing argument for parameter #2 in call}}
290290
s.genericTwo(d) // expected-error {{missing argument for parameter #2 in call}}
291291

292-
s.genericTuple(a, b) // expected-error {{instance method 'genericTuple' expects a single parameter of type '(T, U)'}} {{18-18=(}} {{22-22=)}}
292+
s.genericTuple(a, b) // expected-error {{instance method 'genericTuple' expects a single parameter of type '(T, U)' [where T = Int, U = Int]}} {{18-18=(}} {{22-22=)}}
293293
s.genericTuple((a, b))
294294
s.genericTuple(d)
295295
}
@@ -312,7 +312,7 @@ do {
312312
s.genericTwo((a, b)) // expected-error {{missing argument for parameter #2 in call}}
313313
s.genericTwo(d) // expected-error {{missing argument for parameter #2 in call}}
314314

315-
s.genericTuple(a, b) // expected-error {{instance method 'genericTuple' expects a single parameter of type '(T, U)'}} {{18-18=(}} {{22-22=)}}
315+
s.genericTuple(a, b) // expected-error {{instance method 'genericTuple' expects a single parameter of type '(T, U)' [where T = Int, U = Int]}} {{18-18=(}} {{22-22=)}}
316316
s.genericTuple((a, b))
317317
s.genericTuple(d)
318318
}
@@ -401,7 +401,7 @@ do {
401401
s.mutatingGenericTwo(3, 4)
402402
s.mutatingGenericTwo((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
403403

404-
s.mutatingGenericTuple(3, 4) // expected-error {{instance method 'mutatingGenericTuple' expects a single parameter of type '(T, U)'}} {{26-26=(}} {{30-30=)}}
404+
s.mutatingGenericTuple(3, 4) // expected-error {{instance method 'mutatingGenericTuple' expects a single parameter of type '(T, U)' [where T = Int, U = Int]}} {{26-26=(}} {{30-30=)}}
405405
s.mutatingGenericTuple((3, 4))
406406
}
407407

@@ -423,7 +423,7 @@ do {
423423
s.mutatingGenericTwo((a, b)) // expected-error {{missing argument for parameter #2 in call}}
424424
s.mutatingGenericTwo(d) // expected-error {{missing argument for parameter #2 in call}}
425425

426-
s.mutatingGenericTuple(a, b) // expected-error {{instance method 'mutatingGenericTuple' expects a single parameter of type '(T, U)'}} {{26-26=(}} {{30-30=)}}
426+
s.mutatingGenericTuple(a, b) // expected-error {{instance method 'mutatingGenericTuple' expects a single parameter of type '(T, U)' [where T = Int, U = Int]}} {{26-26=(}} {{30-30=)}}
427427
s.mutatingGenericTuple((a, b))
428428
s.mutatingGenericTuple(d)
429429
}
@@ -446,7 +446,7 @@ do {
446446
s.mutatingGenericTwo((a, b)) // expected-error {{missing argument for parameter #2 in call}}
447447
s.mutatingGenericTwo(d) // expected-error {{missing argument for parameter #2 in call}}
448448

449-
s.mutatingGenericTuple(a, b) // expected-error {{instance method 'mutatingGenericTuple' expects a single parameter of type '(T, U)'}} {{26-26=(}} {{30-30=)}}
449+
s.mutatingGenericTuple(a, b) // expected-error {{instance method 'mutatingGenericTuple' expects a single parameter of type '(T, U)' [where T = Int, U = Int]}} {{26-26=(}} {{30-30=)}}
450450
s.mutatingGenericTuple((a, b))
451451
s.mutatingGenericTuple(d)
452452
}
@@ -937,10 +937,10 @@ do {
937937
_ = GenericInitTwo(3, 4)
938938
_ = GenericInitTwo((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
939939

940-
_ = GenericInitTuple(3, 4) // expected-error {{initializer expects a single parameter of type '(T, T)'}} {{24-24=(}} {{28-28=)}}
940+
_ = GenericInitTuple(3, 4) // expected-error {{initializer expects a single parameter of type '(T, T)' [where T = Int]}} {{24-24=(}} {{28-28=)}}
941941
_ = GenericInitTuple((3, 4))
942942

943-
_ = GenericInitLabeledTuple(x: 3, 4) // expected-error {{initializer expects a single parameter of type '(T, T)'}}
943+
_ = GenericInitLabeledTuple(x: 3, 4) // expected-error {{initializer expects a single parameter of type '(T, T)' [where T = Int]}}
944944
_ = GenericInitLabeledTuple(x: (3, 4))
945945
}
946946

@@ -974,7 +974,7 @@ do {
974974
_ = GenericInitTwo((a, b)) // expected-error {{missing argument for parameter #2 in call}}
975975
_ = GenericInitTwo(c) // expected-error {{missing argument for parameter #2 in call}}
976976

977-
_ = GenericInitTuple(a, b) // expected-error {{initializer expects a single parameter of type '(T, T)'}} {{24-24=(}} {{28-28=)}}
977+
_ = GenericInitTuple(a, b) // expected-error {{initializer expects a single parameter of type '(T, T)' [where T = Int]}} {{24-24=(}} {{28-28=)}}
978978
_ = GenericInitTuple((a, b))
979979
_ = GenericInitTuple(c)
980980
}
@@ -1010,7 +1010,7 @@ do {
10101010
_ = GenericInitTwo((a, b)) // expected-error {{missing argument for parameter #2 in call}}
10111011
_ = GenericInitTwo(c) // expected-error {{missing argument for parameter #2 in call}}
10121012

1013-
_ = GenericInitTuple(a, b) // expected-error {{initializer expects a single parameter of type '(T, T)'}} {{24-24=(}} {{28-28=)}}
1013+
_ = GenericInitTuple(a, b) // expected-error {{initializer expects a single parameter of type '(T, T)' [where T = Int]}} {{24-24=(}} {{28-28=)}}
10141014
_ = GenericInitTuple((a, b))
10151015
_ = GenericInitTuple(c)
10161016
}
@@ -1137,7 +1137,7 @@ do {
11371137
_ = GenericEnum.two(3, 4)
11381138
_ = GenericEnum.two((3, 4)) // expected-error {{missing argument for parameter #2 in call}}
11391139

1140-
_ = GenericEnum.tuple(3, 4) // expected-error {{enum case 'tuple' expects a single parameter of type '(T, T)'}} {{25-25=(}} {{29-29=)}}
1140+
_ = GenericEnum.tuple(3, 4) // expected-error {{enum case 'tuple' expects a single parameter of type '(T, T)' [where T = Int]}} {{25-25=(}} {{29-29=)}}
11411141
_ = GenericEnum.tuple((3, 4))
11421142
}
11431143

@@ -1170,7 +1170,7 @@ do {
11701170
_ = GenericEnum.two((a, b)) // expected-error {{missing argument for parameter #2 in call}}
11711171
_ = GenericEnum.two(c) // expected-error {{missing argument for parameter #2 in call}}
11721172

1173-
_ = GenericEnum.tuple(a, b) // expected-error {{enum case 'tuple' expects a single parameter of type '(T, T)'}} {{25-25=(}} {{29-29=)}}
1173+
_ = GenericEnum.tuple(a, b) // expected-error {{enum case 'tuple' expects a single parameter of type '(T, T)' [where T = Int]}} {{25-25=(}} {{29-29=)}}
11741174
_ = GenericEnum.tuple((a, b))
11751175
_ = GenericEnum.tuple(c)
11761176
}
@@ -1206,7 +1206,7 @@ do {
12061206
_ = GenericEnum.two((a, b)) // expected-error {{missing argument for parameter #2 in call}}
12071207
_ = GenericEnum.two(c) // expected-error {{missing argument for parameter #2 in call}}
12081208

1209-
_ = GenericEnum.tuple(a, b) // expected-error {{enum case 'tuple' expects a single parameter of type '(T, T)'}} {{25-25=(}} {{29-29=)}}
1209+
_ = GenericEnum.tuple(a, b) // expected-error {{enum case 'tuple' expects a single parameter of type '(T, T)' [where T = Int]}} {{25-25=(}} {{29-29=)}}
12101210
_ = GenericEnum.tuple((a, b))
12111211
_ = GenericEnum.tuple(c)
12121212
}

0 commit comments

Comments
 (0)