Skip to content

Commit d13efef

Browse files
authored
Merge pull request #7845 from dotty-staging/fix-#7840
Fix #7840: Don't instantiateSelected to Nothing
2 parents 77ec689 + 748da85 commit d13efef

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3930,6 +3930,10 @@ object Types {
39303930
def hasLowerBound(implicit ctx: Context): Boolean =
39313931
!ctx.typerState.constraint.entry(origin).loBound.isBottomType
39323932

3933+
/** For uninstantiated type variables: Is the upper bound different from Any? */
3934+
def hasUpperBound(implicit ctx: Context): Boolean =
3935+
!ctx.typerState.constraint.entry(origin).hiBound.isRef(defn.AnyClass)
3936+
39333937
/** Unwrap to instance (if instantiated) or origin (if not), until result
39343938
* is no longer a TypeVar
39353939
*/

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ object Inferencing {
4646
/** Instantiate selected type variables `tvars` in type `tp` */
4747
def instantiateSelected(tp: Type, tvars: List[Type])(implicit ctx: Context): Unit =
4848
if (tvars.nonEmpty)
49-
new IsFullyDefinedAccumulator(new ForceDegree.Value(tvars.contains, minimizeAll = true)).process(tp)
49+
new IsFullyDefinedAccumulator(new ForceDegree.Value(tvars.contains, minimizeAll = true, allowBottom = false)).process(tp)
5050

5151
/** Instantiate any type variables in `tp` whose bounds contain a reference to
5252
* one of the parameters in `tparams` or `vparamss`.
@@ -108,10 +108,9 @@ object Inferencing {
108108
&& ctx.typerState.constraint.contains(tvar)
109109
&& {
110110
val direction = instDirection(tvar.origin)
111-
def avoidBottom =
112-
!force.allowBottom &&
113-
defn.isBottomType(ctx.typeComparer.approximation(tvar.origin, fromBelow = true))
114-
def preferMin = force.minimizeAll || variance >= 0 && !avoidBottom
111+
def preferMin =
112+
force.minimizeAll && (tvar.hasLowerBound || !tvar.hasUpperBound)
113+
|| variance >= 0 && (force.allowBottom || tvar.hasLowerBound)
115114
if (direction != 0) instantiate(tvar, direction < 0)
116115
else if (preferMin) instantiate(tvar, fromBelow = true)
117116
else toMaximize = true

tests/neg/i1802.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object Exception {
1414
def apply(x: Throwable): T = f(downcast(x).get)
1515
}
1616

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

1919
implicit def throwableSubtypeToCatcher[Ex <: Throwable: ClassTag, T](pf: PartialFunction[Ex, T]) = // error: result type needs to be given
2020
mkCatcher(pf.isDefinedAt _, pf.apply _) // error: method needs return type

tests/pos/i7840.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object Example extends App {
2+
3+
trait ZSink[-R, +E, +A0, -A, +B] {
4+
def flatMap[R1 <: R, E1 >: E, A00 >: A0, A1 <: A, C](
5+
f: B => ZSink[R1, E1, A00, A1, C]
6+
)(implicit ev: A00 =:= A1, e2: A1 =:= A00): ZSink[R1, E1, A00, A1, C] =
7+
???
8+
}
9+
10+
object ZSink {
11+
def identity[A]: ZSink[Any, Unit, Nothing, A, A] = ???
12+
def succeed[A, B](b: B): ZSink[Any, Nothing, A, A, B] = ???
13+
}
14+
15+
// cannot prove that Int =:= Nothing
16+
ZSink.identity[Int].flatMap(n => ZSink.succeed[Int, String](n.toString))
17+
}

0 commit comments

Comments
 (0)