swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.41k stars 10.34k forks source link

[SR-14844] Static member func Set<X> parameter sometimes cannot convert from array literal #57191

Open mattyoung opened 3 years ago

mattyoung commented 3 years ago
Previous ID SR-14844
Radar rdar://problem/79921692
Original Reporter @mattyoung
Type Bug
Environment Xcode Version 13.0 beta (13A5155e)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 981f73b357d0583a02a72282ffe203c2

Issue Description:

import Foundation
import SwiftUI

// `fields` parameter is correctly converted from array literal to Set<Field>
let _ :Date.ComponentsFormatStyle  = .components(style: .abbreviated, fields: [.minute])
//                                                                            ^^^^^^^^^
let range = Date.now ..< Date.now
#warning("`fields` do not compile from array literal")
range.formatted(.components(style: .abbreviated, fields: [.minute]))
//                                                       ^^^^^^^^^
// compile error:
// Cannot convert value of type '[Any]' to expected argument type 'Set<Date.ComponentsFormatStyle.Field>?'
// Reference to member 'minute' cannot be resolved without a contextual type

// fields: add explicit .init then compile, should not need to add .init()
range.formatted(.components(style: .abbreviated, fields: .init([.minute])))
//                                                       ^^^^^

// same problem with SwiftUI Text:
// this do not compile:
Text(range, format: .components(style: .abbreviated, fields: [.minute]))
// this is fine:
Text(range, format: .components(style: .abbreviated, fields: .init([.minute])))

// this do not compile:
Text("\(range, format: .components(style: .abbreviated, fields: [.minute]))")
// this is fine:
Text("\(range, format: .components(style: .abbreviated, fields: .init([.minute])))")
typesanitizer commented 3 years ago

The range.formatted example works on main, so maybe that fix should be cherry-picked to 5.5. The others don't work, and I even see odd diagnostics such as error: incorrect argument label in call (have ':format:', expected ':formatter:').

@swift-ci create