swiftlang/swift

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

Open

#57,437 创建于 2021年8月25日

在 GitHub 查看
 (13 评论) (0 反应) (0 负责人)Swift (10,719 fork)batch import
bugcompilerdiagnostics qualityexpressionsgood first issueoptional chainingswift 5.9type checkertype inference

仓库指标

Star
 (69,989 star)
PR 合并指标
 (平均合并 8天 17小时) (30 天内合并 510 个 PR)

描述

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?'

贡献者指南