Skip to content

Commit d68f125

Browse files
committed
Teach the ASTVerifier not to force generic signatures
The ASTVerifier is called by parsing tests and IDE tests that may not necessarily set up a typechecker. Remove the verifier's ability to force computing generic signatures so we don't accidentally wander into the typechecker and crash.
1 parent d097c81 commit d68f125

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

lib/AST/ASTVerifier.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,32 @@ std::pair<bool, Expr *> dispatchVisitPreExprHelper(
167167
}
168168

169169
namespace {
170-
/// Retrieve the "overridden" declaration of this declaration, but only if
171-
// it's already been computed.
172-
template<typename T>
173-
T *getOverriddenDeclIfAvailable(T *decl) {
174-
if (!decl->overriddenDeclsComputed()) return nullptr;
170+
// Retrieve the "overridden" declaration of this declaration, but only if
171+
// it's already been computed.
172+
template <typename T> T *getOverriddenDeclIfAvailable(T *decl) {
173+
if (!decl->overriddenDeclsComputed())
174+
return nullptr;
175175

176-
return cast_or_null<T>(decl->getOverriddenDecl());
177-
}
176+
return cast_or_null<T>(decl->getOverriddenDecl());
178177
}
178+
179+
// Retrieve the generic signature of the innermost context that has been forced
180+
// so far.
181+
//
182+
// This avoids kicking off the request for a generic signature in the verifier.
183+
static GenericSignature *
184+
getNearestForcedGenericSignatureOfContext(DeclContext *dc) {
185+
do {
186+
if (auto decl = dc->getAsDecl())
187+
if (auto GC = decl->getAsGenericContext())
188+
if (GC->hasComputedGenericSignature())
189+
return GC->getGenericSignature();
190+
} while ((dc = dc->getParent()));
191+
192+
return nullptr;
193+
}
194+
} // namespace
195+
179196
class Verifier : public ASTWalker {
180197
PointerUnion<ModuleDecl *, SourceFile *> M;
181198
ASTContext &Ctx;
@@ -686,14 +703,14 @@ class Verifier : public ASTWalker {
686703

687704
void pushScope(DeclContext *scope) {
688705
Scopes.push_back(scope);
689-
GenericSig.push_back(scope->getGenericSignatureOfContext());
706+
GenericSig.push_back(::getNearestForcedGenericSignatureOfContext(scope));
690707
}
691708
void pushScope(BraceStmt *scope) {
692709
Scopes.push_back(scope);
693710
}
694711
void popScope(DeclContext *scope) {
695712
assert(Scopes.back().get<DeclContext*>() == scope);
696-
assert(GenericSig.back() == scope->getGenericSignatureOfContext());
713+
assert(GenericSig.back() == ::getNearestForcedGenericSignatureOfContext(scope));
697714
Scopes.pop_back();
698715
GenericSig.pop_back();
699716
}

0 commit comments

Comments
 (0)