mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
One difficulty in generating reasonable diagnostic data for type check failures has been the fact that many constraints had been synthesized without regard for where they were rooted in the program source. The result of this was that even though we would store failure information for specific constraints, we wouldn't emit it for lack of a source location. By making location data a non-optional component of constraints, we can begin diagnosing type check errors closer to their point of failure. Swift SVN r18751
138 lines
2.6 KiB
Swift
138 lines
2.6 KiB
Swift
// RUN: %swift -parse -verify %s
|
|
|
|
operator infix +++ {}
|
|
|
|
protocol ConcatToAnything {
|
|
@infix func +++ <T>(lhs: Self, other: T)
|
|
}
|
|
|
|
func min<T : Comparable>(x: T, y: T) -> T {
|
|
if y < x { return y }
|
|
return x
|
|
}
|
|
|
|
func weirdConcat<T : ConcatToAnything, U>(t: T, u: U) {
|
|
t +++ u
|
|
t +++ 1
|
|
u +++ t // expected-error{{type 'U' does not conform to protocol 'ConcatToAnything'}}
|
|
}
|
|
|
|
// Make sure that the protocol operators don't get in the way.
|
|
var b1, b2 : Bool
|
|
b1 != b2
|
|
|
|
extension UnicodeScalar {
|
|
func isAlpha2() -> Bool {
|
|
return (self >= "A" && self <= "Z") || (self >= "a" && self <= "z")
|
|
}
|
|
}
|
|
|
|
protocol P {
|
|
class func foo(arg: Self) -> Self
|
|
}
|
|
struct S : P {
|
|
static func foo(arg: S) -> S {
|
|
return arg
|
|
}
|
|
}
|
|
|
|
func foo<T : P>(arg: T) -> T {
|
|
return T.foo(arg)
|
|
}
|
|
|
|
// Associated types and metatypes
|
|
protocol SomeProtocol {
|
|
typealias SomeAssociated
|
|
}
|
|
|
|
func generic_metatypes<T : SomeProtocol>(x: T)
|
|
-> (T.Type, T.SomeAssociated.Type)
|
|
{
|
|
return (x.dynamicType, x.dynamicType.SomeAssociated.self)
|
|
}
|
|
|
|
// Inferring a variable's type from a call to a generic.
|
|
struct Pair<T, U> { }
|
|
|
|
func pair<T, U>(x: T, y: U) -> Pair<T, U> { }
|
|
|
|
var i : Int, f : Float
|
|
var p = pair(i, f)
|
|
|
|
// Conformance constraints on static variables.
|
|
func f1<S1 : Sequence>(s1: S1) {}
|
|
var x : Array<Int> = [1]
|
|
f1(x)
|
|
|
|
// Inheritance involving generics and non-generics.
|
|
class X {
|
|
func f() {}
|
|
}
|
|
|
|
class Foo<T> : X {
|
|
func g() { }
|
|
}
|
|
|
|
class Y<U> : Foo<Int> {
|
|
}
|
|
|
|
func genericAndNongenericBases(x: Foo<Int>, y: Y<()>) {
|
|
x.f()
|
|
y.f()
|
|
y.g()
|
|
}
|
|
|
|
func genericAndNongenericBasesTypeParameter<T : Y<()>>(t: T) {
|
|
t.f()
|
|
t.g()
|
|
}
|
|
|
|
protocol P1 {}
|
|
protocol P2 {}
|
|
|
|
func foo<T : P1>(t: T) -> P2 {
|
|
return t // expected-error{{type 'T' does not conform to protocol 'P2'}}
|
|
}
|
|
|
|
func foo2(p1: P1) -> P2 {
|
|
return p1 // expected-error{{type 'P1' does not conform to protocol 'P2'}}
|
|
}
|
|
|
|
// <rdar://problem/14005696>
|
|
protocol BinaryMethodWorkaround {
|
|
typealias MySelf
|
|
}
|
|
|
|
protocol Squigglable : BinaryMethodWorkaround {
|
|
}
|
|
|
|
operator infix ~~~ { }
|
|
|
|
func ~~~ <T : Squigglable where T.MySelf == T>(lhs: T, rhs: T) -> Bool {
|
|
return true
|
|
}
|
|
|
|
extension UInt8 : Squigglable {
|
|
typealias MySelf = UInt8
|
|
}
|
|
|
|
var rdar14005696 : UInt8
|
|
rdar14005696 ~~~ 5
|
|
|
|
// <rdar://problem/15168483>
|
|
func f1<
|
|
S: Collection
|
|
where S.IndexType: BidirectionalIndex
|
|
>(seq: S) {
|
|
let x = reverse(indices(seq))
|
|
PermutationGenerator(elements: seq, indices: x)
|
|
PermutationGenerator(elements: seq, indices: reverse(indices(seq)))
|
|
}
|
|
|
|
// <rdar://problem/16078944>
|
|
func count16078944<C: Collection>(x: C) -> Int { return 0 }
|
|
|
|
func test16078944 <T: ForwardIndex>(lhs: T, args: T) -> Int {
|
|
return count16078944(lhs..args) // don't crash
|
|
}
|