Skip to content

[QoI] fix diagnosis of non-Optional enum used in optional pattern #4445

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
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 7 additions & 30 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1455,8 +1455,10 @@ bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,

case PatternKind::OptionalSome: {
auto *OP = cast<OptionalSomePattern>(P);
auto *enumDecl = type->getEnumOrBoundGenericEnum();
if (!enumDecl) {
OptionalTypeKind optionalKind;
Type elementType = type->getAnyOptionalObjectType(optionalKind);

if (elementType.isNull()) {
auto diagID = diag::optional_element_pattern_not_valid_type;
SourceLoc loc = OP->getQuestionLoc();
// Produce tailored diagnostic for if/let and other conditions.
Expand All @@ -1469,35 +1471,10 @@ bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
return true;
}

// If the element decl was not resolved (because it was spelled without a
// type as `.Foo`), resolve it now that we have a type.
if (!OP->getElementDecl()) {
auto *element = lookupEnumMemberElement(*this, dc, type, Context.Id_some,
OP->getLoc());
if (!element) {
diagnose(OP->getLoc(), diag::enum_element_pattern_member_not_found,
"Some", type);
return true;
}
OP->setElementDecl(element);
}

EnumElementDecl *elt = OP->getElementDecl();
// Is the enum element actually part of the enum type we're matching?
if (elt->getParentEnum() != enumDecl) {
diagnose(OP->getLoc(), diag::enum_element_pattern_not_member_of_enum,
"Some", type);
return true;
}
EnumElementDecl *elementDecl = Context.getOptionalSomeDecl(optionalKind);
assert(elementDecl && "missing optional some decl?!");
OP->setElementDecl(elementDecl);

// Check the subpattern & push the enum element type down onto it.
Type elementType;
if (elt->hasArgumentType())
elementType = type->getTypeOfMember(elt->getModuleContext(),
elt, this,
elt->getArgumentInterfaceType());
else
elementType = TupleType::getEmpty(Context);
Pattern *sub = OP->getSubPattern();
if (coercePatternToType(sub, dc, elementType,
subOptions|TR_FromNonInferredPattern|TR_EnumPatternPayload,
Expand Down
39 changes: 0 additions & 39 deletions test/SILGen/statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -591,45 +591,6 @@ func testRequireOptional2(_ a : String?) -> String {
return t
}

enum MyOptional<Wrapped> {
case none
case some(Wrapped)
}

// CHECK-LABEL: sil hidden @_TF10statements28testAddressOnlyEnumInRequire
// CHECK: bb0(%0 : $*T, %1 : $*MyOptional<T>):
// CHECK-NEXT: debug_value_addr %1 : $*MyOptional<T>, let, name "a"
// CHECK-NEXT: %3 = alloc_stack $T, let, name "t"
// CHECK-NEXT: %4 = alloc_stack $MyOptional<T>
// CHECK-NEXT: copy_addr %1 to [initialization] %4 : $*MyOptional<T>
// CHECK-NEXT: switch_enum_addr %4 : $*MyOptional<T>, case #MyOptional.some!enumelt.1: bb2, default bb1
func testAddressOnlyEnumInRequire<T>(_ a: MyOptional<T>) -> T {
// CHECK: bb1:
// CHECK-NEXT: dealloc_stack %4
// CHECK-NEXT: dealloc_stack %3
// CHECK-NEXT: br bb3
guard let t = a else { abort() }

// CHECK: bb2:
// CHECK-NEXT: %10 = unchecked_take_enum_data_addr %4 : $*MyOptional<T>, #MyOptional.some!enumelt.1
// CHECK-NEXT: copy_addr [take] %10 to [initialization] %3 : $*T
// CHECK-NEXT: dealloc_stack %4
// CHECK-NEXT: copy_addr [take] %3 to [initialization] %0 : $*T
// CHECK-NEXT: dealloc_stack %3
// CHECK-NEXT: destroy_addr %1 : $*MyOptional<T>
// CHECK-NEXT: tuple ()
// CHECK-NEXT: return

// CHECK: bb3:
// CHECK-NEXT: // function_ref statements.abort () -> Swift.Never
// CHECK-NEXT: %18 = function_ref @_TF10statements5abortFT_Os5Never
// CHECK-NEXT: %19 = apply %18() : $@convention(thin) () -> Never
// CHECK-NEXT: unreachable

return t
}



// CHECK-LABEL: sil hidden @_TF10statements19testCleanupEmission
// <rdar://problem/20563234> let-else problem: cleanups for bound patterns shouldn't be run in the else block
Expand Down
15 changes: 13 additions & 2 deletions test/stmt/if_while_var.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// RUN: %target-parse-verify-swift

struct NonOptionalStruct {}
enum NonOptionalEnum { case foo }

func foo() -> Int? { return .none }
func nonOptional() -> Int { return 0 }
func nonOptionalStruct() -> NonOptionalStruct { fatalError() }
func nonOptionalEnum() -> NonOptionalEnum { fatalError() }
func use(_ x: Int) {}
func modify(_ x: inout Int) {}

Expand All @@ -19,7 +23,14 @@ if var x = foo() {

use(x) // expected-error{{unresolved identifier 'x'}}

if let x = nonOptional() { } // expected-error{{initializer for conditional binding must have Optional type, not 'Int'}}
if let x = nonOptionalStruct() { } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
if let x = nonOptionalEnum() { } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalEnum'}}

guard let _ = nonOptionalStruct() else { fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
guard let _ = nonOptionalEnum() else { fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalEnum'}}

if case let x? = nonOptionalStruct() { } // expected-error{{'?' pattern cannot match values of type 'NonOptionalStruct'}}
if case let x? = nonOptionalEnum() { } // expected-error{{'?' pattern cannot match values of type 'NonOptionalEnum'}}

class B {} // expected-note * {{did you mean 'B'?}}
class D : B {}// expected-note * {{did you mean 'D'?}}
Expand Down