Files
swift-mirror/test/expr/postfix/dot/optional_context_member.swift
Slava Pestov d843b10bcc Sema: Fix crash with unresolved dot expression with optional protocol metatype base
Mostly fixes <rdar://problem/35945827>, but there is a case that should
work that does not type check. At least we don't crash though.
2018-01-05 20:33:12 -08:00

91 lines
3.0 KiB
Swift

// RUN: %target-typecheck-verify-swift
struct Foo {
static var someVar: Foo = Foo()
static var someOptVar: Foo? = Foo()
static func someFunc() -> Foo {}
static func someOptFunc() -> Foo? {}
}
func nonOptContext() -> Foo {
switch () {
case ():
return .someVar
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .someOptVar // expected-error 2 {{value of optional type 'Foo' not unwrapped; did you mean to use '!' or '?'?}} {{23-23=!}}
// TODO
//case ():
// return .someOptVar!
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .someFunc()
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .someOptFunc() // expected-error{{}} {{26-26=!}}
// TODO
//case ():
// return .someOptFunc()!
}
}
func optContext() -> Foo? {
switch () {
case ():
return .someVar
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .someOptVar
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .someFunc()
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .someOptFunc()
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .some(.someVar)
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .none
}
}
func iuoContext() -> Foo! {
switch () {
case ():
return .someVar
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .someOptVar
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .someFunc()
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .someOptFunc()
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .some(.someVar)
case (): // expected-warning {{case is already handled by previous patterns; consider removing it}}
return .none
}
}
// Favor the outermost type if the member appears at multiple levels of
// unwrapping.
func nestedOptContext() -> Foo?? {
return .none
}
// <rdar://problem/35945827>
// This should diagnose instead of crashing in SILGen
protocol Horse {
static var palomino: Horse { get }
}
func rideAHorse(_ horse: Horse?) {}
rideAHorse(.palomino)
// expected-error@-1 {{static member 'palomino' cannot be used on protocol metatype 'Horse.Protocol'}}
// FIXME: This should work if the static member is part of a class though
class Donkey {
static var mule: Donkey & Horse { while true {} }
}
func rideAMule(_ mule: (Horse & Donkey)?) {}
rideAMule(.mule)
// expected-error@-1 {{static member 'mule' cannot be used on protocol metatype '(Donkey & Horse).Protocol'}}