Skip to content

Commit 9fca165

Browse files
authored
Merge pull request #26875 from DougGregor/circular-inheritance-spin-5.1
[5.1] [Constraint solver] Cope with circular inheritance.
2 parents fcc37cd + a94770e commit 9fca165

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
@@ -1554,11 +1554,18 @@ ConstraintSystem::matchSuperclassTypes(Type type1, Type type2,
15541554
TypeMatchOptions subflags = getDefaultDecompositionOptions(flags);
15551555

15561556
auto classDecl2 = type2->getClassOrBoundGenericClass();
1557+
SmallPtrSet<ClassDecl *, 4> superclasses1;
15571558
for (auto super1 = type1->getSuperclass();
15581559
super1;
15591560
super1 = super1->getSuperclass()) {
1560-
if (super1->getClassOrBoundGenericClass() != classDecl2)
1561+
auto superclass1 = super1->getClassOrBoundGenericClass();
1562+
if (superclass1 != classDecl2) {
1563+
// Break if we have circular inheritance.
1564+
if (superclass1 && !superclasses1.insert(superclass1).second)
1565+
break;
1566+
15611567
continue;
1568+
}
15621569

15631570
return matchTypes(super1, type2, ConstraintKind::Bind,
15641571
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)