Skip to content

Commit 9a5d6fe

Browse files
authored
Merge pull request #26872 from DougGregor/circular-inheritance-spin
2 parents 3676568 + 1ee6d81 commit 9a5d6fe

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1629,11 +1629,18 @@ ConstraintSystem::matchSuperclassTypes(Type type1, Type type2,
16291629
TypeMatchOptions subflags = getDefaultDecompositionOptions(flags);
16301630

16311631
auto classDecl2 = type2->getClassOrBoundGenericClass();
1632+
SmallPtrSet<ClassDecl *, 4> superclasses1;
16321633
for (auto super1 = type1->getSuperclass();
16331634
super1;
16341635
super1 = super1->getSuperclass()) {
1635-
if (super1->getClassOrBoundGenericClass() != classDecl2)
1636+
auto superclass1 = super1->getClassOrBoundGenericClass();
1637+
if (superclass1 != classDecl2) {
1638+
// Break if we have circular inheritance.
1639+
if (superclass1 && !superclasses1.insert(superclass1).second)
1640+
break;
1641+
16361642
continue;
1643+
}
16371644

16381645
return matchTypes(super1, type2, ConstraintKind::Bind,
16391646
subflags, locator);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// rdar://problem/54296278 - infinite loop
4+
class Foo {}
5+
class Bar: Bar {} // expected-error{{'Bar' inherits from itself}}
6+
func foo(_ o: AnyObject) -> Foo? {
7+
return o as? Bar // expected-error{{cannot convert return expression of type 'Bar?' to return type 'Foo?'}}
8+
}

0 commit comments

Comments
 (0)