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-922] Inconsistent handling of labeled and unlabeled tuples #43534

Closed
natecook1000 opened this issue Mar 11, 2016 · 5 comments
Closed

[SR-922] Inconsistent handling of labeled and unlabeled tuples #43534

natecook1000 opened this issue Mar 11, 2016 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@natecook1000
Copy link
Member

Previous ID SR-922
Radar None
Original Reporter @natecook1000
Type Bug
Status Resolved
Resolution Won't Do
Environment

Latest Swift version 3.0-dev

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

md5: 522299af37596ed9074109d223562b2e

Issue Description:

Swift 3's type checker sometimes doesn't match tuples that have the right type but are missing labels for the values. This seems like a regression from Swift 2.2:

struct MyDictionary<Key: Hashable, Value> { 
  typealias KeyValueTuple = (key: Key, value: Value) 
   
#if swift(>=3.0)
  func foo<S: Sequence where S.Iterator.Element == KeyValueTuple>(s: S) {
    print(s) 
  } 
#else
  func foo<S: SequenceType where S.Generator.Element == KeyValueTuple>(s: S) {
    print(s) 
  } 
#endif
} 

let labeled = (key: "a", value: 1)
let unlabeled = ("a", 1)

let dictionary = MyDictionary<String, Int>()

// These work in Swift 2.2 and 3.0
dictionary.foo([(key: "a", value: 1)])
dictionary.foo([("a", 1)])
dictionary.foo([labeled])

// This fails in Swift 3.0
dictionary.foo([unlabeled])
@belkadan
Copy link
Contributor

cc cwillmore (JIRA User)

@swift-ci
Copy link
Collaborator

Comment by Dan Appel (JIRA)

This issue requires code to have explicitly labeled tuples all around. For example, the following code (in Swift 2)

extension CollectionType where Generator.Element == (String, Int) {}

Would match both a dictionary `[String:Int]` and an array of tuples `[(String, Int)]` (which is essentially a dictionary).

However, in Swift 3, the tuples are required to be labeled, otherwise the extensions don't apply.

extension Collection where Iterator.Element == (key: String, value: Int) {}

Which does match dictionaries `[String:Int]` and arrays `[(key: String, value: Int)]` but not the arrays without the labels `[(String, Int)]`.

As you can imagine, this is a major inconvenience and seems to give labeled tuples a separate type, even though in both cases their values can still be accessed with `tuple.0` and `tuple.1`.

@slavapestov
Copy link
Member

This is working as intended – labeled and unlabeled tuple types are different for purposes of same-type constraints, even though they can convert to each other in expressions.

@swift-ci
Copy link
Collaborator

swift-ci commented Jun 3, 2017

Comment by Tim (JIRA)

FWIW, this issue is causing the new Swift 4 methods Dictionary.merge and Dictionary.merging to not accept other dictionaries.

@rudkx
Copy link
Member

rudkx commented Jun 3, 2017

The issues with Dictionary.merge and Dictionary.merging are covered in https://bugs.swift.org/browse/SR-4969.

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants