Files
swift-mirror/test/decl/ext/extensions.swift
Doug Gregor 3d77855b31 Start allowing extensions of protocol types.
Remove the semantic restrictions that prohibited extensions of
protocol types, and start making some systematic changes so that
protocol extensions start to make sense:
  - Replace a lot of occurrences of isa<ProtocolDecl> and
    dyn_cast<ProtocolDecl> on DeclContexts to use the new
    DeclContext::isProtocolOrProtocolExtensionContext(), where we want
    that behavior to apply equally to protocols and protocol extensions.
  - Eliminate ProtocolDecl::getSelf() in favor of
    DeclContext::getProtocolSelf(), which produces the appropriate
    generic type parameter for the 'Self' of a protocol or protocol
    extension. Update all of the callers of ProtocolDecl::getSelf()
    appropriately.
  - Update extension validation to appropriately form generic
    parameter lists for protocol extensions.
  - Methods in protocol extensions always use the witnesscc calling
  convention.

At this point, we can type check and SILGen very basic definitions of
protocol extensions with methods that can call protocol requirements,
generic free functions, and other methods within the same protocol
extension.

Regresses four compiler crashers but improves three compiler
crashers... we'll call that "progress"; the four regressions all hit
the same assertion in the constraint system that will likely be
addressed as protocol extensions starts working.

Swift SVN r26579
2015-03-26 04:50:51 +00:00

91 lines
2.8 KiB
Swift

// RUN: %target-parse-verify-swift
extension extension_for_invalid_type_1 { // expected-error {{use of undeclared type 'extension_for_invalid_type_1'}}
func f() { }
}
extension extension_for_invalid_type_2 { // expected-error {{use of undeclared type 'extension_for_invalid_type_2'}}
static func f() { }
}
extension extension_for_invalid_type_3 { // expected-error {{use of undeclared type 'extension_for_invalid_type_3'}}
init() {}
}
extension extension_for_invalid_type_4 { // expected-error {{use of undeclared type 'extension_for_invalid_type_4'}}
deinit {} // expected-error {{deinitializers may only be declared within a class}}
}
extension extension_for_invalid_type_5 { // expected-error {{use of undeclared type 'extension_for_invalid_type_5'}}
typealias X = Int
}
//===--- Test that we only allow extensions at file scope.
extension NestingTest1 { // expected-error {{use of undeclared type 'NestingTest1'}}
extension Foo {} // expected-error {{declaration is only valid at file scope}}
}
struct NestingTest2 {
extension Foo {} // expected-error {{declaration is only valid at file scope}}
}
class NestingTest3 {
extension Foo {} // expected-error {{declaration is only valid at file scope}}
}
enum NestingTest4 {
extension Foo {} // expected-error {{declaration is only valid at file scope}}
}
protocol NestingTest5 {
extension Foo {} // expected-error {{declaration is only valid at file scope}}
}
func nestingTest6() {
extension Foo {} // expected-error {{declaration is only valid at file scope}}
}
//===--- Test that we only allow extensions only for nominal types.
struct S1 {
struct NestedStruct {}
}
extension S1 {} // no-error
extension S1.Type {} // expected-error {{cannot extend a metatype of a type}}
extension S1.NestedStruct {} // no-error
typealias TA_S1 = S1
extension TA_S1 {} // no-error
typealias TA_S1_NestedStruct = S1.NestedStruct
extension TA_S1_NestedStruct {} // no-error
enum U1 {
struct NestedStruct {}
}
extension U1 {} // no-error
extension U1.NestedStruct {} // no-error
class C1 {
struct NestedStruct {}
}
extension C1 {} // no-error
extension C1.NestedStruct {} // no-error
protocol P1 {}
protocol P2 {}
extension () {} // expected-error {{expected identifier in extension declaration}} expected-error{{type of expression is ambiguous without more context}}
// expected-error @-1{{braced block of statements is an unused closure}}
// expected-error @-2{{cannot begin with a closure expression}}
// expected-note @-3{{explicitly discard the result}}
typealias TupleAlias = (x: Int, y: Int)
extension TupleAlias {} // expected-error{{non-nominal type 'TupleAlias' cannot be extended}}
// Test property accessors in extended types
class C {}
extension C {
var p1: Int {
get {return 1}
set(v) {}
}
}
var c = C()
var x = c.p1
c.p1 = 1