Skip to content

Commit b214b8a

Browse files
committed
[Diagnostics] Introdduce multiple extraneous arguments diagnostic
1 parent e09e8c3 commit b214b8a

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,8 @@ ERROR(extra_argument_named,none,
11221122
"extra argument %0 in call", (Identifier))
11231123
ERROR(extra_argument_positional,none,
11241124
"extra argument in call", ())
1125+
ERROR(extra_arguments_in_call,none,
1126+
"extra arguments at positions %0 in call", (StringRef))
11251127
ERROR(extra_argument_to_nullary_call,none,
11261128
"argument passed to call that takes no arguments", ())
11271129
ERROR(extra_trailing_closure_in_call,none,

lib/Sema/CSDiagnostics.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4285,7 +4285,28 @@ bool ExtraneousArgumentsFailure::diagnoseAsError() {
42854285
}
42864286
}
42874287

4288-
return false;
4288+
if (ExtraArgs.size() < 2)
4289+
return false;
4290+
4291+
llvm::SmallString<64> positions;
4292+
llvm::raw_svector_ostream OS(positions);
4293+
4294+
interleave(
4295+
ExtraArgs,
4296+
[&](const std::pair<unsigned, AnyFunctionType::Param> &arg) {
4297+
OS << "#" << (arg.first + 1);
4298+
},
4299+
[&] { OS << ", "; });
4300+
4301+
emitDiagnostic(anchor->getLoc(), diag::extra_arguments_in_call, OS.str());
4302+
4303+
if (auto overload = getChoiceFor(getLocator())) {
4304+
if (auto *decl = overload->choice.getDeclOrNull()) {
4305+
emitDiagnostic(decl, diag::decl_declared_here, decl->getFullName());
4306+
}
4307+
}
4308+
4309+
return true;
42894310
}
42904311

42914312
bool ExtraneousArgumentsFailure::diagnoseAsNote() {

test/Constraints/keyword_arguments.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,22 +255,20 @@ missingargs2(x: 1, y: 2) // expected-error{{missing argument for parameter #3 in
255255
// -------------------------------------------
256256
// Extra arguments
257257
// -------------------------------------------
258-
// FIXME: Diagnostics could be improved with all extra arguments and
259-
// note pointing to the declaration being called.
260-
func extraargs1(x: Int) {}
258+
func extraargs1(x: Int) {} // expected-note {{'extraargs1(x:)' declared here}}
261259

262260
extraargs1(x: 1, y: 2) // expected-error{{extra argument 'y' in call}}
263-
extraargs1(x: 1, 2, 3) // expected-error{{extra argument in call}}
261+
extraargs1(x: 1, 2, 3) // expected-error{{extra arguments at positions #2, #3 in call}}
264262

265263
// -------------------------------------------
266264
// Argument name mismatch
267265
// -------------------------------------------
268266

269-
func mismatch1(thisFoo: Int = 0, bar: Int = 0, wibble: Int = 0) { }
267+
func mismatch1(thisFoo: Int = 0, bar: Int = 0, wibble: Int = 0) { } // expected-note {{'mismatch1(thisFoo:bar:wibble:)' declared here}}
270268

271269
mismatch1(foo: 5) // expected-error {{extra argument 'foo' in call}}
272270
mismatch1(baz: 1, wobble: 2) // expected-error{{incorrect argument labels in call (have 'baz:wobble:', expected 'bar:wibble:')}} {{11-14=bar}} {{19-25=wibble}}
273-
mismatch1(food: 1, zap: 2) // expected-error{{extra argument 'food' in call}}
271+
mismatch1(food: 1, zap: 2) // expected-error{{extra arguments at positions #1, #2 in call}}
274272

275273
// -------------------------------------------
276274
// Subscript keyword arguments

0 commit comments

Comments
 (0)