Skip to content

Verify that a GenericTypeParamDecl's depth and index are correct #20445

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
Nov 9, 2018
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
42 changes: 42 additions & 0 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2738,6 +2738,48 @@ class Verifier : public ASTWalker {
verifyCheckedBase(nominal);
}

void verifyCheckedAlways(GenericTypeParamDecl *GTPD) {
PrettyStackTraceDecl debugStack("verifying GenericTypeParamDecl", GTPD);

const DeclContext *DC = GTPD->getDeclContext();
if (!GTPD->getDeclContext()->isInnermostContextGeneric()) {
Out << "DeclContext of GenericTypeParamDecl does not have "
"generic params\n";
abort();
}

GenericParamList *paramList =
static_cast<const GenericContext *>(DC)->getGenericParams();
if (!paramList) {
Out << "DeclContext of GenericTypeParamDecl does not have "
"generic params\n";
abort();
}

unsigned currentDepth = paramList->getDepth();
if (currentDepth < GTPD->getDepth()) {
Out << "GenericTypeParamDecl has incorrect depth\n";
abort();
}
while (currentDepth > GTPD->getDepth()) {
paramList = paramList->getOuterParameters();
--currentDepth;
}
assert(paramList && "this is guaranteed by the parameter list's depth");

if (paramList->size() <= GTPD->getIndex() ||
paramList->getParams()[GTPD->getIndex()] != GTPD) {
if (llvm::is_contained(paramList->getParams(), GTPD))
Out << "GenericTypeParamDecl has incorrect index\n";
else
Out << "GenericTypeParamDecl not found in GenericParamList; "
"incorrect depth or wrong DeclContext\n";
abort();
}

verifyCheckedBase(GTPD);
}

void verifyChecked(ExtensionDecl *ext) {
// Make sure that the protocol conformances are complete.
for (auto conformance : ext->getLocalConformances()) {
Expand Down
9 changes: 6 additions & 3 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2674,7 +2674,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
index);

// Always create GenericTypeParamDecls in the associated module;
// maybeReadGenericParams() will reparent them.
// the real context will reparent them.
auto DC = getAssociatedModule();
auto genericParam = createDecl<GenericTypeParamDecl>(DC,
getIdentifier(nameID),
Expand Down Expand Up @@ -3936,9 +3936,12 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
// Generic parameter lists are written from outermost to innermost.
// Keep reading until we run out of generic parameter lists.
GenericParamList *outerParams = nullptr;
while (auto *genericParams = maybeReadGenericParams(DC, outerParams))
while (auto *genericParams = maybeReadGenericParams(DC, outerParams)) {
// We do this repeatedly to set up the correct DeclContexts for the
// GenericTypeParamDecls in the list.
extension->setGenericParams(genericParams);
outerParams = genericParams;
extension->setGenericParams(outerParams);
}

configureGenericEnvironment(extension, genericEnvID);

Expand Down