Skip to content

[AST/Sema] Don't crash when referencing enum case in where-clause #1554

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 2 commits into from
Mar 13, 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
4 changes: 3 additions & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5270,7 +5270,9 @@ class EnumElementDecl : public ValueDecl {
static_cast<unsigned>(ElementRecursiveness::NotRecursive);
}

void computeType();
/// \returns false if there was an error during the computation rendering the
/// EnumElementDecl invalid, true otherwise.
bool computeType();

bool hasArgumentType() const { return !ArgumentType.getType().isNull(); }
Type getArgumentType() const { return ArgumentType.getType(); }
Expand Down
10 changes: 8 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4309,10 +4309,15 @@ SourceRange EnumElementDecl::getSourceRange() const {
return {getStartLoc(), getNameLoc()};
}

void EnumElementDecl::computeType() {
bool EnumElementDecl::computeType() {
EnumDecl *ED = getParentEnum();

Type resultTy = ED->getDeclaredTypeInContext();

if (resultTy->is<ErrorType>()) {
setType(resultTy);
return false;
}

Type argTy = MetatypeType::get(resultTy);

// The type of the enum element is either (T) -> T or (T) -> ArgType -> T.
Expand All @@ -4326,6 +4331,7 @@ void EnumElementDecl::computeType() {
resultTy = FunctionType::get(argTy, resultTy);

setType(resultTy);
return true;
}

Type EnumElementDecl::getArgumentInterfaceType() const {
Expand Down
13 changes: 8 additions & 5 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5543,11 +5543,14 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
EED->setRecursiveness(ElementRecursiveness::NotRecursive);
}

// Now that we have an argument type we can set the element's declared
// type.
if (!EED->hasType())
EED->computeType();
EED->setIsBeingTypeChecked(false);
{
defer { EED->setIsBeingTypeChecked(false); };

// Now that we have an argument type we can set the element's declared
// type.
if (!EED->hasType() && !EED->computeType())
return;
}

// Test for type parameters, as opposed to a generic decl context, in
// case the enclosing enum type was illegally declared inside of a generic
Expand Down
2 changes: 2 additions & 0 deletions test/Generics/generic_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,5 @@ struct UnsolvableInheritance1<T : T.A> {}
struct UnsolvableInheritance2<T : U.A, U : T.A> {}
// expected-error@-1 {{inheritance from non-protocol, non-class type 'U.A'}}
// expected-error@-2 {{inheritance from non-protocol, non-class type 'T.A'}}

enum X7<T where X7.X : G> { case X } // expected-error{{'X' is not a member type of 'X7<T>'}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// RUN: not --crash %target-swift-frontend %s -parse
// RUN: not %target-swift-frontend %s -parse
// REQUIRES: asserts
{enum S<T where S.c:A{case c