Skip to content

[Sema] Fix crash on bad inheritance clause #17662

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 1 commit into from
Jul 2, 2018
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
6 changes: 4 additions & 2 deletions lib/Sema/TypeCheckAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,8 @@ void AccessControlChecker::check(Decl *D) {
return false;
Type ty = inherited.getType();
if (ty->is<ProtocolCompositionType>())
ty = ty->getExistentialLayout().superclass;
if (auto superclass = ty->getExistentialLayout().superclass)
ty = superclass;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s another copy of this check in the UsableFromInlineChecker too. I know that’s bad and I need to clean it up, but can you fix it and add a test?

return ty->getAnyNominal() == superclassDecl;
});
// Sanity check: we couldn't find the superclass for whatever reason
Expand Down Expand Up @@ -1475,7 +1476,8 @@ void UsableFromInlineChecker::check(Decl *D) {
return false;
Type ty = inherited.getType();
if (ty->is<ProtocolCompositionType>())
ty = ty->getExistentialLayout().superclass;
if (auto superclass = ty->getExistentialLayout().superclass)
ty = superclass;
return ty->getAnyNominal() == superclassDecl;
});
// Sanity check: we couldn't find the superclass for whatever reason
Expand Down
18 changes: 15 additions & 3 deletions test/decl/inherit/inherit.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -swift-version 5

class A { }
protocol P { }
public class A { }
public protocol P { }
public protocol P1 { }

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

// SR-8160
class D1 : Any, A { } // expected-error{{superclass 'A' must appear first in the inheritance clause}}{{15-18=}}{{12-12=A, }}

class D2 : P & P1, A { } // expected-error{{superclass 'A' must appear first in the inheritance clause}}{{18-21=}}{{12-12=A, }}

@usableFromInline
class D3 : Any, A { } // expected-error{{superclass 'A' must appear first in the inheritance clause}}{{15-18=}}{{12-12=A, }}

@usableFromInline
class D4 : P & P1, A { } // expected-error{{superclass 'A' must appear first in the inheritance clause}}{{18-21=}}{{12-12=A, }}

// Struct inheriting a class
struct S : A { } // expected-error{{non-class type 'S' cannot inherit from class 'A'}}

Expand Down