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-7484] 4.1 regression: incorrect type inference on generic function #50027

Closed
ZevEisenberg opened this issue Apr 20, 2018 · 6 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.1 type checker Area → compiler: Semantic analysis

Comments

@ZevEisenberg
Copy link
Contributor

Previous ID SR-7484
Radar rdar://problem/39616039
Original Reporter @ZevEisenberg
Type Bug
Status Resolved
Resolution Done
Environment

Builds fine on:
Xcode 9.2 (9C40b) on macOS 10.13.4 (17E199)

Fails to build on:
Xcode 9.3 (9E145) on macOS 10.13.4 (17E199)

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, 4.1Regression, TypeChecker
Assignee @ZevEisenberg
Priority Medium

md5: fd6bbe9d3a3ec89dfc1919e459f3c63c

Issue Description:

This code compiles correctly in Xcode 9.2, but fails in Xcode 9.3 (i.e. Swift 4.1). I bet it could be simplified more, but this is a fairly reduced test case. The original use case in real, production code is here.

import Foundation

// This function has been simplified beyond recognition in service of
// making a small test case, but it has a similar type signature to
// a function in a real app that does real work.
func pick<ValueType>(default: ValueType, overrides: [String: ValueType]) -> ValueType {
    return overrides["foo"] ?? `default`
}

var inferredInt = pick(
    default: 74,
    overrides: ["foo": 24]
)
inferredInt += 1 // works as expected

var inferredDouble = pick(
    default: 74.0,
    overrides: ["foo": 24]
)
inferredDouble += 1 // In Swift 4.1: error: binary operator '+=' cannot be applied to operands of type 'Any' and 'Int'

var inferredCGFloat = pick(
    default: 74.0,
    overrides: ["foo": CGFloat(24)]
)
inferredCGFloat += 1 // In Swift 4.1: error: binary operator '+=' cannot be applied to operands of type 'Any' and 'Int'
@belkadan
Copy link
Contributor

cc @rudkx, @xedin. I'm not sure the new inference is wrong, but the old one is certainly more useful, and it is a behavior change.

@swift-ci
Copy link
Collaborator

Comment by Chris Ballinger (JIRA)

FWIW manually constraining to Numeric fixes it. Shouldn't it be able to infer Numeric?

// This function has been simplified beyond recognition in service of
 // making a small test case, but it has a similar type signature to
 // a function in a real app that does real work.
 func pick<ValueType: Numeric>(default: ValueType, overrides: [String: ValueType]) -> ValueType {
 return overrides["foo"] ?? `default`
 }

@xedin
Copy link
Member

xedin commented Apr 21, 2018

This is definitely a bug, since it looks like Double/CGFloat didn't even get attempted because of the binding ordering which changed.

@xedin
Copy link
Member

xedin commented Apr 21, 2018

@swift-ci create

@xedin
Copy link
Member

xedin commented Jul 2, 2018

The fix has been merged via #16590 please using one of the
nightly snapshots of 4.2 or master to verify and close.

@ZevEisenberg
Copy link
Contributor Author

Confirmed fixed in Xcode 10 beta 6

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

No branches or pull requests

5 participants