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

Associated type inference resolution ignores conditionality of extensions

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Won't Do
    • Component/s: Compiler
    • Labels:
      None
    • Environment:
      • Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)
      • Also seems to be present in Swift 5

      Description

      This is a magical behavior, and it should be disallowed as it's a source of potential ambiguity issues in a more complex project.

      protocol Test {
        associatedtype Something
        var something: Something { get }
      }
      
      // This is a 'conditional' extension which should not be picked
      // by the compiler to satisfy protocol conformance automatically
      // when the conforming type does not provide any requirements
      // until the explicitly stated that `Something` should be of type `Int`
      extension Test where Something == Int {
        var something: Something {
          return 42
        }
      }
      
      struct S: Test {} // THIS SHOULD ERROR OUT
      
      print(S().something) // prints 42
      

      Similar example where the default associated type is simply overriden/ignored:

      protocol Test {
        associatedtype Something = String
        var something: Something { get }
      }
      
      extension Test where Something == Int {
        var something: Something {
         return 42
        }
      }
      
      struct S: Test {}
      
      print(S().something) // again an Int and not String
      

      Discussion on the forums: https://forums.swift.org/t/what-kind-of-magic-behavior-is-this/22014

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                DevAndArtist Adrian Zubarev
              • Votes:
                1 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: