Skip to content

Commit cf2f18c

Browse files
committed
Fix #7840: Don't instantiateSelected to Nothing
When instantiating type variables ahead of an implicit search, avoid instantiating them to Nothing. These are really two changes: - Assert `avoidBottom` in `instantiateSelected` - Change FullyDefinedAccululator logic so that `avoidBottom` overrides `minimizeAll`.
1 parent d45fea0 commit cf2f18c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 2 additions & 2 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`.
@@ -111,7 +111,7 @@ object Inferencing {
111111
def avoidBottom =
112112
!force.allowBottom &&
113113
defn.isBottomType(ctx.typeComparer.approximation(tvar.origin, fromBelow = true))
114-
def preferMin = force.minimizeAll || variance >= 0 && !avoidBottom
114+
def preferMin = (force.minimizeAll || variance >= 0) && !avoidBottom
115115
if (direction != 0) instantiate(tvar, direction < 0)
116116
else if (preferMin) instantiate(tvar, fromBelow = true)
117117
else toMaximize = true

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)