Skip to content

Fix #2982: Avoid instantiating to Nothing in some situations #3834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3327,6 +3327,10 @@ object Types {
def instantiate(fromBelow: Boolean)(implicit ctx: Context): Type =
instantiateWith(ctx.typeComparer.instanceType(origin, fromBelow))

/** For uninstantiated type variables: Is the lower bound different from Nothing? */
def hasLowerBound(implicit ctx: Context) =
!ctx.typerState.constraint.entry(origin).loBound.isBottomType

/** Unwrap to instance (if instantiated) or origin (if not), until result
* is no longer a TypeVar
*/
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ trait Inferencing { this: Typer =>
if (!(vs contains tvar) && qualifies(tvar)) {
typr.println(s"instantiating non-occurring ${tvar.show} in ${tp.show} / $tp")
ensureConstrained()
tvar.instantiate(fromBelow = true)
tvar.instantiate(fromBelow = tvar.hasLowerBound)
}
}
if (constraint.uninstVars exists qualifies) interpolate()
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ trait TypeAssigner {
case tp: SkolemType if partsToAvoid(mutable.Set.empty, tp.info).nonEmpty =>
range(tp.info.bottomType, apply(tp.info))
case tp: TypeVar if ctx.typerState.constraint.contains(tp) =>
val lo = ctx.typeComparer.instanceType(tp.origin, fromBelow = variance >= 0)
val lo = ctx.typeComparer.instanceType(
tp.origin, fromBelow = variance > 0 || variance == 0 && tp.hasLowerBound)
val lo1 = apply(lo)
if (lo1 ne lo) lo1 else tp
case _ =>
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-tailcall/tailrec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Failures {
}

// unsafe
@tailrec final def fail3[T](x: Int): Int = fail3(x - 1)
@tailrec final def fail3[T](x: Int): Int = fail3(x - 1) // error // error: recursive application has different type arguments

// unsafe
class Tom[T](x: Int) {
Expand Down
10 changes: 4 additions & 6 deletions tests/pos/i2982.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

object A {
def fun[E >: B](a: A[E]): E => Unit = ???
val x = fun(new A[C])
def fun[E](a: A[E]): Unit = ()
fun(new A[Int])
}
class B extends C
class C

class A[-X >: B]
class A[-X]