Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-6211] type-inference fails through Optionals #48763

Open
weissi opened this issue Oct 24, 2017 · 2 comments
Open

[SR-6211] type-inference fails through Optionals #48763

weissi opened this issue Oct 24, 2017 · 2 comments
Assignees
Labels
compiler The Swift compiler in itself improvement type checker Area → compiler: Semantic analysis

Comments

@weissi
Copy link
Member

weissi commented Oct 24, 2017

Previous ID SR-6211
Radar None
Original Reporter @weissi
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, TypeChecker
Assignee @xedin
Priority Medium

md5: 8e8a1c55fe146af0611d8368222697ed

Issue Description:

this small example program

public struct Bar {
    public init?() {
        return nil
    }
}

let x: Bar = .init()!
let y: Bar? = .init()

should even with Swift's local-only type-inference be inferred I believe, however the type-checker fails on both x and y :'(

test2.swift:7:21: error: type of expression is ambiguous without more context
let x: Bar = .init()!
             ~~~~~~~^
test2.swift:8:16: error: value of optional type 'Bar' not unwrapped; did you mean to use '!' or '?'?
let y: Bar? = .init()
               ^
                     !

for

$ swiftc --version
Apple Swift version 4.0.1 (swiftlang-900.0.66 clang-900.0.37)
Target: x86_64-apple-macosx10.9

and

Apple Swift version 4.1-dev (LLVM f35a4270ce, Clang bb20a94e86, Swift e4ddd564b9)
Target: x86_64-apple-darwin17.2.0
@belkadan
Copy link
Contributor

Hm. Swift's inference says that the "return type" must exactly match the contextual type, so it's not surprising that this isn't supported. In particular, your second statement let y: Bar? = .init() could indicate an initializer defined on Optional, not on Bar. But we could consider adding something like this.

cc @xedin, @rudkx

@weissi
Copy link
Member Author

weissi commented Apr 20, 2018

same in swift 4.1 release

$ swiftc test.swift
test.swift:7:21: error: type of expression is ambiguous without more context
let x: Bar = .init()!
             ~~~~~~~^
test.swift:8:16: error: value of optional type 'Bar' not unwrapped; did you mean to use '!' or '?'?
let y: Bar? = .init()
               ^
                     !

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants