Skip to content

Fix #7840: Don't instantiateSelected to Nothing #7845

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 4 commits into from
Dec 25, 2019
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 @@ -3930,6 +3930,10 @@ object Types {
def hasLowerBound(implicit ctx: Context): Boolean =
!ctx.typerState.constraint.entry(origin).loBound.isBottomType

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

/** Unwrap to instance (if instantiated) or origin (if not), until result
* is no longer a TypeVar
*/
Expand Down
9 changes: 4 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object Inferencing {
/** Instantiate selected type variables `tvars` in type `tp` */
def instantiateSelected(tp: Type, tvars: List[Type])(implicit ctx: Context): Unit =
if (tvars.nonEmpty)
new IsFullyDefinedAccumulator(new ForceDegree.Value(tvars.contains, minimizeAll = true)).process(tp)
new IsFullyDefinedAccumulator(new ForceDegree.Value(tvars.contains, minimizeAll = true, allowBottom = false)).process(tp)

/** Instantiate any type variables in `tp` whose bounds contain a reference to
* one of the parameters in `tparams` or `vparamss`.
Expand Down Expand Up @@ -108,10 +108,9 @@ object Inferencing {
&& ctx.typerState.constraint.contains(tvar)
&& {
val direction = instDirection(tvar.origin)
def avoidBottom =
!force.allowBottom &&
defn.isBottomType(ctx.typeComparer.approximation(tvar.origin, fromBelow = true))
def preferMin = force.minimizeAll || variance >= 0 && !avoidBottom
def preferMin =
force.minimizeAll && (tvar.hasLowerBound || !tvar.hasUpperBound)
|| variance >= 0 && (force.allowBottom || tvar.hasLowerBound)
if (direction != 0) instantiate(tvar, direction < 0)
else if (preferMin) instantiate(tvar, fromBelow = true)
else toMaximize = true
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/i1802.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object Exception {
def apply(x: Throwable): T = f(downcast(x).get)
}

def mkThrowableCatcher[T](isDef: Throwable => Boolean, f: Throwable => T) = mkCatcher(isDef, f) // error: undetermined ClassTag
def mkThrowableCatcher[T](isDef: Throwable => Boolean, f: Throwable => T) = mkCatcher(isDef, f)

implicit def throwableSubtypeToCatcher[Ex <: Throwable: ClassTag, T](pf: PartialFunction[Ex, T]) = // error: result type needs to be given
mkCatcher(pf.isDefinedAt _, pf.apply _) // error: method needs return type
Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i7840.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
object Example extends App {

trait ZSink[-R, +E, +A0, -A, +B] {
def flatMap[R1 <: R, E1 >: E, A00 >: A0, A1 <: A, C](
f: B => ZSink[R1, E1, A00, A1, C]
)(implicit ev: A00 =:= A1, e2: A1 =:= A00): ZSink[R1, E1, A00, A1, C] =
???
}

object ZSink {
def identity[A]: ZSink[Any, Unit, Nothing, A, A] = ???
def succeed[A, B](b: B): ZSink[Any, Nothing, A, A, B] = ???
}

// cannot prove that Int =:= Nothing
ZSink.identity[Int].flatMap(n => ZSink.succeed[Int, String](n.toString))
}