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

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

Merged
merged 51 commits into from Jan 8, 2019

Conversation

Projects
None yet
5 participants
@theblixguy
Copy link
Contributor

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 added some commits Jan 4, 2019

@theblixguy

This comment has been minimized.

Copy link
Contributor

theblixguy commented Jan 4, 2019

@xedin
Copy link
Member

xedin left a comment

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

This comment has been minimized.

Copy link
Member

slavapestov commented Jan 4, 2019

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

This comment has been minimized.

Copy link
Contributor

theblixguy commented Jan 4, 2019

@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

@brentdax
Copy link
Collaborator

brentdax left a comment

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

Show resolved Hide resolved lib/Sema/CSApply.cpp Outdated
Show resolved Hide resolved include/swift/AST/DiagnosticsSema.def Outdated
Show resolved Hide resolved lib/Sema/CSApply.cpp Outdated
Show resolved Hide resolved lib/Sema/CSApply.cpp Outdated
Show resolved Hide resolved test/decl/enum/enumtest.swift
Show resolved Hide resolved test/decl/enum/enumtest.swift Outdated
Show resolved Hide resolved lib/Sema/CSApply.cpp Outdated
Show resolved Hide resolved include/swift/AST/DiagnosticsSema.def Outdated
@theblixguy

This comment has been minimized.

Copy link
Contributor

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 🙏

Suyash Srijan and others added some commits Jan 6, 2019

@xedin

This comment has been minimized.

Copy link
Member

xedin commented Jan 6, 2019

@swift-ci please smoke test

@theblixguy

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

xedin commented Jan 6, 2019

@swift-ci please smoke test

@theblixguy

This comment has been minimized.

Copy link
Contributor

theblixguy commented Jan 6, 2019

All tests have passed 🎉 @xedin

Show resolved Hide resolved lib/Sema/CSApply.cpp Outdated
Show resolved Hide resolved lib/Sema/CSApply.cpp Outdated
@theblixguy

This comment has been minimized.

Copy link
Contributor

theblixguy commented Jan 7, 2019

@brentdax @xedin could you invoke the CI now?

@brentdax

This comment has been minimized.

Copy link
Collaborator

brentdax commented Jan 7, 2019

@swift-ci please smoke test

@theblixguy

This comment has been minimized.

Copy link
Contributor

theblixguy commented Jan 7, 2019

Good to merge now? @brentdax @xedin

@slavapestov slavapestov merged commit f30ba30 into apple:master Jan 8, 2019

2 checks passed

Swift Test Linux Platform (smoke test)
Details
Swift Test OS X Platform (smoke test)
Details

@theblixguy theblixguy deleted the theblixguy:fix/SR-2176 branch Jan 8, 2019

theblixguy added a commit to theblixguy/swift that referenced this pull request Jan 8, 2019

