-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer: Don't minimise to Nothing if there's an upper bound
- Loading branch information
Showing
4 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
compiler/test/dotty/tools/dotc/typer/InstantiateModel.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package dotty.tools | ||
package dotc | ||
package typer | ||
|
||
// Modelling the decision in IsFullyDefined | ||
object InstantiateModel: | ||
enum LB { case NN; case LL; case L1 }; import LB.* | ||
enum UB { case AA; case UU; case U1 }; import UB.* | ||
enum Var { case V; case NotV }; import Var.* | ||
enum MSe { case M; case NotM }; import MSe.* | ||
enum Bot { case Fail; case Ok; case Flip }; import Bot.* | ||
enum Act { case Min; case Max; case ToMax; case Skip; case False }; import Act.* | ||
|
||
// NN/AA = Nothing/Any | ||
// LL/UU = the original bounds, on the type parameter | ||
// L1/U1 = the constrained bounds, on the type variable | ||
// V = variance >= 0 ("non-contravariant") | ||
// MSe = minimisedSelected | ||
// Bot = IfBottom | ||
// ToMax = delayed maximisation, via addition to toMaximize | ||
// Skip = minimisedSelected "hold off instantiating" | ||
// False = return false | ||
|
||
// there are 9 combinations: | ||
// # | LB | UB | d | // d = direction | ||
// --+----+----+---+ | ||
// 1 | L1 | AA | - | L1 <: T | ||
// 2 | L1 | UU | - | L1 <: T <: UU | ||
// 3 | LL | U1 | + | LL <: T <: U1 | ||
// 4 | NN | U1 | + | T <: U1 | ||
// 5 | L1 | U1 | 0 | L1 <: T <: U1 | ||
// 6 | LL | UU | 0 | LL <: T <: UU | ||
// 7 | LL | AA | 0 | LL <: T | ||
// 8 | NN | UU | 0 | T <: UU | ||
// 9 | NN | AA | 0 | T | ||
|
||
def decide(lb: LB, ub: UB, v: Var, bot: Bot, m: MSe): Act = (lb, ub) match | ||
case (L1, AA) => Min | ||
case (L1, UU) => Min | ||
case (LL, U1) => Max | ||
case (NN, U1) => Max | ||
|
||
case (L1, U1) => if m==M || v==V then Min else ToMax | ||
case (LL, UU) => if m==M || v==V then Min else ToMax | ||
case (LL, AA) => if m==M || v==V then Min else ToMax | ||
|
||
case (NN, UU) => bot match | ||
case _ if m==M => Max | ||
//case Ok if v => Min // removed | ||
case Fail if v==V => False | ||
case _ => ToMax | ||
|
||
case (NN, AA) => bot match | ||
case _ if m==M => Skip | ||
case Ok if v==V => Min | ||
case Fail if v==V => False | ||
case _ => ToMax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Pet | ||
class Cat extends Pet | ||
|
||
class Z1[ S2 <: Pet](val fn: S2 => Unit) // the case: given an upper bound | ||
class Z2[ S1 ](val fn: S1 => Unit) // the counter-example: works without an upper bound | ||
class Z3[-S3 <: Pet](val fn: S3 => Unit) | ||
|
||
abstract class Test: | ||
def test = | ||
val r1 = new Z1((_: Pet) => ()); eat[Z1[Pet]](r1) // the case: using the parameter bound in situ infers Z[Nothing] | ||
val r2 = new Z2((_: Pet) => ()); eat[Z2[Pet]](r2) // counter-example: infers as desired without an upper bound | ||
val r3 = new Z3((_: Pet) => ()); eat[Z3[Pet]](r3) // workaround: declare it contravariant | ||
val r4 = new Z1((_: Cat) => ()); eat[Z1[Cat]](r4) // counter-example: infers as desired with a subtype | ||
|
||
def eat[T](x: T): Unit |