Uploaded image for project: 'Swift'
  1. Swift
  2. SR-6899

Codable does not derive correctly for enums backed by non basic raw types

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
    • Environment:

      MacOS Sierra 10.12.6 (16G29)

      Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
      Target: x86_64-apple-macosx10.9

      Description

      The following code does not compile

       

      import Foundation
      
      public struct Bar: Codable {
          
          let value: String
          
          public init(bar: Bar) {
              
              self.value = bar.value
          }
      }
      
      extension Bar: ExpressibleByStringLiteral {
          
          public typealias StringLiteralType = String
          
          public init(unicodeScalarLiteral value: UnicodeScalar) {
              
              self.value = "\(value)"
          }
          
          public init(extendedGraphemeClusterLiteral value: String) {
              
              self.value = value
          }
          
          public init(stringLiteral value: String) {
              
              self.value = value
          }
      }
      
      extension Bar: Equatable { }
      
      public func ==(lhs: Bar, rhs: Bar) -> Bool {
          
          return lhs.value.caseInsensitiveCompare(rhs.value) == ComparisonResult.orderedSame
      }
      
      public enum Foo: Bar, Codable {
          
          case foor = "Foo"
      }
      

      Generating 2 errors:

       

       

      error: type 'Foo' does not conform to protocol 'Decodable'
      public enum Foo: Bar, Codable {
                  ^
      error: type 'Foo' does not conform to protocol 'Encodable'
      public enum Foo: Bar, Codable {

      Change the RawValue of `Foo` to String, and the problem goes away.

      Expected Result:
      Codable is derived entirely.

      Discovered along side with: https://bugs.swift.org/browse/SR-6897

       

       

        Attachments

          Activity

            People

            • Assignee:
              itaiferber Itai Ferber
              Reporter:
              tmpz Tommaso Piazza
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: