Skip to content

[Sema] Add warning for ambiguous value assignment when using Optional #21621

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 51 commits into from
Jan 8, 2019
Merged

[Sema] Add warning for ambiguous value assignment when using Optional #21621

merged 51 commits into from
Jan 8, 2019

Conversation

theblixguy
Copy link
Collaborator

@theblixguy theblixguy commented Jan 4, 2019

This issue is best explained with an example:

enum Foo {
  case bar
  case none
}

// The assigned value is (silently) Optional.none
// rather than Foo.none as you would expect.
let _: Foo? = .none

let test: Foo? = Foo.none
// False, as it matches Optional.none, rather than Foo.none.
let result: Bool = test == .none 

// Same with a struct // 

struct Foo {
  static let bar = Foo()
  static let none = Foo()
}

let _: Foo? = .none // uh-oh

This PR fixes this issue by diagnosing it as a warning and offering two fixits: prefix Optional or prefix the explicit type to silence the warning.

Resolves SR-2176, SR-3711, SR-6044, SR-8283, SR-8451 & SR-8515.
Resolves rdar://problem/26126801.

@theblixguy theblixguy changed the title [Se a]SR-2176: Add warning for ambiguous enum value assignment [Sema] SR-2176: Add warning for ambiguous enum value assignment Jan 4, 2019
@theblixguy
Copy link
Collaborator Author

cc @jrose-apple @xedin

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

Overall, I think this is supposed to be an error instead of a warning but warning is better than nothing.

It seems like it could only happen in expression context, so maybe it would make sense for this warning to be produced by the constraint solver instead? You can take a look at ExprRewritter::visitUnresolvedMemberExpr, I think it would be reasonable to put that check there if the base was optional type.

Also it might be reasonable to mention member name as well e.g. enum case .noneexists in both 'E' andOptional, picked the one from 'Optional<E>' or something like that...

@slavapestov
Copy link
Contributor

A warning is our only choice since an error would create a source compatibility issue. Here's a test case demonstrating that it needs to happen in CSApply's ExprRewriter and not when checking the initial value of a property:

enum E {
  case none
}

func foo(_: E?) {}

foo(.none) // should be the same warning?

@theblixguy
Copy link
Collaborator Author

@slavapestov @xedin done 👍

@theblixguy theblixguy changed the title [Sema] SR-2176: Add warning for ambiguous enum value assignment [Sema] Add warning for ambiguous enum value assignment Jan 4, 2019
@theblixguy theblixguy changed the title [Sema] Add warning for ambiguous enum value assignment [Sema] Add warning for ambiguous enum value assignment when using Optional Jan 4, 2019
Copy link
Contributor

@beccadax beccadax left a comment

Choose a reason for hiding this comment

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

I have a lot of suggestions, but this is a really solid start!

@theblixguy
Copy link
Collaborator Author

@slavapestov can you invoke the CI again?

@xedin
Copy link
Contributor

xedin commented Jan 6, 2019

@swift-ci please smoke test

@theblixguy
Copy link
Collaborator Author

theblixguy commented Jan 6, 2019

Seems like I should've attached the fix-it to the expected-note rather than the expected-warning. Also, there was a crash because it was looking up the enum case in Optional extensions or tuple types, which I have now fixed.

@xedin can you invoke the CI now? The tests should now pass 🙏

@xedin
Copy link
Contributor

xedin commented Jan 6, 2019

@swift-ci please smoke test

@theblixguy
Copy link
Collaborator Author

theblixguy commented Jan 6, 2019

The column numbers I used for the fixits differed by 1 🤦‍♂️ @xedin can you invoke the CI once again? Thanks 😄

@xedin
Copy link
Contributor

xedin commented Jan 6, 2019

@swift-ci please smoke test

@theblixguy
Copy link
Collaborator Author

All tests have passed 🎉 @xedin

@theblixguy
Copy link
Collaborator Author

@brentdax @xedin could you invoke the CI now?

@beccadax
Copy link
Contributor

beccadax commented Jan 7, 2019

@swift-ci please smoke test

@theblixguy
Copy link
Collaborator Author

Good to merge now? @brentdax @xedin

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.

5 participants