[Sema] Add warning for ambiguous value assignment when using Optional (
…apple#21621)

* [sema] emit a diag if the enum case matches Optional<T>.none

* [test] update tests

* [sema] fix indent

* [test] fix indent

* [test] add more test cases

* [test] add even more test cases

* [sema] move the check to CSApply

* [diag] update diagnostic message

* [test] update tests

* [test] fix conflicts

* [diag] reflow lines

* [sema] reindent using spaces

* [test] adds new line

* [diag] update diagnostic message

* [sema] add support for structs as well

* [test] add more test cases

* [sema] check for enum assoc values

* [test] add more test cases

* [diag] add fixit notes

* [sema] emit fix its

* [diag] rename diag names

* [sema] fit within 80 char line limit

* [sema] use baseUnwrappedType's name directly

* [test] adds nested generic enum tests

* [test] fix indent

* [test] adds fixit check

* [test] re-indent some enums

* [sema] [csapply] extract code into a separate function

* [sema] [csapply] remove redundant vardecl check

* [sema] [csapply] reindent

* [sema] [csapply] removes extra line

* [sema] [csapply] use cantype & check for extension on Optional

* [diag] update diagnostic message

* [sema] [csapply] fix ident

* [test] update tests

* [sema] [csapply] fix typo and remove redundant isOptional check

* [sema] [csapply] update var name & comments

* [sema] [csapply] bring back isOptional check

* [test] add expected-note for fix-its

* [sema] [csapply] fix a crash

* [sema] [csapply] move isOptional check outside

* [test] fix indent

* [test] fix typo

* [sema] [csapply] use baseTyUnwrapped for fixit

* [test] fix columns for fixits

* [test] update column numbers

* [sema] [csapply] move code out of for loop

(cherry picked from commit f30ba30)

theblixguy added a commit to theblixguy/swift that referenced this pull request Jan 8, 2019

[Sema] Add warning for ambiguous value assignment when using Optional (
…apple#21621)

* [sema] emit a diag if the enum case matches Optional<T>.none

* [test] update tests

* [sema] fix indent

* [test] fix indent

* [test] add more test cases

* [test] add even more test cases

* [sema] move the check to CSApply

* [diag] update diagnostic message

* [test] update tests

* [test] fix conflicts

* [diag] reflow lines

* [sema] reindent using spaces

* [test] adds new line

* [diag] update diagnostic message

* [sema] add support for structs as well

* [test] add more test cases

* [sema] check for enum assoc values

* [test] add more test cases

* [diag] add fixit notes

* [sema] emit fix its

* [diag] rename diag names

* [sema] fit within 80 char line limit

* [sema] use baseUnwrappedType's name directly

* [test] adds nested generic enum tests

* [test] fix indent

* [test] adds fixit check

* [test] re-indent some enums

* [sema] [csapply] extract code into a separate function

* [sema] [csapply] remove redundant vardecl check

* [sema] [csapply] reindent

* [sema] [csapply] removes extra line

* [sema] [csapply] use cantype & check for extension on Optional

* [diag] update diagnostic message

* [sema] [csapply] fix ident

* [test] update tests

* [sema] [csapply] fix typo and remove redundant isOptional check

* [sema] [csapply] update var name & comments

* [sema] [csapply] bring back isOptional check

* [test] add expected-note for fix-its

* [sema] [csapply] fix a crash

* [sema] [csapply] move isOptional check outside

* [test] fix indent

* [test] fix typo

* [sema] [csapply] use baseTyUnwrapped for fixit

* [test] fix columns for fixits

* [test] update column numbers

* [sema] [csapply] move code out of for loop

(cherry picked from commit f30ba30)

theblixguy added a commit to theblixguy/swift that referenced this pull request Jan 8, 2019

[Sema] Add warning for ambiguous value assignment when using Optional (
…apple#21621)

* [sema] emit a diag if the enum case matches Optional<T>.none

* [test] update tests

* [sema] fix indent

* [test] fix indent

* [test] add more test cases

* [test] add even more test cases

* [sema] move the check to CSApply

* [diag] update diagnostic message

* [test] update tests

* [test] fix conflicts

* [diag] reflow lines

* [sema] reindent using spaces

* [test] adds new line

* [diag] update diagnostic message

* [sema] add support for structs as well

* [test] add more test cases

* [sema] check for enum assoc values

* [test] add more test cases

* [diag] add fixit notes

* [sema] emit fix its

* [diag] rename diag names

* [sema] fit within 80 char line limit

* [sema] use baseUnwrappedType's name directly

* [test] adds nested generic enum tests

* [test] fix indent

* [test] adds fixit check

* [test] re-indent some enums

* [sema] [csapply] extract code into a separate function

* [sema] [csapply] remove redundant vardecl check

* [sema] [csapply] reindent

* [sema] [csapply] removes extra line

* [sema] [csapply] use cantype & check for extension on Optional

* [diag] update diagnostic message

* [sema] [csapply] fix ident

* [test] update tests

* [sema] [csapply] fix typo and remove redundant isOptional check

* [sema] [csapply] update var name & comments

* [sema] [csapply] bring back isOptional check

* [test] add expected-note for fix-its

* [sema] [csapply] fix a crash

* [sema] [csapply] move isOptional check outside

* [test] fix indent

* [test] fix typo

* [sema] [csapply] use baseTyUnwrapped for fixit

* [test] fix columns for fixits

* [test] update column numbers

* [sema] [csapply] move code out of for loop

(cherry picked from commit f30ba30)

theblixguy added a commit to theblixguy/swift that referenced this pull request Jan 8, 2019

[Sema] Add warning for ambiguous value assignment when using Optional (
…apple#21621)

* [sema] emit a diag if the enum case matches Optional<T>.none

* [test] update tests

* [sema] fix indent

* [test] fix indent

* [test] add more test cases

* [test] add even more test cases

* [sema] move the check to CSApply

* [diag] update diagnostic message

* [test] update tests

* [test] fix conflicts

* [diag] reflow lines

* [sema] reindent using spaces

* [test] adds new line

* [diag] update diagnostic message

* [sema] add support for structs as well

* [test] add more test cases

* [sema] check for enum assoc values

* [test] add more test cases

* [diag] add fixit notes

* [sema] emit fix its

* [diag] rename diag names

* [sema] fit within 80 char line limit

* [sema] use baseUnwrappedType's name directly

* [test] adds nested generic enum tests

* [test] fix indent

* [test] adds fixit check

* [test] re-indent some enums

* [sema] [csapply] extract code into a separate function

* [sema] [csapply] remove redundant vardecl check

* [sema] [csapply] reindent

* [sema] [csapply] removes extra line

* [sema] [csapply] use cantype & check for extension on Optional

* [diag] update diagnostic message

* [sema] [csapply] fix ident

* [test] update tests

* [sema] [csapply] fix typo and remove redundant isOptional check

* [sema] [csapply] update var name & comments

* [sema] [csapply] bring back isOptional check

* [test] add expected-note for fix-its

* [sema] [csapply] fix a crash

* [sema] [csapply] move isOptional check outside

* [test] fix indent

* [test] fix typo

* [sema] [csapply] use baseTyUnwrapped for fixit

* [test] fix columns for fixits

* [test] update column numbers

* [sema] [csapply] move code out of for loop

(cherry picked from commit f30ba30)

theblixguy added a commit to theblixguy/swift that referenced this pull request Jan 8, 2019

[Sema] Add warning for ambiguous value assignment when using Optional (
…apple#21621)

* [sema] emit a diag if the enum case matches Optional<T>.none

* [test] update tests

* [sema] fix indent

* [test] fix indent

* [test] add more test cases

* [test] add even more test cases

* [sema] move the check to CSApply

* [diag] update diagnostic message

* [test] update tests

* [test] fix conflicts

* [diag] reflow lines

* [sema] reindent using spaces

* [test] adds new line

* [diag] update diagnostic message

* [sema] add support for structs as well

* [test] add more test cases

* [sema] check for enum assoc values

* [test] add more test cases

* [diag] add fixit notes

* [sema] emit fix its

* [diag] rename diag names

* [sema] fit within 80 char line limit

* [sema] use baseUnwrappedType's name directly

* [test] adds nested generic enum tests

* [test] fix indent

* [test] adds fixit check

* [test] re-indent some enums

* [sema] [csapply] extract code into a separate function

* [sema] [csapply] remove redundant vardecl check

* [sema] [csapply] reindent

* [sema] [csapply] removes extra line

* [sema] [csapply] use cantype & check for extension on Optional

* [diag] update diagnostic message

* [sema] [csapply] fix ident

* [test] update tests

* [sema] [csapply] fix typo and remove redundant isOptional check

* [sema] [csapply] update var name & comments

* [sema] [csapply] bring back isOptional check

* [test] add expected-note for fix-its

* [sema] [csapply] fix a crash

* [sema] [csapply] move isOptional check outside

* [test] fix indent

* [test] fix typo

* [sema] [csapply] use baseTyUnwrapped for fixit

* [test] fix columns for fixits

* [test] update column numbers

* [sema] [csapply] move code out of for loop

(cherry picked from commit f30ba30)

theblixguy added a commit to theblixguy/swift that referenced this pull request Jan 8, 2019

[Sema] Add warning for ambiguous value assignment when using Optional (
…apple#21621)

* [sema] emit a diag if the enum case matches Optional<T>.none

* [test] update tests

* [sema] fix indent

* [test] fix indent

* [test] add more test cases

* [test] add even more test cases

* [sema] move the check to CSApply

* [diag] update diagnostic message

* [test] update tests

* [test] fix conflicts

* [diag] reflow lines

* [sema] reindent using spaces

* [test] adds new line

* [diag] update diagnostic message

* [sema] add support for structs as well

* [test] add more test cases

* [sema] check for enum assoc values

* [test] add more test cases

* [diag] add fixit notes

* [sema] emit fix its

* [diag] rename diag names

* [sema] fit within 80 char line limit

* [sema] use baseUnwrappedType's name directly

* [test] adds nested generic enum tests

* [test] fix indent

* [test] adds fixit check

* [test] re-indent some enums

* [sema] [csapply] extract code into a separate function

* [sema] [csapply] remove redundant vardecl check

* [sema] [csapply] reindent

* [sema] [csapply] removes extra line

* [sema] [csapply] use cantype & check for extension on Optional

* [diag] update diagnostic message

* [sema] [csapply] fix ident

* [test] update tests

* [sema] [csapply] fix typo and remove redundant isOptional check

* [sema] [csapply] update var name & comments

* [sema] [csapply] bring back isOptional check

* [test] add expected-note for fix-its

* [sema] [csapply] fix a crash

* [sema] [csapply] move isOptional check outside

* [test] fix indent

* [test] fix typo

* [sema] [csapply] use baseTyUnwrapped for fixit

* [test] fix columns for fixits

* [test] update column numbers

* [sema] [csapply] move code out of for loop

(cherry picked from commit f30ba30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment