Skip to content

[6.2] AST: More robust TypeBase::getSuperclassForDecl() #82131

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
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
22 changes: 14 additions & 8 deletions lib/AST/TypeSubstitution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,20 @@ Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass,
t = t->getSuperclass(useArchetypes);
}

#ifndef NDEBUG
auto *currentClass = getConcreteTypeForSuperclassTraversing(this)
->getClassOrBoundGenericClass();
assert(baseClass->isSuperclassOf(currentClass) &&
"no inheritance relationship between given classes");
#endif

return ErrorType::get(this);
if (CONDITIONAL_ASSERT_enabled()) {
auto *currentClass = getConcreteTypeForSuperclassTraversing(this)
->getClassOrBoundGenericClass();
ASSERT(baseClass->isSuperclassOf(currentClass) &&
"no inheritance relationship between given classes");
}

// We can end up here if the AST is invalid, because then
// getSuperclassDecl() might resolve to a decl, and yet
// getSuperclass() is just an ErrorType. Make sure we still
// return a nominal type as the result though, and not an
// ErrorType, because that's what callers expect.
return baseClass->getDeclaredInterfaceType()
.subst(SubstitutionMap())->getCanonicalType();
}

SubstitutionMap TypeBase::getContextSubstitutionMap() {
Expand Down
10 changes: 10 additions & 0 deletions test/Generics/unify_superclass_types_invalid.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-typecheck-verify-swift
// RUN: not %target-swift-frontend -typecheck -debug-generic-signatures %s 2>&1 | %FileCheck %s

class Base<T> {}
class Derived : Base<Foo> {}
// expected-error@-1 {{cannot find type 'Foo' in scope}}

// CHECK-LABEL: unify_superclass_types_invalid.(file).f@
// CHECK: Generic signature: <T where T : Derived>
func f<T>(_: T) where T: Base<Int>, T: Derived {}