Files
swift-mirror/test/Constraints/ranking.swift
Doug Gregor 91ffa957b9 Add very basic support for selecting the best solution when the
constraint solver produces multiple solutions. The criteria for "best"
are fairly simple: we compare the bindings for each type variable
bound in both solutions:

  - If the binding in the first solution is a subtype of the binding
    in the second solution, but not vice-versa, the first solution is
    at least as good as the second
  - If the type variable has a literal constraint on it, and the bound
    type in the first solution matches the default literal type for
    that literal kind, the first solution is as good as the second.

From there, we follow the obvious rules for picking a winner: if some
of the type variables in the first solution are as good as the same type
variables in the second solution, but none of the type variables are
worse, then the first solution is better.

We can now type-check "1 + 2.0". Take *that*, TypeCheckCoercion.cpp.



Swift SVN r2894
2012-09-21 18:31:25 +00:00

43 lines
1.1 KiB
Swift

// RUN: %swift -parse -verify -constraint-checker -debug-constraints %s > %t
// RUN: FileCheck %s < %t
func f0<T>(x : T) {}
// FIXME: Lookup breaks if these come after f1!
class A { };
class B : A { }
func f1(a : A) -> A { return a }
func f1(b : B) -> B { return b }
// CHECK: ---Child system #1---
// CHECK: Assumptions:
// CHECK: selected overload set #0 choice #0 for f1: $T1 == (a : A) -> A
// CHECK: assuming $T0 == A
// CHECK: Type Variables:
// CHECK: $T0 as A
// CHECK: $T1 as $T3 -> $T4
// CHECK: $T2 as ()
// CHECK: $T3 as (a : A)
// CHECK: $T4 as A
// CHECK: $T5 as (x : $T0)
// CHECK: $T6 as ()
// CHECK: SOLVED (completely)
// CHECK: ---Child system #2 (best)---
// CHECK: Assumptions:
// CHECK: selected overload set #0 choice #1 for f1: $T1 == (b : B) -> B
// CHECK: assuming $T0 == B
// CHECK: Type Variables:
// CHECK: $T0 as B
// CHECK: $T1 as $T3 -> $T4
// CHECK: $T2 as ()
// CHECK: $T3 as (b : B)
// CHECK: $T4 as B
// CHECK: $T5 as (x : $T0)
// CHECK: $T6 as ()
// CHECK: SOLVED (completely)
// CHECK: SOLVED (completely)
f0(f1(new B()))