Skip to content

Refrain from instantiating type variables to undetermined types #12294

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
Apr 30, 2021
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
29 changes: 25 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ object Types {
/** Is this a higher-kinded type lambda with given parameter variances? */
def isDeclaredVarianceLambda: Boolean = false

/** Does this type contain wildcard types? */
final def containsWildcardTypes(using Context) =
existsPart(_.isInstanceOf[WildcardType], stopAtStatic = true)

// ----- Higher-order combinators -----------------------------------

/** Returns true if there is a part of this type that satisfies predicate `p`.
Expand Down Expand Up @@ -4461,13 +4465,30 @@ object Types {
def instantiate(fromBelow: Boolean)(using Context): Type =
instantiateWith(avoidCaptures(TypeComparer.instanceType(origin, fromBelow)))

/** For uninstantiated type variables: the entry in the constraint (either bounds or
* provisional instance value)
*/
private def currentEntry(using Context): Type = ctx.typerState.constraint.entry(origin)

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

/** For uninstantiated type variables: Is the upper bound different from Any? */
def hasUpperBound(using Context): Boolean =
!ctx.typerState.constraint.entry(origin).hiBound.isRef(defn.AnyClass)
def hasUpperBound(using Context): Boolean = !currentEntry.hiBound.isRef(defn.AnyClass)

/** For uninstantiated type variables: Is the lower bound different from Nothing and
* does it not contain wildcard types?
*/
def hasNonWildcardLowerBound(using Context): Boolean =
val lo = currentEntry.loBound
!lo.isExactlyNothing && !lo.containsWildcardTypes

/** For uninstantiated type variables: Is the upper bound different from Any and
* does it not contain wildcard types?
*/
def hasNonWildcardUpperBound(using Context): Boolean =
val hi = currentEntry.hiBound
!hi.isRef(defn.AnyClass) && !hi.containsWildcardTypes

/** Unwrap to instance (if instantiated) or origin (if not), until result
* is no longer a TypeVar
Expand Down
12 changes: 7 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@ object Inferencing {
&& ctx.typerState.constraint.contains(tvar)
&& {
val direction = instDirection(tvar.origin)
if direction != 0 then
if minimizeSelected then
if direction <= 0 && tvar.hasNonWildcardLowerBound then
instantiate(tvar, fromBelow = true)
else if direction >= 0 && tvar.hasNonWildcardUpperBound then
instantiate(tvar, fromBelow = false)
// else hold off instantiating unbounded unconstrained variable
else if direction != 0 then
instantiate(tvar, fromBelow = direction < 0)
else if minimizeSelected then
if tvar.hasLowerBound then instantiate(tvar, fromBelow = true)
else if tvar.hasUpperBound then instantiate(tvar, fromBelow = false)
else () // hold off instantiating unbounded unconstrained variables
else if variance >= 0 && (force.ifBottom == IfBottom.ok || tvar.hasLowerBound) then
instantiate(tvar, fromBelow = true)
else if variance >= 0 && force.ifBottom == IfBottom.fail then
Expand Down
27 changes: 27 additions & 0 deletions tests/pos/i12247.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
sealed abstract class CtorType
object CtorType {
final class Props extends CtorType
sealed trait Summoner { type CT <: CtorType }
implicit def summonP: Summoner {type CT = Props} = ???
}

final case class Builder() {
def build(using ctorType: CtorType.Summoner): Component[ctorType.CT] = ???
}

final class Component[CT <: CtorType]

object Test {

def assertTypeOf[A](a: => A) = new TestDsl[A]
class TestDsl[A] {
def is[B](implicit ev: A =:= B): Unit = ()
}

type Expect = Component[CtorType.Props]

assertTypeOf( Builder().build ).is[Expect] // error

val x = Builder().build
assertTypeOf(x).is[Expect] // ok
}