mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
If the -enable-experimental-subclass-existentials staging flag is on, resolveType() now allows protocol compositions to contain class types. It also diagnoses if a composition has more than one superclass requirement. Also, change diagnostics that talked about 'protocol composition' to 'protocol-constrained type'. Since such types can now contain a superclass constraint, it's not correct to call them protocol composition. "Protocol-constrained type" isn't quite accurate either because 'Any' has no protocols, and 'AnyObject' will have no protocols but a general class constraint; but those are edge cases which won't come up in these diagnostics.
54 lines
2.5 KiB
Swift
54 lines
2.5 KiB
Swift
// RUN: %target-typecheck-verify-swift
|
|
|
|
class A { }
|
|
protocol P { }
|
|
|
|
// Duplicate inheritance
|
|
class B : A, A { } // expected-error{{duplicate inheritance from 'A'}}{{12-15=}}
|
|
|
|
// Duplicate inheritance from protocol
|
|
class B2 : P, P { } // expected-error{{duplicate inheritance from 'P'}}{{13-16=}}
|
|
|
|
// Multiple inheritance
|
|
class C : B, A { } // expected-error{{multiple inheritance from classes 'B' and 'A'}}
|
|
|
|
// 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, }}
|
|
|
|
// Struct inheriting a class
|
|
struct S : A { } // expected-error{{non-class type 'S' cannot inherit from class 'A'}}
|
|
|
|
// Protocol inheriting a class
|
|
protocol Q : A { } // expected-error{{non-class type 'Q' cannot inherit from class 'A'}}
|
|
|
|
// Extension inheriting a class
|
|
extension C : A { } // expected-error{{extension of type 'C' cannot inherit from class 'A'}}
|
|
|
|
// Keywords in inheritance clauses
|
|
struct S2 : struct { } // expected-error{{expected identifier for type name}}
|
|
|
|
// Protocol composition in inheritance clauses
|
|
struct S3 : P, P & Q { } // expected-error {{duplicate inheritance from 'P'}}
|
|
// expected-error @-1 {{protocol-constrained type is neither allowed nor needed here}}
|
|
// expected-error @-2 {{'Q' requires that 'S3' inherit from 'A'}}
|
|
// expected-note @-3 {{requirement specified as 'Self' : 'A' [with Self = S3]}}
|
|
struct S4 : P, P { } // expected-error {{duplicate inheritance from 'P'}}
|
|
struct S6 : P & { } // expected-error {{expected identifier for type name}}
|
|
// expected-error @-1 {{protocol-constrained type is neither allowed nor needed here}}
|
|
struct S7 : protocol<P, Q> { } // expected-warning {{'protocol<...>' composition syntax is deprecated; join the protocols using '&'}}
|
|
// expected-error @-1 {{protocol-constrained type is neither allowed nor needed here}}{{13-22=}} {{26-27=}}
|
|
// expected-error @-2 {{'Q' requires that 'S7' inherit from 'A'}}
|
|
// expected-note @-3 {{requirement specified as 'Self' : 'A' [with Self = S7]}}
|
|
|
|
class GenericBase<T> {}
|
|
|
|
class GenericSub<T> : GenericBase<T> {} // okay
|
|
|
|
class InheritGenericParam<T> : T {} // expected-error {{inheritance from non-protocol, non-class type 'T'}}
|
|
class InheritBody : T { // expected-error {{use of undeclared type 'T'}}
|
|
typealias T = A
|
|
}
|
|
class InheritBodyBad : fn { // expected-error {{use of undeclared type 'fn'}}
|
|
func fn() {}
|
|
}
|