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-8075] Generic parameter type inference regression #50608

Closed
swift-ci opened this issue Jun 22, 2018 · 2 comments
Closed

[SR-8075] Generic parameter type inference regression #50608

swift-ci opened this issue Jun 22, 2018 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.2 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8075
Radar rdar://problem/41369682
Original Reporter rerelease (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 10 beta 2

Happens for both Swift 4.1.50 and 4.2

Worked OK for previous versions.

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

md5: deb426552504dfa59b34f4dc576ea953

Issue Description:

When using shorthand initializers (leading dot syntax) with a generic function to deduce the initializer parameter, the compiler can not deduce the type of the generic function.

Here's a reproducing Playground

import UIKit

func switchOnCategory<T>(_ categoryToValue: [Int: T]) -> T {
    return categoryToValue.first!.value
}

let label = UILabel()
label.font = .systemFont(ofSize: switchOnCategory([0: 15.5, 1: 20.5]))

Getting a `Generic parameter 'T' could not be inferred` compiler error on the last line. This works fine with Xcode 9 and Swift 4.1.

The issue can be worked around in two ways.

1) Explicitly state the init type:

label.font = UIFont.systemFont(...)

2) Explicitly specialize the parameter type:

label.font = .systemFont(ofSize: switchOnCategory([0: 15.5, 1: 20.5]) as CGFloat)

@belkadan
Copy link
Contributor

@swift-ci create

@huonw
Copy link
Mannequin

huonw mannequin commented Jun 27, 2018

Reduced:

struct UIFont {
    init(ofSize: Float) {}
}

func switchOnCategory<T>(_ categoryToValue: [Int: T]) -> T {
    fatalError()
}

let font: UIFont = .init(ofSize: switchOnCategory([0: 15.5, 1: 20.5]))

It is bad from the 2018-03-30 snapshot to the 2018-05-14 snapshot. It was good at 2018-03-28 (and before), and is good at 2018-05-17 (and after). It is specifically the ‘Float’ in the init, changing that to ‘Double’ infers fine. Based on that, I think it was fixed by #16590 and that was merged into the 4.2 branch (where it also works fine now) by #16621

#17552 adds a test.

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

No branches or pull requests

3 participants