Skip to content

Commit 678e564

Browse files
authored
Merge pull request #17662 from hamishknight/bad-inheritance-crash
[Sema] Fix crash on bad inheritance clause
2 parents 84f17e1 + 40fada7 commit 678e564

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/Sema/TypeCheckAccess.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,8 @@ void AccessControlChecker::check(Decl *D) {
956956
return false;
957957
Type ty = inherited.getType();
958958
if (ty->is<ProtocolCompositionType>())
959-
ty = ty->getExistentialLayout().superclass;
959+
if (auto superclass = ty->getExistentialLayout().superclass)
960+
ty = superclass;
960961
return ty->getAnyNominal() == superclassDecl;
961962
});
962963
// Sanity check: we couldn't find the superclass for whatever reason
@@ -1475,7 +1476,8 @@ void UsableFromInlineChecker::check(Decl *D) {
14751476
return false;
14761477
Type ty = inherited.getType();
14771478
if (ty->is<ProtocolCompositionType>())
1478-
ty = ty->getExistentialLayout().superclass;
1479+
if (auto superclass = ty->getExistentialLayout().superclass)
1480+
ty = superclass;
14791481
return ty->getAnyNominal() == superclassDecl;
14801482
});
14811483
// Sanity check: we couldn't find the superclass for whatever reason

test/decl/inherit/inherit.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -swift-version 5
22

3-
class A { }
4-
protocol P { }
3+
public class A { }
4+
public protocol P { }
5+
public protocol P1 { }
56

67
// Duplicate inheritance
78
class B : A, A { } // expected-error{{duplicate inheritance from 'A'}}{{12-15=}}
@@ -18,6 +19,17 @@ class C : B, A { } // expected-error{{multiple inheritance from classes 'B' and
1819
// Superclass in the wrong position
1920
class D : P, A { } // expected-error{{superclass 'A' must appear first in the inheritance clause}}{{12-15=}}{{11-11=A, }}
2021

22+
// SR-8160
23+
class D1 : Any, A { } // expected-error{{superclass 'A' must appear first in the inheritance clause}}{{15-18=}}{{12-12=A, }}
24+
25+
class D2 : P & P1, A { } // expected-error{{superclass 'A' must appear first in the inheritance clause}}{{18-21=}}{{12-12=A, }}
26+
27+
@usableFromInline
28+
class D3 : Any, A { } // expected-error{{superclass 'A' must appear first in the inheritance clause}}{{15-18=}}{{12-12=A, }}
29+
30+
@usableFromInline
31+
class D4 : P & P1, A { } // expected-error{{superclass 'A' must appear first in the inheritance clause}}{{18-21=}}{{12-12=A, }}
32+
2133
// Struct inheriting a class
2234
struct S : A { } // expected-error{{non-class type 'S' cannot inherit from class 'A'}}
2335

0 commit comments

Comments
 (0)