swiftlang/swift

[SR-15111] “Type of expression is ambiguous” error when initializing a class

Open

#57,437 opened on Aug 25, 2021

View on GitHub
 (13 comments) (0 reactions) (0 assignees)Swift (10,719 forks)batch import
bugcompilerdiagnostics qualityexpressionsgood first issueoptional chainingswift 5.9type checkertype inference

Repository metrics

Stars
 (69,989 stars)
PR merge metrics
 (Avg merge 8d 17h) (510 merged PRs in 30d)

Description

Previous ID SR-15111
Radar rdar://problem/82407752
Original Reporter elidy (JIRA User)
Type Bug

Xcode Version 13.0 beta 4 (13A5201i)

Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug, TypeChecker
Assignee elidy (JIRA)
Priority Medium

md5: 37c1160eb7b2424ea42b3ec849906fef

Issue Description:

When a class is initialized, passing an argument as a property of an optional class instance of incorrect type results in “Type of expression is ambiguous” error.

 class IntClass {
    var int: Int?
    
    init(int: Int?) {
        self.int = int
    }
}

class StringClass {
    var string: String? = "hello"
}

var stringClass: StringClass? = StringClass()

IntClass(int: stringClass?.string)
//Type of expression is ambiguous without more context
//Expected error: Cannot convert value of type 'String?' to expected argument type 'Int?'

var stringValue = stringClass?.string
IntClass(int: stringValue)
//Cannot convert value of type 'String?' to expected argument type 'Int?'

Contributor guide