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

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

Merged
merged 51 commits into from Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
5501bee
[sema] emit a diag if the enum case matches Optional<T>.none
Jan 4, 2019
39db6a2
[test] update tests
Jan 4, 2019
fea7221
[sema] fix indent
Jan 4, 2019
3e295b1
[test] fix indent
theblixguy Jan 4, 2019
193dfa4
[test] add more test cases
theblixguy Jan 4, 2019
07fb819
[test] add even more test cases
theblixguy Jan 4, 2019
ffcf5ef
[sema] move the check to CSApply
Jan 4, 2019
e9bd7c9
[diag] update diagnostic message
Jan 4, 2019
08150d2
[test] update tests
Jan 4, 2019
19cff10
[test] resolve conflict
Jan 4, 2019
2862ff0
[test] fix conflicts
Jan 4, 2019
71f3092
[diag] reflow lines
Jan 4, 2019
599ab70
[sema] reindent using spaces
Jan 4, 2019
8ae8d58
Merge remote-tracking branch 'upstream/master' into fix/SR-2176
Jan 4, 2019
6ab5268
[test] adds new line
Jan 4, 2019
3ed50ba
[diag] update diagnostic message
Jan 4, 2019
c3fae9a
[sema] add support for structs as well
Jan 4, 2019
d140e9e
[test] add more test cases
Jan 4, 2019
0710127
[sema] check for enum assoc values
Jan 4, 2019
37dd800
[test] add more test cases
Jan 4, 2019
7ac619e
[diag] add fixit notes
Jan 4, 2019
b846c4e
[sema] emit fix its
Jan 4, 2019
b7ffac3
[diag] rename diag names
Jan 4, 2019
3d6ecfc
[sema] fit within 80 char line limit
Jan 4, 2019
ba13f9f
[sema] use baseUnwrappedType's name directly
Jan 4, 2019
7a47674
[test] adds nested generic enum tests
Jan 4, 2019
5a89456
[test] fix indent
theblixguy Jan 4, 2019
6b8343b
[test] adds fixit check
Jan 4, 2019
fe57f14
Merge branch 'fix/SR-2176' of https://github.com/theblixguy/swift int…
Jan 4, 2019
dceb43c
[test] re-indent some enums
theblixguy Jan 4, 2019
6be62cd
[sema] [csapply] extract code into a separate function
Jan 5, 2019
f08670f
[sema] [csapply] remove redundant vardecl check
Jan 5, 2019
b470f22
[sema] [csapply] reindent
Jan 5, 2019
fabd559
[sema] [csapply] removes extra line
Jan 5, 2019
d7da34b
Merge remote-tracking branch 'upstream/master' into fix/SR-2176
Jan 6, 2019
aacc63e
[sema] [csapply] use cantype & check for extension on Optional
Jan 6, 2019
268b9db
[diag] update diagnostic message
Jan 6, 2019
5c9d5ce
[sema] [csapply] fix ident
Jan 6, 2019
16b2eae
[test] update tests
Jan 6, 2019
06e0678
[sema] [csapply] fix typo and remove redundant isOptional check
Jan 6, 2019
87344c8
[sema] [csapply] update var name & comments
Jan 6, 2019
1f6ea3d
[sema] [csapply] bring back isOptional check
Jan 6, 2019
ed10386
[test] add expected-note for fix-its
Jan 6, 2019
95e3f1d
[sema] [csapply] fix a crash
Jan 6, 2019
1a38061
[sema] [csapply] move isOptional check outside
Jan 6, 2019
da09422
[test] fix indent
theblixguy Jan 6, 2019
bb30682
[test] fix typo
theblixguy Jan 6, 2019
07e8717
[sema] [csapply] use baseTyUnwrapped for fixit
Jan 6, 2019
cf23de4
[test] fix columns for fixits
Jan 6, 2019
d226b02
[test] update column numbers
Jan 6, 2019
0fc9c8b
[sema] [csapply] move code out of for loop
Jan 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Expand Up @@ -1391,6 +1391,8 @@ ERROR(pattern_binds_no_variables,none,
"variables",
(unsigned))

WARNING(ambiguous_pattern_binding_enum,none, "the enum case is ambiguous", ())

ERROR(nscoding_unstable_mangled_name,none,
"%select{private|fileprivate|nested|local}0 class %1 has an "
"unstable name when archiving via 'NSCoding'",
Expand Down
29 changes: 29 additions & 0 deletions lib/Sema/TypeCheckDecl.cpp
Expand Up @@ -2372,6 +2372,35 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
if (!PBD->isInitializerChecked(i) && PBD->getInit(i))
TC.typeCheckPatternBinding(PBD, i);
}

// If the pattern is optional and we're referring to an enum case via the dot syntax (i.e .none)
// and the enum case matches the Optional<T>.none case then emit a diagnostic
for (unsigned i = 0, e = PBD->getNumPatternEntries(); i != e; ++i) {
if (auto baseType = PBD->getPattern(i)->getType()->getOptionalObjectType()) {
if (auto DSCE = dyn_cast<DotSyntaxCallExpr>(PBD->getInit(i))) {
if (auto EED = dyn_cast<EnumElementDecl>(DSCE->getCalledValue())) {
if (EED->getParentEnum()->isOptionalDecl()) {

// Lookup the case in the original enum decl
auto results = TC.lookupMember(PBD->getModuleContext(), baseType, EED->getName(), defaultMemberLookupOptions);
bool caseExistsInBase = false;

for (LookupResultEntry result : results) {
if (auto *eed = dyn_cast<EnumElementDecl>(result.getValueDecl())) {
caseExistsInBase = true;
break;
}
}

// Emit a warning dignostic
if (caseExistsInBase) {
TC.Diags.diagnose(DSCE->getLoc(), swift::diag::ambiguous_pattern_binding_enum);
}
}
}
}
}
}
}

void visitSubscriptDecl(SubscriptDecl *SD) {
Expand Down
17 changes: 17 additions & 0 deletions test/decl/enum/enumtest.swift
Expand Up @@ -326,3 +326,20 @@ enum Lens<T> {
case baz((inout T) -> ()) // ok
case quux((inout T, inout T) -> ()) // ok
}

enum Foo {
theblixguy marked this conversation as resolved.
Show resolved Hide resolved
case bar
case none
}

let _: Foo? = .none // expected-warning {{the enum case is ambiguous}}
let _: Foo?? = .none // expected-warning {{the enum case is ambiguous}}

let _: Foo = .none // ok
let _: Foo = .bar // ok
let _: Foo? = .bar // ok
let _: Foo?? = .bar // ok
let _: Foo = Foo.bar // ok
let _: Foo = Foo.none // ok
let _: Foo? = Foo.none // ok
let _: Foo?? = Foo.none // ok