[SR-2176] Add warning for ambiguous enum value assignment Created: 26 Jul 2016  Updated: 8 Jan 2019  Resolved: 8 Jan 2019

Status: Resolved
Project: Swift
Component/s: Compiler

Type: Improvement Priority: Medium
Reporter: Kelan Champagne Assignee: Suyash Srijan
Resolution: Done Votes: 2
Labels: StarterBug

Xcode v8.0 beta 3 (8S174q)
Apple Swift version 3.0 (swiftlang-800.0.34.6 clang-800.0.33)
Target: x86_64-apple-macosx10.9


Xcode v7.3.1 (7D1014)
Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)
Target: x86_64-apple-macosx10.9

Issue Links:
is duplicated by SR-6044 We need an ambiguity warning for assi... Resolved
is duplicated by SR-8283 Enum cases named "none" can cause amb... Resolved
is duplicated by SR-8451 Swift's optional lifting/promotion is... Resolved
is duplicated by SR-8515 Compiler should show error for ambigu... Resolved
is duplicated by SR-3711 Make .member lookups in Optional<T> c... Closed
relates to SR-3711 Make .member lookups in Optional<T> c... Closed
Radar URL: rdar://problem/26126801


I've been caught off-guard by the value that's chosen by the compiler when inferring the type to assign to an enum value. This can be especially surprising because nil is shorthand for Optional.none.

For example, if I have this enum:

enum Coverage {
    case all      ///< everything is covered
    case partial  ///< only partially covered
    case none     ///< nothing is covered

let myCoverageA: Coverage? = .all
let myCoverageB: Coverage? = .none

Because myCoverageA gets a value of .some(.all), you might expect the value of myCoverageB to be .some(.none), but in fact it is .none.

I think this would also come up in a double-Optional. For example:

let str: String?? = nil
// is it `.none` or `.some(.none)`?


I think it would be nice to have a compiler warning in ambiguous cases like this.


I wrote up a slightly more detailed example of this here: http://kelan.io/2016/type-inferior-ence/

Comment by Jordan Rose [ 26 Jul 2016 ]

I'm pretty sure this can only happen with Optional, because we don't look through any other types in dot-shorthand syntax, but it still seems like a good idea to me.

Comment by Jordan Rose [ 3 Oct 2017 ]

Hey, Pavel Yaskevich, think this is simple enough for a starter bug? It's "just" post-processing of a type-checked expression, right?

Comment by Pavel Yaskevich [ 3 Oct 2017 ]

I think so, I can help out if anybody wants to take it too.

Comment by Kelan Champagne [ 4 Oct 2017 ]

I'd love to be able to take this., especially because I reported it.  But, it would be my very first Swift contribution, so I'd have a bit of a learning curve to climb.  Also, coincidentally, I just started at Apple (yesterday!), so I think there are some forms I need to submit, etc before I can actually post any PRs.

But, I'm really interested in learning how to do this, so if it's not urgent, I'd like to give it a shot.

Comment by Pavel Yaskevich [ 4 Oct 2017 ]

Kelan Champagne Sure, take your time! If you need any help please do let me know.

Comment by Andyy Hope [ 8 Jan 2018 ]

Hey Kelan Champagne - You still doing this? Sounds interesting


Comment by Kelan Champagne [ 9 Jan 2018 ]

Andyy Hope- I haven't actually started on anything for this yet.  I am still interested, but there is some process I have to get through first. So, if you're excited about it – go ahead.

If you're looking for StarterBugs, another one I had on my list to look at is SR-5982.  Although that one looks a little more involved (but probably also more interesting).

Comment by Suyash Srijan [ 3 Jan 2019 ]

I've created a PR for review: https://github.com/apple/swift/pull/21621

Comment by Suyash Srijan [ 8 Jan 2019 ]

Fixed on master. I'll cherry pick this to the 5.0 branch.

Generated at Wed Jan 23 09:17:00 CST 2019 using Jira 7.13.0#713000-sha1:fbf406879436de2f3fb1cfa09c7fa556fb79615a.