swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.61k stars 10.37k forks source link

[SR-3922] Type inference bug #46507

Open 05109ee7-7cd9-4cd4-92d0-698e676fc6af opened 7 years ago

05109ee7-7cd9-4cd4-92d0-698e676fc6af commented 7 years ago
Previous ID SR-3922
Radar None
Original Reporter @an0
Type Bug
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, TypeChecker | |Assignee | None | |Priority | Medium | md5: 3686f0973a3fe6a58b141a14d72ecff4

Issue Description:

Type inference fails to resolve the type of this expression even though the result type is given explicitly:
let result: [supertype] = [subtype1] + [subtype2]

class A {}
class B: A {}
class C: A {}

let b = [B()]
let c = [C()]
let aa: [A] = b + c // Error: type of expression is ambiguous without more context
CodaFi commented 7 years ago

I don't think this is a bug with the way type inference is performed, I think the diagnostic is the real issue here. Individually, [B] and [C] can be coerced to [A], but + expects homogeneous arguments which means the system is truly ambiguous. If you want to work around this, you should coerce each argument:

let aa = (b as [A]) + (c as [A])