Skip to content

Commit 001e7bb

Browse files
authored
Merge pull request swiftlang#34246 from slavapestov/local-function-overloading-now-works
Add test case and changelog entry for local function overloading
2 parents 658fca1 + 9bcec37 commit 001e7bb

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ CHANGELOG
2727
Swift Next
2828
----------
2929

30+
* [SR-10069][]:
31+
32+
Function overloading now works in local contexts, making the following valid:
33+
34+
```swift
35+
func outer(x: Int, y: String) {
36+
func doIt(_: Int) {}
37+
func doIt(_: String) {}
38+
39+
doIt(x) // calls the first 'doIt(_:)' with an Int value
40+
doIt(y) // calls the second 'doIt(_:)' with a String value
41+
}
42+
```
43+
3044
* [SE-0284][]:
3145

3246
Functions, subscripts, and initializers may now have more than one variadic parameter, as long as all parameters which follow variadic parameters are labeled. This makes declarations like the following valid:
@@ -8196,6 +8210,7 @@ Swift 1.0
81968210
[SR-8974]: <https://bugs.swift.org/browse/SR-8974>
81978211
[SR-9043]: <https://bugs.swift.org/browse/SR-9043>
81988212
[SR-9827]: <https://bugs.swift.org/browse/SR-9827>
8213+
[SR-10069]: <https://bugs.swift.org/browse/SR-10069>
81998214
[SR-11298]: <https://bugs.swift.org/browse/SR-11298>
82008215
[SR-11429]: <https://bugs.swift.org/browse/SR-11429>
82018216
[SR-11700]: <https://bugs.swift.org/browse/SR-11700>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// RUN: %target-typecheck-verify-swift -disable-parser-lookup
2+
3+
func valid1() {
4+
func inner(_: Int) {}
5+
func inner(_: String) {}
6+
7+
func inner(label: Int) {}
8+
func inner(label: String) {}
9+
10+
inner(123)
11+
inner("hello")
12+
13+
inner(label: 123)
14+
inner(label: "hello")
15+
}
16+
17+
func valid2() {
18+
func inner(_: Int = 0) {}
19+
func inner() -> Bool {}
20+
func inner(first: Int, second: Int = 0) {}
21+
22+
let _: Bool = inner()
23+
let _ = inner()
24+
25+
inner(first: 123)
26+
}
27+
28+
func invalid1() {
29+
func inner(_: Int) {}
30+
// expected-note@-1 {{'inner' previously declared here}}
31+
func inner(_: Int) {}
32+
// expected-error@-1 {{invalid redeclaration of 'inner'}}
33+
}
34+
35+
func invalid2() {
36+
func inner(_: Int) {}
37+
// expected-note@-1 {{candidate expects value of type 'Int' for parameter #1}}
38+
// expected-note@-2 {{found this candidate}}
39+
// expected-note@-3 {{did you mean 'inner'?}}
40+
func inner(_: String) {}
41+
// expected-note@-1 {{candidate expects value of type 'String' for parameter #1}}
42+
// expected-note@-2 {{found this candidate}}
43+
// expected-note@-3 {{did you mean 'inner'?}}
44+
45+
func inner(label: Int) {}
46+
// expected-note@-1 {{found this candidate}}
47+
// expected-note@-2 {{did you mean 'inner'?}}
48+
49+
inner([])
50+
// expected-error@-1 {{no exact matches in call to local function 'inner'}}
51+
52+
inner(label: "hi")
53+
// expected-error@-1 {{cannot convert value of type 'String' to expected argument type 'Int'}}
54+
55+
_ = inner
56+
// expected-error@-1 {{ambiguous use of 'inner'}}
57+
58+
_ = inner(label:) // no-error
59+
60+
// FIXME: This isn't as good as in the non-local function case?
61+
_ = inner(invalidLabel:)
62+
// expected-error@-1 {{cannot find 'inner(invalidLabel:)' in scope}}
63+
}

0 commit comments

Comments
 (0)