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-69] [QOI] Poor diagnostic with non-Range in for loop #42691

Closed
ddunbar opened this issue Dec 5, 2015 · 3 comments
Closed

[SR-69] [QOI] Poor diagnostic with non-Range in for loop #42691

ddunbar opened this issue Dec 5, 2015 · 3 comments
Assignees
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

Comments

@ddunbar
Copy link
Member

ddunbar commented Dec 5, 2015

Previous ID SR-69
Radar None
Original Reporter @ddunbar
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI
Assignee @gregomni
Priority Medium

md5: ec6e593e3561b9b848b6e71011de74ba

Issue Description:

Swift produces a poor diagnostic on this example:

$ cat t.swift
func foo() {
    for i in min(1,2) {
    }
}

$ swiftc -c t.swift
t.swift:2:14: error: cannot invoke 'min' with an argument list of type '(Int, Int)'
    for i in min(1,2) {
             ^
t.swift:2:14: note: expected an argument list of type '(T, T)'
    for i in min(1,2) {
             ^

It should tell me I forgot to make a range.

@ddunbar
Copy link
Member Author

ddunbar commented Dec 5, 2015

Somewhat more generally, it isn't that uncommon that Swift produces a diagnostic of the form "cannot do x with ... some types ..." ... expected an argument list of type ... some type args, which the previous statement obviously satisfies...".

Would it be possible for the type checker to simply detect when it is going to emit this kind of poor diagnostic, and emit something more verbose?

@lilyball
Copy link
Mannequin

lilyball mannequin commented Dec 5, 2015

I suspect what's going on here is the only way for min() to be valid there is if the type it returns is a SequenceType, which means that the min() call's generic parameter T is being given an implicit bound <T: SequenceType>. Given that bound. min(1, 2) is not valid, because Int does not conform to SequenceType. So the problem here isn't that the error is a poor error in general (it's not; it's perfectly legitimate in the cases where it's actually accurate), but that the diagnostic does not recognize that the failure is caused by an implicit bound that is not actually part of the min() call itself.

So basically, instead of determining that the arguments aren't valid because they don't satisfy the bound, it should determine that the arguments are valid for the expression in isolation, but that the resulting expression type doesn't satisfy the bound, and should therefore report the error as being that the expression's type Int does not conform to SequenceType (or that it has no member Generator as that seems to be what the error today is for for i in 1).

@gregomni
Copy link
Collaborator

This just got merged in e15d08e. The new error for this code should be: `type 'Int' does not conform to protocol 'SequenceType'`

@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
Projects
None yet
Development

No branches or pull requests

2 participants