Skip to content

[SR-2176] Add warning for ambiguous enum value assignment #44784

@swift-ci

Description

@swift-ci
Previous ID SR-2176
Radar rdar://problem/26126801
Original Reporter kelan (JIRA User)
Type Improvement
Status Closed
Resolution Done
Environment

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

AND

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

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Improvement, StarterBug
Assignee @theblixguy
Priority Medium

md5: 1e3b35e57d95ed4fe6d3877bd98474f4

is duplicated by:

  • SR-6044 We need an ambiguity warning for assigning .none to an optional enum where .none is a valid case.
  • SR-8283 Enum cases named "none" can cause ambiguity with Optional's "none"
  • SR-8451 Swift's optional lifting/promotion is overzealous
  • SR-10659 Append key problem on Dictionary with enum on specific scenario
  • SR-10705 Enum Type Inference produces unexpected result with optional
  • SR-3711 Make .member lookups in Optional context ambiguous if member can be found in both Optional and T
  • SR-8515 Compiler should show error for ambiguous enum cases with implicit casting

relates to:

  • SR-3711 Make .member lookups in Optional context ambiguous if member can be found in both Optional and T

Issue Description:

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)`?

Expect

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

Notes

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

Metadata

Metadata

Assignees

Labels

OptionalArea → standard library: The `Optional` typecompilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of Implementationenum casesFeature → enums: Enum casesgood first issueGood for newcomersimprovementtype checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions