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-4309] Type inferring fails in expression with ?? and + #46892

Closed
swift-ci opened this issue Mar 21, 2017 · 4 comments
Closed

[SR-4309] Type inferring fails in expression with ?? and + #46892

swift-ci opened this issue Mar 21, 2017 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-4309
Radar None
Original Reporter sulthan (JIRA User)
Type Bug
Status Closed
Resolution Duplicate
Environment

Xcode 8.2.1

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee sulthan (JIRA)
Priority Medium

md5: 1ded587e498b0edd7ce2ef020c1cb6c2

blocks:

  • SR-4304 Swift compiler unable to resolve correct overload of + operator for arrays in generic function

Issue Description:

See the following example of a rather simple class:

class A<T> {
    var value: T
    var left: A?
    var right: A?
    
    init(value: T) {
        self.value = value
    }
    
    func values() -> [T] {
        return (self.left?.values() ?? []) + [value] + (self.right?.values() ?? [])
    }
}

The expression:

(self.left?.values() ?? []) + [value] + (self.right?.values() ?? [])

fails to compile with Cannot convert value of type '[T]?' to expected argument of type '[_]'

The workarounds are obvious. Simplifying the expression using variables,adding additional parenthesis or adding a as [T]? cast.

An interesting case is when we remove the generic:

class A {
    var value: NSObject
    var left: A?
    var right: A?
    
    init(value: NSObject) {
        self.value = value
    }
    
    func values() -> [NSObject] {
        return (self.left?.values() ?? []) + [value] + (self.right?.values() ?? [])
    }
}

will fail with Ambigious reference to member '??'

Considering all the types are well defined, there should be no room for ambiguity.

@swift-ci
Copy link
Collaborator Author

Comment by Ondrej Hanslik (JIRA)

Might be related to SR-1122

@belkadan
Copy link
Contributor

Likely a dup of SR-4304, a much simpler case.

@rudkx
Copy link
Member

rudkx commented Mar 22, 2017

Yes, this does in fact look like a dup of the other issue.

@swift-ci
Copy link
Collaborator Author

Comment by Ondrej Hanslik (JIRA)

I guess this is a dup then.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants