Skip to content

Backport "More careful ClassTag instantiation" to 3.7.3 #23714

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

Open
wants to merge 1 commit into
base: release-3.7.3_backport-23664
Choose a base branch
from
Open
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
32 changes: 27 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ast.tpd.*
import Synthesizer.*
import sbt.ExtractDependencies.*
import xsbti.api.DependencyContext.*
import TypeComparer.{fullLowerBound, fullUpperBound}

/** Synthesize terms for special classes */
class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
Expand All @@ -38,10 +39,32 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
// bounds are usually widened during instantiation.
instArg(tp.tp1)
case tvar: TypeVar if ctx.typerState.constraint.contains(tvar) =>
// If tvar has a lower or upper bound:
// 1. If the bound is not another type variable, use this as approximation.
// 2. Otherwise, if the type can be forced to be fully defined, use that type
// as approximation.
// 3. Otherwise leave argument uninstantiated.
// The reason for (2) is that we observed complicated constraints in i23611.scala
// that get better types if a fully defined type is computed than if several type
// variables are approximated incrementally. This is a minimization of some ZIO code.
// So in order to keep backwards compatibility (where before we _only_ did 2) we
// add that special case.
def isGroundConstr(tp: Type): Boolean = tp.dealias match
case tvar: TypeVar if ctx.typerState.constraint.contains(tvar) => false
case pref: TypeParamRef if ctx.typerState.constraint.contains(pref) => false
case tp: AndOrType => isGroundConstr(tp.tp1) && isGroundConstr(tp.tp2)
case _ => true
instArg(
if tvar.hasLowerBound then tvar.instantiate(fromBelow = true)
else if tvar.hasUpperBound then tvar.instantiate(fromBelow = false)
else NoType)
if tvar.hasLowerBound then
if isGroundConstr(fullLowerBound(tvar.origin)) then tvar.instantiate(fromBelow = true)
else if isFullyDefined(tp, ForceDegree.all) then tp
else NoType
else if tvar.hasUpperBound then
if isGroundConstr(fullUpperBound(tvar.origin)) then tvar.instantiate(fromBelow = false)
else if isFullyDefined(tp, ForceDegree.all) then tp
else NoType
else
NoType)
case _ =>
tp

Expand Down Expand Up @@ -569,9 +592,8 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
resType <:< target
val tparams = poly.paramRefs
val variances = childClass.typeParams.map(_.paramVarianceSign)
val instanceTypes = tparams.lazyZip(variances).map((tparam, variance) =>
val instanceTypes = tparams.lazyZip(variances).map: (tparam, variance) =>
TypeComparer.instanceType(tparam, fromBelow = variance < 0, Widen.Unions)
)
val instanceType = resType.substParams(poly, instanceTypes)
// this is broken in tests/run/i13332intersection.scala,
// because type parameters are not correctly inferred.
Expand Down
26 changes: 26 additions & 0 deletions tests/pos/i23611.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.io.{File, IOException}
import java.net.URI
import java.nio.file.{Path, Paths}
import scala.reflect.ClassTag

trait FileConnectors {
def listPath(path: => Path): ZStream[Any, IOException, Path]

final def listFile(file: => File): ZStream[Any, IOException, File] =
for {
path <- null.asInstanceOf[ZStream[Any, IOException, Path]]
r <- listPath(path).mapZIO(a => ZIO.attempt(a.toFile).refineToOrDie)
} yield r
}

sealed trait ZIO[-R, +E, +A]
extension [R, E <: Throwable, A](self: ZIO[R, E, A])
def refineToOrDie[E1 <: E: ClassTag]: ZIO[R, E1, A] = ???

object ZIO:
def attempt[A](code: => A): ZIO[Any, Throwable, A] = ???

sealed trait ZStream[-R, +E, +A]:
def map[B](f: A => B): ZStream[R, E, B] = ???
def flatMap[R1 <: R, E1 >: E, B](f: A => ZStream[R1, E1, B]): ZStream[R1, E1, B]
def mapZIO[R1 <: R, E1 >: E, A1](f: A => ZIO[R1, E1, A1]): ZStream[R1, E1, A1]
30 changes: 30 additions & 0 deletions tests/pos/i23611a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.io.{File, IOException}
import java.net.URI
import java.nio.file.{Path, Paths}
import scala.reflect.ClassTag

trait FileConnectors {
def listPath(path: => Path): ZStream[Any, IOException, Path]

final def listFile(file: => File): ZStream[Any, IOException, File] =
for {
path <- null.asInstanceOf[ZStream[Any, IOException, Path]]
r <- listPath(path).mapZIO(a => ZIO.attempt(a.toFile).refineToOrDie)
} yield r
}

sealed abstract class CanFail[-E]
object CanFail:
given [E]: CanFail[E] = ???

sealed trait ZIO[-R, +E, +A]
extension [R, E <: Throwable, A](self: ZIO[R, E, A])
def refineToOrDie[E1 <: E: ClassTag](using CanFail[E]): ZIO[R, E1, A] = ???

object ZIO:
def attempt[A](code: => A): ZIO[Any, Throwable, A] = ???

sealed trait ZStream[-R, +E, +A]:
def map[B](f: A => B): ZStream[R, E, B] = ???
def flatMap[R1 <: R, E1 >: E, B](f: A => ZStream[R1, E1, B]): ZStream[R1, E1, B]
def mapZIO[R1 <: R, E1 >: E, A1](f: A => ZIO[R1, E1, A1]): ZStream[R1, E1, A1]