swift icon indicating copy to clipboard operation
swift copied to clipboard

[SR-3348] Investigate cases where the generic environment of a GenericTypeToArchetypeResolver was nullptr

Open akyrtzi opened this issue 10 years ago • 3 comments

Previous ID SR-3348
Radar None
Original Reporter @akyrtzi
Type Bug
Status Resolved
Resolution Done

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee @slavapestov
Priority Medium

md5: 26d926336a996aac7d2e71aa3dc93135

Issue Description:

Apply this change:

diff --git a/lib/Sema/TypeCheckGeneric.cpp b/lib/Sema/TypeCheckGeneric.cpp
index c17b9bf..ff6e4c8 100644
--- a/lib/Sema/TypeCheckGeneric.cpp
+++ b/lib/Sema/TypeCheckGeneric.cpp
@@ -87,7 +87,7 @@ Type GenericTypeToArchetypeResolver::resolveGenericTypeParamType(
 
   // Hack: See parseGenericParameters(). When the issue there is fixed,
   // we won't need the isInvalid() check anymore.
-  if (gpDecl->isInvalid() || !GenericEnv)
+  if (gpDecl->isInvalid())
     return ErrorType::get(gpDecl->getASTContext());
 
   auto type = GenericEnv->getMappingIfPresent(gp);

and note the crashers in validation-test suite, e.g. validation-test/compiler_crashers_fixed/28466-segfault-0xc27624-0xc2741f-0xc25bb5-0xbcdbbb.swift and validation-test/IDE/crashers_fixed/099-swift-genericenvironment-maptypeintocontext.swift

Related to https://github.com/apple/swift/pull/6090 and 6c7277be2.

akyrtzi avatar Dec 06 '16 18:12 akyrtzi

I already did this investigation as part of the original pull request. If you ask for the GenericEnvironment of a DeclContext which is a module, file, or top level code, it's nullptr. This particular crasher centered around what was parsed to be a closure at the top level.

Basically, any uses of DeclContext::getGenericEnvironmentOfContext should be audited, as well as GenericTypeToArchetypeResolver constructors: both the one which takes a DeclContext, which then immediately calls getGenericEnvironmentOfContext; and the one which takes a GenericEnvironment itself.

I could see the former being a source of crashes if the DeclContext is nullptr, so the constructor could. immediately crash.

The latter was the one which allowed the nullptr to sneak in, early in PreCheckExpression::walkToClosureExprPre, where a GenericTypeToArchetypeResolver was created for a closure at the top level.

It sounds like Doug was working on removing or changing this; I don't know what Doug's plans are so, of course, I defer completely to him. I would just say that, if GenericEnvironment indeed is fundamentally a mapping, therefore a container type, it should have a non-null empty representation.

bitjammer avatar Dec 06 '16 19:12 bitjammer

This analysis is not quite right. The problem is that name lookup is finding a GenericTypeParamDecl even though we're not inside a generic context. This should not happen. There are two cases where this can occur that I'm aware of – I have work-in-progress fixes for both:

  • When we parse an associated type declaration, we have an error recovery path where if we see a generic parameter list, we parse it and throw it out. Unfortunately, the new generic parameters become available for name lookup even though the result is thrown out.
  • There are a few cases where we resolve inheritance clauses before a generic environment has been set up. In these cases it is invalid to reference generic parameters anyway (protocols and associated types cannot inherit from bound generic types) so we just need to catch the invalid code sooner.

slavapestov avatar Dec 30 '16 18:12 slavapestov

I believe this has been cleaned up.

slavapestov avatar Feb 26 '17 00:02 slavapestov