Skip to content

More careful ClassTag instantiation #23659

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
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5116,11 +5116,17 @@ object Types extends TypeUtils {
*/
private def currentEntry(using Context): Type = ctx.typerState.constraint.entry(origin)

/** For uninstantiated type variables: the lower bound */
def lowerBound(using Context): Type = currentEntry.loBound

/** For uninstantiated type variables: the upper bound */
def upperBound(using Context): Type = currentEntry.hiBound

/** For uninstantiated type variables: Is the lower bound different from Nothing? */
def hasLowerBound(using Context): Boolean = !currentEntry.loBound.isExactlyNothing
def hasLowerBound(using Context): Boolean = !lowerBound.isExactlyNothing

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

/** Unwrap to instance (if instantiated) or origin (if not), until result
* is no longer a TypeVar
Expand Down
24 changes: 22 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,29 @@ 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 ZIP 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 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)
if tvar.hasLowerBound then
if isGroundConstr(tvar.lowerBound) then tvar.instantiate(fromBelow = true)
else if isFullyDefined(tp, ForceDegree.all) then tp
else NoType
else if tvar.hasUpperBound then
if isGroundConstr(tvar.upperBound) then tvar.instantiate(fromBelow = false)
else if isFullyDefined(tp, ForceDegree.all) then tp
else NoType
else NoType)
case _ =>
tp
Expand Down
Loading