Skip to content

Commit 9bb0827

Browse files
committed
Refine criterion when to use fullyDefinedType in ClassTag search
1 parent 3e6d1bc commit 9bb0827

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5116,17 +5116,11 @@ object Types extends TypeUtils {
51165116
*/
51175117
private def currentEntry(using Context): Type = ctx.typerState.constraint.entry(origin)
51185118

5119-
/** For uninstantiated type variables: the lower bound */
5120-
def lowerBound(using Context): Type = currentEntry.loBound
5121-
5122-
/** For uninstantiated type variables: the upper bound */
5123-
def upperBound(using Context): Type = currentEntry.hiBound
5124-
51255119
/** For uninstantiated type variables: Is the lower bound different from Nothing? */
5126-
def hasLowerBound(using Context): Boolean = !lowerBound.isExactlyNothing
5120+
def hasLowerBound(using Context): Boolean = !currentEntry.loBound.isExactlyNothing
51275121

51285122
/** For uninstantiated type variables: Is the upper bound different from Any? */
5129-
def hasUpperBound(using Context): Boolean = !upperBound.isTopOfSomeKind
5123+
def hasUpperBound(using Context): Boolean = !currentEntry.hiBound.isTopOfSomeKind
51305124

51315125
/** Unwrap to instance (if instantiated) or origin (if not), until result
51325126
* is no longer a TypeVar

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import ast.tpd.*
2020
import Synthesizer.*
2121
import sbt.ExtractDependencies.*
2222
import xsbti.api.DependencyContext.*
23+
import TypeComparer.{fullLowerBound, fullUpperBound}
2324

2425
/** Synthesize terms for special classes */
2526
class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
@@ -50,18 +51,20 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
5051
// add that special case.
5152
def isGroundConstr(tp: Type): Boolean = tp.dealias match
5253
case tvar: TypeVar if ctx.typerState.constraint.contains(tvar) => false
54+
case pref: TypeParamRef if ctx.typerState.constraint.contains(pref) => false
5355
case tp: AndOrType => isGroundConstr(tp.tp1) && isGroundConstr(tp.tp2)
5456
case _ => true
5557
instArg(
5658
if tvar.hasLowerBound then
57-
if isGroundConstr(tvar.lowerBound) then tvar.instantiate(fromBelow = true)
59+
if isGroundConstr(fullLowerBound(tvar.origin)) then tvar.instantiate(fromBelow = true)
5860
else if isFullyDefined(tp, ForceDegree.all) then tp
5961
else NoType
6062
else if tvar.hasUpperBound then
61-
if isGroundConstr(tvar.upperBound) then tvar.instantiate(fromBelow = false)
63+
if isGroundConstr(fullUpperBound(tvar.origin)) then tvar.instantiate(fromBelow = false)
6264
else if isFullyDefined(tp, ForceDegree.all) then tp
6365
else NoType
64-
else NoType)
66+
else
67+
NoType)
6568
case _ =>
6669
tp
6770

@@ -593,9 +596,8 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
593596
resType <:< target
594597
val tparams = poly.paramRefs
595598
val variances = childClass.typeParams.map(_.paramVarianceSign)
596-
val instanceTypes = tparams.lazyZip(variances).map((tparam, variance) =>
599+
val instanceTypes = tparams.lazyZip(variances).map: (tparam, variance) =>
597600
TypeComparer.instanceType(tparam, fromBelow = variance < 0, Widen.Unions)
598-
)
599601
val instanceType = resType.substParams(poly, instanceTypes)
600602
// this is broken in tests/run/i13332intersection.scala,
601603
// because type parameters are not correctly inferred.

tests/pos/i23611a.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.{File, IOException}
2+
import java.net.URI
3+
import java.nio.file.{Path, Paths}
4+
import scala.reflect.ClassTag
5+
6+
trait FileConnectors {
7+
def listPath(path: => Path): ZStream[Any, IOException, Path]
8+
9+
final def listFile(file: => File): ZStream[Any, IOException, File] =
10+
for {
11+
path <- null.asInstanceOf[ZStream[Any, IOException, Path]]
12+
r <- listPath(path).mapZIO(a => ZIO.attempt(a.toFile).refineToOrDie)
13+
} yield r
14+
}
15+
16+
sealed abstract class CanFail[-E]
17+
object CanFail:
18+
given [E]: CanFail[E] = ???
19+
20+
sealed trait ZIO[-R, +E, +A]
21+
extension [R, E <: Throwable, A](self: ZIO[R, E, A])
22+
def refineToOrDie[E1 <: E: ClassTag](using CanFail[E]): ZIO[R, E1, A] = ???
23+
24+
object ZIO:
25+
def attempt[A](code: => A): ZIO[Any, Throwable, A] = ???
26+
27+
sealed trait ZStream[-R, +E, +A]:
28+
def map[B](f: A => B): ZStream[R, E, B] = ???
29+
def flatMap[R1 <: R, E1 >: E, B](f: A => ZStream[R1, E1, B]): ZStream[R1, E1, B]
30+
def mapZIO[R1 <: R, E1 >: E, A1](f: A => ZIO[R1, E1, A1]): ZStream[R1, E1, A1]

0 commit comments

Comments
 (0)