Skip to content

[SR-1594] Provide a better diagnostic for nil comparisons. #2755

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

Merged
merged 1 commit into from
Jun 1, 2016

Conversation

CodaFi
Copy link
Contributor

@CodaFi CodaFi commented May 27, 2016

What's in this pull request?

Comparisons to nil are already suspect, but the diagnostic about it applied only to types with reference semantics, which Int? in the linked SR most certainly does not have. This patch differentiates the error for types with reference and value semantics.

Resolved bug number: (SR-1594)


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

@lattner
Copy link
Contributor

lattner commented May 28, 2016

Looks good. You might also consider the case of the UnsafePointer family. Please include a test case that shows your new diagnostic. Thanks!

@CodaFi
Copy link
Contributor Author

CodaFi commented May 28, 2016

Good idea. We can provide a fixit too.

@CodaFi CodaFi force-pushed the triple-value-meal branch from 4000daa to abf5bf4 Compare May 29, 2016 00:59
@CodaFi
Copy link
Contributor Author

CodaFi commented May 29, 2016

@lattner How does it look now?

@CodaFi CodaFi changed the title Provide a better diagnostic for nil comparisons. [SR-1594] Provide a better diagnostic for nil comparisons. May 29, 2016
@CodaFi CodaFi force-pushed the triple-value-meal branch 2 times, most recently from 4000daa to abf5bf4 Compare May 30, 2016 02:52
class SR1594 {
func sr1594(bytes : UnsafeMutablePointer<Int>, _ i : Int?) {
_ = (i === nil) // expected-error {{value of type 'Int?' does not have reference semantics, comparison isn't allowed}}
_ = (bytes === nil) // expected-error {{pointer value of type 'UnsafeMutablePointer<Int>' cannot be compared by reference; did you mean to compare by value?}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be checking the fixit for replacing the operator here too {{16-19===}}

@gregomni
Copy link
Contributor

@CodaFi I think the old existing diagnostic value of type %0 can never be nil, comparison isn't allowed worked fine, and is more straightforward than mentioning reference semantics, but that is a matter of taste.

I'd like to see a fixit for the Int? === nil case as well. I'd suggest taking the word "pointer" out of the description of that error, moving it to be the first clause and checking for getAnyPointerElementType() || getAnyOptionalObjectType().

Finally, you probably shouldn't .highlight() where you are also doing a .fixIt(). The fixit will already be highlighting the replacement range.

@CodaFi CodaFi force-pushed the triple-value-meal branch from abf5bf4 to 4000daa Compare May 30, 2016 20:16
@lattner
Copy link
Contributor

lattner commented May 30, 2016

I agree, I think that "type %0 does not have reference semantics, comparison isn't allowed" is actively misleading, because it could causes someone to think that nullability goes with reference types. Something more along the lines of "type %0 is not optional, value cannot be nil" seems better.

@CodaFi
Copy link
Contributor Author

CodaFi commented May 30, 2016

All excellent suggestions. Give me a moment to re-run the test suite on my end and we should be good to go.

@CodaFi CodaFi force-pushed the triple-value-meal branch 2 times, most recently from 6a0651d to 1325a52 Compare May 30, 2016 21:17
@CodaFi
Copy link
Contributor Author

CodaFi commented May 30, 2016

@lattner @gregomni Thanks for all of this. Any further comments?

@gregomni
Copy link
Contributor

LGTM!

@lattner
Copy link
Contributor

lattner commented May 31, 2016

I don't think this patch is the best it can be yet. I'd suggest removing the reference_type_comparison_with_nil_illegal case entirely, since the more general message is better - just remove the " if (lhsType->hasReferenceSemantics()) {" code.

The "getAnyPointerElementType() || lhsType->getAnyOptionalObjectType" may be correct, but it is confusing to me. I'd suggest making it more specific with something like:

if (lhsType->getAnyOptionalObjectType() && (overloadName == "!==" || overloadName == "==="))

Also, I don't think it is going to be a common error for people to use ===/!=== with UnsafePointer, I think they are most likely to use things like "!= nil" because they used to be nullable and now they aren't anymore. As such, I'd just special case the "type %0 is not optional, value can never be nil" message to say something like "type %0 cannot point to nil, use an %0? to achieve that" or something.

@lattner
Copy link
Contributor

lattner commented May 31, 2016

If you choose to include the unsafe pointer case, please add a test case for it as well.

@CodaFi
Copy link
Contributor Author

CodaFi commented May 31, 2016

It doesn't seem worth it to me to specialize for the pointer cases (yet, who knows?). I'll change the condition and remove the extraneous diagnostic.

@CodaFi CodaFi force-pushed the triple-value-meal branch from 1325a52 to a7ef67c Compare May 31, 2016 21:06
@lattner
Copy link
Contributor

lattner commented May 31, 2016

Sounds good, thanks!

@lattner
Copy link
Contributor

lattner commented May 31, 2016

@swift-ci please test and merge

@CodaFi CodaFi force-pushed the triple-value-meal branch from fb9b37d to 054f2ff Compare May 31, 2016 23:49
@CodaFi
Copy link
Contributor Author

CodaFi commented May 31, 2016

Sorry about that. More test changes that I expected there with that last change.

@lattner
Copy link
Contributor

lattner commented Jun 1, 2016

@swift-ci please test and merge

@swift-ci swift-ci merged commit 009f69b into swiftlang:master Jun 1, 2016
@CodaFi CodaFi deleted the triple-value-meal branch June 1, 2016 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants