Skip to content

Commit 4b3943a

Browse files
authored
Merge pull request #82131 from slavapestov/get-superclass-for-decl-6.2
[6.2] AST: More robust TypeBase::getSuperclassForDecl()
2 parents 15f9e0e + 27a2396 commit 4b3943a

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

lib/AST/TypeSubstitution.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,20 @@ Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass,
517517
t = t->getSuperclass(useArchetypes);
518518
}
519519

520-
#ifndef NDEBUG
521-
auto *currentClass = getConcreteTypeForSuperclassTraversing(this)
522-
->getClassOrBoundGenericClass();
523-
assert(baseClass->isSuperclassOf(currentClass) &&
524-
"no inheritance relationship between given classes");
525-
#endif
526-
527-
return ErrorType::get(this);
520+
if (CONDITIONAL_ASSERT_enabled()) {
521+
auto *currentClass = getConcreteTypeForSuperclassTraversing(this)
522+
->getClassOrBoundGenericClass();
523+
ASSERT(baseClass->isSuperclassOf(currentClass) &&
524+
"no inheritance relationship between given classes");
525+
}
526+
527+
// We can end up here if the AST is invalid, because then
528+
// getSuperclassDecl() might resolve to a decl, and yet
529+
// getSuperclass() is just an ErrorType. Make sure we still
530+
// return a nominal type as the result though, and not an
531+
// ErrorType, because that's what callers expect.
532+
return baseClass->getDeclaredInterfaceType()
533+
.subst(SubstitutionMap())->getCanonicalType();
528534
}
529535

530536
SubstitutionMap TypeBase::getContextSubstitutionMap() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-typecheck-verify-swift
2+
// RUN: not %target-swift-frontend -typecheck -debug-generic-signatures %s 2>&1 | %FileCheck %s
3+
4+
class Base<T> {}
5+
class Derived : Base<Foo> {}
6+
// expected-error@-1 {{cannot find type 'Foo' in scope}}
7+
8+
// CHECK-LABEL: unify_superclass_types_invalid.(file).f@
9+
// CHECK: Generic signature: <T where T : Derived>
10+
func f<T>(_: T) where T: Base<Int>, T: Derived {}

0 commit comments

Comments
 (0)