Skip to content
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

[SR-5695] Error defining closure with more than 3 parameters #48265

Closed
NachoSoto opened this issue Aug 16, 2017 · 4 comments
Closed

[SR-5695] Error defining closure with more than 3 parameters #48265

NachoSoto opened this issue Aug 16, 2017 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@NachoSoto
Copy link
Contributor

Previous ID SR-5695
Radar None
Original Reporter @NachoSoto
Type Bug
Status Resolved
Resolution Duplicate
Environment

Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42).

Apple Swift version 4.0 (swiftlang-900.0.43 clang-900.0.22.8)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 03635233dda6fdda8b87b899b7558577

duplicates:

  • SR-1528 Swift compiler requires closures without parameter lists to reference all arguments

Issue Description:

This works:

let a: [(Int, String, Int)] = []
a.filter { $2 == 0 }

But with more than 3 parameters, it fails with a bogus error:

let a: [(Int, String, Int, String)] = []
a.filter { $2 == 0 }
error: repl.swift:2:10: error: contextual closure type '((Int, String, Int, String)) -> Bool' expects 4 arguments, but 3 were used in closure body
a.filter { $2 == 0 }

Same thing happens with 5 or more:

let a: [(Int, String, Int, String, Int)] = []
a.filter { $2 == 0 }
error: repl.swift:7:10: error: contextual closure type '((Int, String, Int, String, Int)) -> Bool' expects 5 arguments, but 3 were used in closure body
a.filter { $2 == 0 }

Swift 4.0 beta 5 tuple deconstruction also only works up to 3 parameters, and fails with more:

let a: [(Int, String, Int)] = []
a.filter { $2 == 0 }
$R5: [(Int, String, Int)] = 0 values
let a: [(Int, String, Int, String, Int)] = []
a.filter { $2 == 0 }
error: repl.swift:12:10: error: contextual closure type '((Int, String, Int, String, Int)) -> Bool' expects 1 argument, but 3 were used in closure body
a.filter { $2 == 0 }
@slavapestov
Copy link
Member

This isn't about having more than four parameters. The issue is that the number of closure arguments is inferred from the number of the maximum anonymous parameter used, in this case $2 means 3 parameters. If you reference $3 somewhere, it will have 4 parameters.

@belkadan probably has an older dupe for this.

@NachoSoto
Copy link
Contributor Author

But referencing $1 when there's 3 works?

@slavapestov
Copy link
Member

That shouldn't work either.

@belkadan
Copy link
Contributor

Yeah, I'm going to say it's SR-1528. I think public opinion has settled on the "remove this diagnostic" but it hasn't risen to the top of anyone's list.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants