Skip to content

[Sema] Correctly diagnose passing tuple as only argument to function with multiple parameters #36499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1847,28 +1847,45 @@ bool swift::diagnoseArgumentLabelError(ASTContext &ctx,
llvm::SmallString<16> missingBuffer;
llvm::SmallString<16> extraBuffer;
for (unsigned i = 0; i != n; ++i) {
Identifier oldName;
// oldName and newName are
// - None if i is out of bounds for the argument list
// - nullptr for an argument without a label
// - have a value if the argument has a label
Optional<Identifier> oldName;
if (i < argList.args.size())
oldName = argList.labels[i];
Identifier newName;
Optional<Identifier> newName;
if (i < newNames.size())
newName = newNames[i];

assert(oldName || newName && "We can't have oldName and newName out of "
"bounds, otherwise n would be smaller");

if (oldName == newName ||
(argList.hasTrailingClosure && i == argList.args.size()-1 &&
(argList.hasTrailingClosure && i == argList.args.size() - 1 &&
(numMissing > 0 || numExtra > 0 || numWrong > 0)))
continue;

if (oldName.empty()) {
if (!oldName.hasValue() && newName.hasValue()) {
++numMissing;
missingBuffer += newName->str();
missingBuffer += ':';
} else if (oldName.hasValue() && !newName.hasValue()) {
++numExtra;
extraBuffer += oldName->str();
extraBuffer += ':';
} else if (oldName->empty()) {
// In the cases from here onwards oldValue and newValue are not null
++numMissing;
missingBuffer += newName.str();
missingBuffer += newName->str();
missingBuffer += ":";
} else if (newName.empty()) {
} else if (newName->empty()) {
++numExtra;
extraBuffer += oldName.str();
extraBuffer += oldName->str();
extraBuffer += ':';
} else
} else {
++numWrong;
}
}

// Emit the diagnostic.
Expand Down
6 changes: 3 additions & 3 deletions test/Constraints/argument_matching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ func testUnlabeledParameterBindingPosition() {
func f(aa: Int, bb: Int, _ cc: Int) {}

f(0, 2)
// expected-error@-1:6 {{missing argument labels 'aa:bb:' in call}}
// expected-error@-1:6 {{missing argument labels 'aa:bb::' in call}}
// expected-error@-2:8 {{missing argument for parameter 'bb' in call}}

f(0, bb: 1, 2)
Expand Down Expand Up @@ -1191,7 +1191,7 @@ func testUnlabeledParameterBindingPosition() {
// expected-error@-1:6 {{missing arguments for parameters 'aa', 'cc' in call}}

f(1, 2, 3)
// expected-error@-1 {{missing argument labels 'aa:cc:' in call}}
// expected-error@-1 {{missing argument labels 'aa:cc::' in call}}
// expected-error@-2:11 {{missing argument for parameter 'cc' in call}}

f(1, 2, 3, 4)
Expand Down Expand Up @@ -1223,7 +1223,7 @@ func testUnlabeledParameterBindingPosition() {
// expected-error@-1:12 {{missing argument for parameter 'bb' in call}}

f(aa: 1, xx: 2, 3)
// expected-error@-1 {{incorrect argument label in call (have 'aa:xx:_:', expected 'aa:bb:_:_:')}}
// expected-error@-1 {{incorrect argument labels in call (have 'aa:xx:_:', expected 'aa:bb:_:_:')}}
// expected-error@-2:12 {{missing argument for parameter 'bb' in call}}

f(aa: 1, bb: 2, 3, xx: 4)
Expand Down
11 changes: 11 additions & 0 deletions test/Sema/call_function_with_tuple.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-typecheck-verify-swift

func twoArgs(_ a: Int, _ b: Int) -> Void { //expected-note{{'twoArgs' declared here}}
}

func call() {
// Call a function that takes two unnamed arguments with a tuple that has two
// named arguments
let namedTuple: (x: Int, y: Int) = (1, 1)
twoArgs(namedTuple) // expected-error{{global function 'twoArgs' expects 2 separate arguments}} expected-error{{missing argument label ':' in call}}
}