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-6272] Tailored diagnostics with fixits for numerical conversions #48822

Closed
jckarter opened this issue Nov 1, 2017 · 6 comments
Closed

[SR-6272] Tailored diagnostics with fixits for numerical conversions #48822

jckarter opened this issue Nov 1, 2017 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers

Comments

@jckarter
Copy link
Member

jckarter commented Nov 1, 2017

Previous ID SR-6272
Radar rdar://problem/22043114
Original Reporter @jckarter
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug
Assignee chuganzy (JIRA)
Priority Medium

md5: 5bec69d6c4f8dae3c2d65ea11480dead

Issue Description:

Conversions among numerical types, and things that are readily convertible to numbers such as enums, are frequently necessary, and we don’t give great feedback when conversions are missing or incorrect, leading to road rage:

https://twitter.com/gumbright/status/626190317025267713

It would be worthwhile to give the type checker special knowledge of the converting initializers and properties available in the standard library for the numeric types, and when a type mismatch is encountered, try to find a set of conversions that can make the system type-check, for things like mismatched integer types, float precision, int-to-float mixups, or enum-to-raw-value conversions.

@modocache
Copy link
Mannequin

modocache mannequin commented Nov 2, 2017

Maybe I'm searching for the wrong stuff, but grepping for the error message from the linked tweet, "could not find overload for '*' that accepts supplied arguments", doesn't turn up any hits (even subsets like just "overload" or "supplied" don't show anything seemingly related). I tried git log -S to search for whether these diagnostics were removed, but with limited success.

Using Swift 4.0 from Xcode 9, the following code:

enum Foo: Int {
    case bar
}

let result = Foo.bar.rawValue * CGFloat(0)

This yields this error: Binary operator '*' cannot be applied to operands of type 'Int' and 'CGFloat'. Am I correct in understanding that this task suggests a fix-it should be added that suggests adding CGFloat(Foo.bar.rawValue)?

@jckarter
Copy link
Member Author

jckarter commented Nov 2, 2017

Yeah, if there's an explicit numeric conversion that could make the system type-check, it'd be nice if we suggested it.

@swift-ci
Copy link
Collaborator

Comment by Matthew Cheok (JIRA)

I'd like to start contributing with this task but I've no idea where to start. Can I get some pointers?

@jckarter
Copy link
Member Author

Thanks matthewcheok (JIRA User)! I'm not the expert on the type checker (@DougGregor, @xedin, and @rudkx definitely know better than I do), but I think a good place to start might be to look at how we handle the fixits for missing bridging conversions when ObjC interop is enabled:

import Foundation

func foo(x: NSString) {}

func bar(x: String) { foo(x: x) } // the compiler sees the String-NSString mismatch here and suggests `as NSString`

@swift-ci
Copy link
Collaborator

Comment by Takeru Chuganji (JIRA)

PR merged: #13272

@jckarter
Copy link
Member Author

jckarter commented Feb 9, 2018

Awesome!

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants