-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Open
Labels
OptionalArea → standard library: The `Optional` typeArea → standard library: The `Optional` typeSILGenArea → compiler: The SIL generation stageArea → compiler: The SIL generation stagebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselfconformancesFeature → protocol: protocol conformancesFeature → protocol: protocol conformancesinitFeature → declarations: InitializersFeature → declarations: Initializersswift 6.0unexpected behaviorBug: Unexpected behavior or incorrect outputBug: Unexpected behavior or incorrect output
Description
Description
I think this must be a bug. The following test passes if the Optional
initializer is declared as a failable initializer, but fails if the ?
is removed from the Optional
initializer.
The Test
@Test
func foo() {
#expect(
Demo<Optional<Int>>.test()
)
}
struct Demo<T: Foo> {
static func test() -> Bool {
if let a = T.init() {
return true
}
return false
}
}
protocol Foo {
init?()
}
Passes If Optional Conforms Like This:
extension Optional: Foo {
init?() {
self = Self.none
}
}
Fails If Optional Conforms Like This:
extension Optional: Foo {
init() {
self = Self.none
}
}
Original Swift Forums Post: https://forums.swift.org/t/apparent-compiler-bug-relating-to-optional-witnessing-failable-initializer-requirement/76759
Reproduction
@Test
func foo() {
#expect(
Demo<Optional<Int>>.test()
)
}
struct Demo<T: Foo> {
static func test() -> Bool {
if let a = T.init() {
return true
}
return false
}
}
protocol Foo {
init?()
}
extension Optional: Foo {
init() {
self = Self.none
}
}
Expected behavior
I expected that the unwrapping if let a = T.init() { }
would succeed since in this case T == Optional<Int>
and Optional
unconditionally succeeds in initializing itself to .none
.
Environment
Swift 6
Additional information
No response
Metadata
Metadata
Assignees
Labels
OptionalArea → standard library: The `Optional` typeArea → standard library: The `Optional` typeSILGenArea → compiler: The SIL generation stageArea → compiler: The SIL generation stagebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfThe Swift compiler itselfconformancesFeature → protocol: protocol conformancesFeature → protocol: protocol conformancesinitFeature → declarations: InitializersFeature → declarations: Initializersswift 6.0unexpected behaviorBug: Unexpected behavior or incorrect outputBug: Unexpected behavior or incorrect output