Files
swift-mirror/test/Constraints/rdar45511837.swift
Pavel Yaskevich 0a8f596736 [Diagnostics] Port contextual mismatches involving nil to new framework
Detect and diagnose contextual failures originating in an attempt
to convert `nil` to some other non-optional type e.g.

```swift
let _: Int = nil // can't initialize `Int` with `nil`

func foo() -> Int {
  return nil // can't return `nil` from `foo`
}

_ = 1 + nil // there is no `+` overload which accepts `Int` and optional
```
2019-08-20 14:23:54 -07:00

25 lines
620 B
Swift

// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s
// REQUIRES: objc_interop
import Foundation
protocol A: RawRepresentable {}
extension A {
static func +(lhs: RawValue, rhs: Self) -> Self {
fatalError()
}
}
class Foo<Bar: NSObject> {
var foobar: Bar {
fatalError()
}
lazy var foo: () -> Void = {
// TODO: improve diagnostic message
_ = self.foobar + nil // expected-error {{'Bar' is not convertible to 'String'; did you mean to use 'as!' to force downcast?}}
// expected-error@-1 {{'nil' is not compatible with expected argument type 'String'}}
}
}