Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-10158] Associated type inference resolution ignores conditionality of extensions #52560

Closed
DevAndArtist mannequin opened this issue Mar 23, 2019 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@DevAndArtist
Copy link
Mannequin

DevAndArtist mannequin commented Mar 23, 2019

Previous ID SR-10158
Radar None
Original Reporter @DevAndArtist
Type Bug
Status Closed
Resolution Won't Do
Environment
  • Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)

  • Also seems to be present in Swift 5

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 516ea1ec593c1b61178384afe9c7df85

relates to:

  • SR-5440 Typealias in constrained extension misinterprets the where clause

Issue 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

@belkadan
Copy link
Contributor

I think this is correct behavior; otherwise we wouldn't be able to provide default implementations in the standard library that include a default associated type, and that's important for protocols that have associated types that are mostly just implementation details. cc @airspeedswift, @lorentey

@airspeedswift
Copy link
Member

Right – this is the correct current behavior and is used to provide functionality for types like Collection. Changes to it would need to be evolution pitches rather than jiras.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants