Skip to content

Commit 6c7277b

Browse files
committed
[Type checker] Work around issues with badly broken generic code.
The GenericTypeOrArchetypeResolver changes are effectively porting some existing hacks from the now-dead PartialGenericTypeOrArchetypeResolver, which dodges some regressions in the compiler-crashers suite and fixes 11 new crashers. There is undoubtedly a more principled approach to fix these problems, most of which now step from recursively looking for a generic environment that isn't there, but doing so requires improvements to our name lookup.
1 parent 13e1714 commit 6c7277b

13 files changed

+31
-14
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7050,6 +7050,10 @@ void TypeChecker::validateDecl(ValueDecl *D, bool resolveTypeParams) {
70507050
return;
70517051
proto->computeType();
70527052

7053+
auto iBTC = proto->isBeingTypeChecked();
7054+
proto->setIsBeingTypeChecked();
7055+
SWIFT_DEFER { proto->setIsBeingTypeChecked(iBTC); };
7056+
70537057
// Resolve the inheritance clauses for each of the associated
70547058
// types.
70557059
for (auto member : proto->getMembers()) {

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ Type GenericTypeToArchetypeResolver::resolveGenericTypeParamType(
8787

8888
// Hack: See parseGenericParameters(). When the issue there is fixed,
8989
// we won't need the isInvalid() check anymore.
90-
if (gpDecl->isInvalid())
90+
if (gpDecl->isInvalid() || !GenericEnv)
9191
return ErrorType::get(gpDecl->getASTContext());
9292

93-
if (auto *Env = GenericEnv)
94-
return Env->mapTypeIntoContext(gp);
93+
auto type = GenericEnv->getMappingIfPresent(gp);
94+
if (!type)
95+
return ErrorType::get(gpDecl->getASTContext());;
9596

96-
return ErrorType::get(gpDecl->getASTContext());
97+
return *type;
9798
}
9899

99100
Type GenericTypeToArchetypeResolver::resolveDependentMemberType(
@@ -113,6 +114,10 @@ Type GenericTypeToArchetypeResolver::resolveSelfAssociatedType(
113114

114115
Type GenericTypeToArchetypeResolver::resolveTypeOfContext(DeclContext *dc,
115116
bool wantSelf) {
117+
// FIXME: Fallback case.
118+
if (dc->isGenericContext() && !dc->isValidGenericContext())
119+
return getTypeOfContext(dc, wantSelf);
120+
116121
return ArchetypeBuilder::mapTypeIntoContext(
117122
dc->getParentModule(), GenericEnv,
118123
getTypeOfContext(dc, wantSelf));
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
// RUN: not --crash %target-swift-ide-test -code-completion -code-completion-token=A -source-filename=%s
2-
// REQUIRES: asserts
32
{()=(var a{#^A^#

validation-test/compiler_crashers/28209-swift-protocoldecl-requiresclassslow.swift renamed to validation-test/compiler_crashers_fixed/28209-swift-protocoldecl-requiresclassslow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not --crash %target-swift-frontend %s -typecheck
1+
// RUN: not %target-swift-frontend %s -typecheck
22
// REQUIRES: asserts
33

44
// Distributed under the terms of the MIT license

validation-test/compiler_crashers/28351-swift-functiontype-get.swift renamed to validation-test/compiler_crashers_fixed/28351-swift-functiontype-get.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -typecheck
8+
// RUN: not %target-swift-frontend %s -typecheck
99
extension A{{}protocol A}protocol A:class{protocol A{}typealias e:A
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -typecheck
8+
// RUN: not %target-swift-frontend %s -typecheck
99
// REQUIRES: asserts
1010
{
1111
class A{

validation-test/compiler_crashers/28408-swift-typechecker-checkinheritanceclause.swift renamed to validation-test/compiler_crashers_fixed/28408-swift-typechecker-checkinheritanceclause.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -typecheck
8+
// RUN: not %target-swift-frontend %s -typecheck
99
// REQUIRES: asserts
1010
class A{{
1111
{

validation-test/compiler_crashers/28423-swift-typechecker-validatedecl.swift renamed to validation-test/compiler_crashers_fixed/28423-swift-typechecker-validatedecl.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -typecheck
8+
// RUN: not %target-swift-frontend %s -typecheck
99
// REQUIRES: asserts
1010
protocol a
1111
protocol a{

validation-test/compiler_crashers/28435-swift-genericenvironment-maptypeintocontext.swift renamed to validation-test/compiler_crashers_fixed/28435-swift-genericenvironment-maptypeintocontext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -typecheck
8+
// RUN: not %target-swift-frontend %s -typecheck
99
enum ST:d
1010
protocol a{typealias d:A.B{
1111
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
// REQUIRES: asserts
1010
protocol a{}protocol a{
1111
typealias e=a

0 commit comments

Comments
 (0)