Skip to content

Diagnose Circular Typealiases in Protocols #138

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
Dec 7, 2015
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
13 changes: 12 additions & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,11 @@ class alignas(1 << DeclAlignInBits) Decl {
unsigned : NumTypeDeclBits;

unsigned Recursive : 1;

/// Whether or not this declaration is currently being type-checked.
unsigned BeingTypeChecked : 1;
};
enum { NumAssociatedTypeDeclBits = NumTypeDeclBits + 1 };
enum { NumAssociatedTypeDeclBits = NumTypeDeclBits + 2 };
static_assert(NumAssociatedTypeDeclBits <= 32, "fits in an unsigned");

class ImportDeclBitfields {
Expand Down Expand Up @@ -2605,6 +2608,14 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
void setIsRecursive() { AssociatedTypeDeclBits.Recursive = true; }
bool isRecursive() { return AssociatedTypeDeclBits.Recursive; }

/// Whether the declaration is currently being validated.
bool isBeingTypeChecked() { return AssociatedTypeDeclBits.BeingTypeChecked; }

/// Toggle whether or not the declaration is being validated.
void setIsBeingTypeChecked(bool ibt = true) {
AssociatedTypeDeclBits.BeingTypeChecked = ibt;
}

static bool classof(const Decl *D) {
return D->getKind() == DeclKind::AssociatedType;
}
Expand Down
17 changes: 16 additions & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3173,7 +3173,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
TAD->getUnderlyingTypeLoc().setInvalidType(TC.Context);
} else if (TAD->getDeclContext()->isGenericContext()) {
TAD->setInterfaceType(
TC.getInterfaceTypeFromInternalType(TAD->getDeclContext(),
TC.getInterfaceTypeFromInternalType(TAD->getDeclContext(),
TAD->getType()));
Copy link
Member

Choose a reason for hiding this comment

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

Unnecessary whitespace change here.

Choose a reason for hiding this comment

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

I would go as far as saying that white space had meaning and now is gone. It was enforcing that getInterfaceTypeFromInternalType is an argument passed to setInterfaceType.
It's merged now but would you oppose adding this space back in? I'm not one for PR (just to correct this) with such minimal changes but...

}

Expand All @@ -3192,6 +3192,19 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}

void visitAssociatedTypeDecl(AssociatedTypeDecl *assocType) {
if (assocType->isBeingTypeChecked()) {

if (!assocType->hasType()) {
assocType->setInvalid();
assocType->overwriteType(ErrorType::get(TC.Context));
}

TC.diagnose(assocType->getLoc(), diag::circular_type_alias, assocType->getName());
return;
}

assocType->setIsBeingTypeChecked();

TC.checkDeclAttributesEarly(assocType);
if (!assocType->hasAccessibility())
assocType->setAccessibility(assocType->getProtocol()->getFormalAccess());
Expand All @@ -3205,6 +3218,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
defaultDefinition.setInvalidType(TC.Context);
}
TC.checkDeclAttributes(assocType);

assocType->setIsBeingTypeChecked(false);
}

bool checkUnsupportedNestedGeneric(NominalTypeDecl *NTD) {
Expand Down
4 changes: 4 additions & 0 deletions test/decl/protocol/req/recursion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ public struct S<A: P where A.T == S<A>> {}
class X<T where T == X> { // expected-error{{same-type requirement makes generic parameter 'T' non-generic}}
var type: T { return self.dynamicType } // expected-error{{use of undeclared type 'T'}}
}

protocol Y {
typealias Z = Z // expected-error{{type alias 'Z' circularly references itself}}
}