Files
swift-mirror/test/Generics/requirement_inference.swift
Doug Gregor 28cc919fb4 Infer conformance requirements from the signature of a generic function.
When a specialization of a generic type occurs within the signature of
a generic function, it implies that the generic arguments meet the
requirements of the generic type. For example, in

  func printHashes<K, V>(dict : Dictionary<K, V>) {
    for (k, v) in dict {
        print("\(k.hashValue())\n")
    } 
  } 

the presence of Dictionary<K, V> in the signature implies that K and V
meet the requirements on Dictionary's generic parameters, i.e., that K
is Hashable. Thus, infer that K is Hashable in printHashes().

Fixes the easy part of <rdar://problem/14691708>. Same-type and
superclass requirements are more interesting.


<rdar://problem/14691708>


Swift SVN r7574
2013-08-26 16:46:38 +00:00

48 lines
946 B
Swift

// RUN: %swift -parse %s -verify
protocol P1 {
func p1()
}
struct X1<T : P1> {
func getT() -> T { }
}
class X2<T : P1> {
func getT() -> T { }
}
class X3 { }
struct X4<T : X3> {
func getT() -> T { }
}
// Infer protocol requirements from the parameter type of a generic function.
func inferFromParameterType<T>(x : X1<T>) {
x.getT().p1()
}
// Infer protocol requirements from the return type of a generic function.
func inferFromReturnType<T>(x : T) -> X1<T> {
x.p1()
}
// Infer protocol requirements from the superclass of a generic parameter.
func inferFromSuperclass<T, U : X2<T>>(t : T, u : U) -> T {
t.p1()
}
// Infer protocol requirements from the parameter type of a constructor.
struct InferFromConstructor {
constructor<T> (x : X1<T>) {
x.getT().p1()
}
}
// FIXME: Infer superclass requirements.
// FIXME: Infer same-type requirements, which requires us to process
// the where clause in its entirety.