-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SE-0284] Allow Multiple Variadic Parameters in Functions, Subscripts, and Initializers #29735
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// RUN: %target-run-simple-swift | %FileCheck %s | ||
|
||
// REQUIRES: executable_test | ||
|
||
func vf(x: Int..., y: Int...) { | ||
print(x, y) | ||
} | ||
|
||
vf(x: 1, 2, 3, y: 4, 5, 6) | ||
// CHECK: [1, 2, 3] [4, 5, 6] | ||
vf(y: 1, 2) | ||
// CHECK: [] [1, 2] | ||
vf(x: 3, 4) | ||
// CHECK: [3, 4] [] | ||
|
||
func vf2(_ x: Int..., y: Int, _ z: Int...) { | ||
print(x, y, z) | ||
} | ||
|
||
vf2(1, 2, 3, y: 4, 5, 6, 7) | ||
// CHECK: [1, 2, 3] 4 [5, 6, 7] | ||
vf2(y: 4, 5, 6, 7) | ||
// CHECK: [] 4 [5, 6, 7] | ||
vf2(1, 2, 3, y: 4) | ||
// CHECK: [1, 2, 3] 4 [] | ||
vf2(y: 4) | ||
// CHECK: [] 4 [] | ||
|
||
func vf3(_ x: Int..., y: Int = 42, _ z: Int...) { | ||
print(x, y, z) | ||
} | ||
|
||
vf3(1, 2, 3, y: 4, 5, 6, 7) | ||
// CHECK: [1, 2, 3] 4 [5, 6, 7] | ||
vf3(y: 4, 5, 6, 7) | ||
// CHECK: [] 4 [5, 6, 7] | ||
vf3(1, 2, 3, y: 4) | ||
// CHECK: [1, 2, 3] 4 [] | ||
vf3(y: 4) | ||
// CHECK: [] 4 [] | ||
|
||
vf3() | ||
// CHECK: [] 42 [] | ||
vf3(1, 2, 3) | ||
// CHECK: [1, 2, 3] 42 [] | ||
|
||
func foo(a: Int..., b: Int, c: Int..., d: Int) { | ||
print("one") | ||
} | ||
|
||
func foo(a: [Int], b: Int, c: [Int], d: Int) { | ||
print("two") | ||
} | ||
|
||
func foo(a: Int..., b: Int, c: [Int], d: Int) { | ||
print("three") | ||
} | ||
|
||
foo(a: 1, 2, 3, b: 4, c: 5, 6, 7, d: 8) | ||
// CHECK: one | ||
foo(a: [1, 2, 3], b: 4, c: [5, 6, 7], d: 8) | ||
// CHECK: two | ||
foo(a: 1, 2, 3, b: 4, c: [5, 6, 7], d: 8) | ||
// CHECK: three | ||
|
||
struct Baz { | ||
init(a: Int..., b: Int...) { | ||
print(a, b) | ||
} | ||
|
||
init(_ a: Int..., b: String, _ c: Int...) { | ||
print(a, b, c) | ||
} | ||
|
||
subscript(a: Int..., b b: Int...) -> [Int] { a + b } | ||
} | ||
|
||
let baz1 = Baz(a: 1, 2, 3, b: 4, 5, 6) | ||
// CHECK: [1, 2, 3] [4, 5, 6] | ||
|
||
let baz2 = Baz(1, 2, 3, b: "hello, world!", 3, 2, 1) | ||
// CHECK: [1, 2, 3] hello, world! [3, 2, 1] | ||
|
||
print(baz1[1, 2, b: 3, 4]) | ||
// CHECK: [1, 2, 3, 4] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,38 @@ func invalidVariadic(_ e: NonExistentType) { // expected-error {{cannot find typ | |
{ (e: ExtraCrispy...) in }() // expected-error {{cannot find type 'ExtraCrispy' in scope}} | ||
} | ||
|
||
func twoVariadics(_ a: Int..., b: Int...) { } // expected-error{{only a single variadic parameter '...' is permitted}} {{38-41=}} | ||
func twoVariadics(_ a: Int..., b: Int...) { } | ||
func unlabeledFollowingVariadic(_ a: Int..., _ b: Int) { } // expected-error {{a parameter following a variadic parameter requires a label}} | ||
func unlabeledVariadicFollowingVariadic(_ a: Int..., _ b: Int...) { } // expected-error {{a parameter following a variadic parameter requires a label}} | ||
func unlabeledFollowingTwoVariadics(_ a: Int..., b: Int..., _ c: Int) { } // expected-error {{a parameter following a variadic parameter requires a label}} | ||
func splitVariadics(_ a: Int..., b: Int, _ c: String...) { } | ||
func splitByDefaultArgVariadics(_ a: Int..., b: Int = 0, _ c: String...) { } | ||
|
||
struct HasSubscripts { | ||
subscript(a: Int...) -> Void { () } | ||
subscript(a: Int..., b b: Int...) -> Void { () } | ||
subscript(a: Int..., b: Int...) -> Void { () } // expected-error {{a parameter following a variadic parameter requires a label}} | ||
subscript(a: Int..., b: Int) -> Void { () } // expected-error {{a parameter following a variadic parameter requires a label}} | ||
subscript(a: Int..., b b: Int..., c c: Int) -> Void { () } | ||
subscript(a: Int..., b b: Int..., c: Int) -> Void { () } // expected-error {{a parameter following a variadic parameter requires a label}} | ||
subscript(a: Int..., c c: Int = 0, b: Int...) -> Void { () } | ||
subscript(a: Int..., b: String = "hello, world!") -> Bool { false } // expected-error {{a parameter following a variadic parameter requires a label}} | ||
} | ||
|
||
struct HasInitializers { | ||
init(a: Int...) {} | ||
init(a: Int..., b: Int...) {} | ||
init(a: Int..., _ b: Int...) {} // expected-error {{a parameter following a variadic parameter requires a label}} | ||
init(a: Int..., c: Int = 0, _ b: Int...) {} | ||
} | ||
|
||
let closure = {(x: Int..., y: Int...) in } // expected-error {{no parameters may follow a variadic parameter in a closure}} | ||
let closure2 = {(x: Int..., y: Int) in } // expected-error {{no parameters may follow a variadic parameter in a closure}} | ||
let closure3 = {(x: Int..., y: Int, z: Int...) in } // expected-error {{no parameters may follow a variadic parameter in a closure}} | ||
let closure4 = {(x: Int...) in } | ||
let closure5 = {(x: Int, y: Int...) in } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess It would be great if we can improve this. (not in this PR) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a test case for this, right now both diagnostics are emitted. |
||
let closure6 = {(x: Int..., y z: Int) in } // expected-error {{closure cannot have keyword arguments}} | ||
// expected-error@-1 {{no parameters may follow a variadic parameter in a closure}} | ||
|
||
// rdar://22056861 | ||
func f5(_ list: Any..., end: String = "") {} | ||
|
Uh oh!
There was an error while loading. Please reload this page.