mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
In Swift 4, constructors had the same name as properties,
methods and enum cases named `init`. This meant that you
could use constructor syntax to call such a member, which
caused some confusing behavior.
Recently I added a special declname for `init` so that
constructors have unique names distinct from any name you
can spell, and "foo.init" syntax would look for a member
with the special name rather than one named `init`.
Unfortunately people actually had code where they defined
members named `init` and then use them via normal member
lookup syntax, like this:
enum E {
case `init`
}
let e: E = E.init
So to maintain backward compatibility, hack member lookup
to also find members named `init` when looking up the special
declname for constructors.
The workaround is only enabled in Swift 4 and 4.2 mode;
in Swift 5 mode you are expected to write "foo.`init`" to access
non-constructor members named `init`.
Fixes <rdar://problem/38682258>.
46 lines
770 B
Swift
46 lines
770 B
Swift
// RUN: %target-typecheck-verify-swift -swift-version 4
|
|
|
|
// https://bugs.swift.org/browse/SR-1660
|
|
|
|
enum DayOfTheWeek : Int {
|
|
case monday = 0
|
|
case `inout` = 1
|
|
case `init` = 2
|
|
case friday = 3
|
|
case tuesday = 4
|
|
}
|
|
|
|
let _: DayOfTheWeek = DayOfTheWeek.init
|
|
|
|
let _: DayOfTheWeek = DayOfTheWeek.`init`
|
|
|
|
func match(_ d: DayOfTheWeek) {
|
|
switch d {
|
|
case .monday: break
|
|
case .`inout`: break
|
|
case .`init`: break
|
|
case .friday: break
|
|
case .tuesday: break
|
|
}
|
|
}
|
|
|
|
enum Fox {
|
|
case `init`(Int)
|
|
|
|
init() {
|
|
self = .`init`(10)
|
|
}
|
|
}
|
|
|
|
let _: Fox = Fox(10)
|
|
// expected-error@-1 {{argument passed to call that takes no arguments}}
|
|
|
|
let _: () -> Fox = Fox.init
|
|
let _: (Int) -> Fox = Fox.`init`
|
|
|
|
func match(_ f: Fox) {
|
|
switch f {
|
|
case .`init`(let n): _ = n
|
|
}
|
|
}
|