Skip to content

Fix for constraining dependent parameter references #13083

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 2 commits into from
Jul 24, 2021
Merged
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
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4417,6 +4417,8 @@ object Types {

private final class RecThisImpl(binder: RecType) extends RecThis(binder)

// @sharable private var skid: Int = 0

// ----- Skolem types -----------------------------------------------

/** A skolem type reference with underlying type `info`.
Expand All @@ -4434,6 +4436,10 @@ object Types {

def withName(name: Name): this.type = { myRepr = name; this }

//skid += 1
//val id = skid
//assert(id != 10)

private var myRepr: Name = null
def repr(using Context): Name = {
if (myRepr == null) myRepr = SkolemName.fresh()
Expand Down
20 changes: 8 additions & 12 deletions compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -601,24 +601,20 @@ trait Inferencing { this: Typer =>
def constraint = state.constraint
type InstantiateQueue = mutable.ListBuffer[(TypeVar, Boolean)]
val toInstantiate = new InstantiateQueue
for (tvar <- qualifying)
if (!tvar.isInstantiated && constraint.contains(tvar)) {
for tvar <- qualifying do
if !tvar.isInstantiated && constraint.contains(tvar) then
constrainIfDependentParamRef(tvar, tree)

// Needs to be checked again, since previous interpolations could already have
// instantiated `tvar` through unification.
val v = vs(tvar)
if (v == null) {
if v == null then
typr.println(i"interpolate non-occurring $tvar in $state in $tree: $tp, fromBelow = ${tvar.hasLowerBound}, $constraint")
toInstantiate += ((tvar, tvar.hasLowerBound))
}
else if v.intValue != 0 then
typr.println(i"interpolate $tvar in $state in $tree: $tp, fromBelow = ${v.intValue == 1}, $constraint")
toInstantiate += ((tvar, v.intValue == 1))
else
if (v.intValue != 0) {
typr.println(i"interpolate $tvar in $state in $tree: $tp, fromBelow = ${v.intValue == 1}, $constraint")
toInstantiate += ((tvar, v.intValue == 1))
}
else typr.println(i"no interpolation for nonvariant $tvar in $state")
}
typr.println(i"no interpolation for nonvariant $tvar in $state")

/** Instantiate all type variables in `buf` in the indicated directions.
* If a type variable A is instantiated from below, and there is another
Expand Down Expand Up @@ -694,7 +690,7 @@ trait Inferencing { this: Typer =>

val arg = findArg(call)
if !arg.isEmpty then
var argType = arg.tpe.widenExpr.widenTermRefExpr
var argType = arg.tpe.widenIfUnstable
if !argType.isSingleton then argType = SkolemType(argType)
argType <:< tvar
case _ =>
Expand Down
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,17 @@ trait TypeAssigner {
}
}
else {
val argTypes = args.tpes
// Make sure arguments don't contain the type `pt` itself.
// make a copy of the argument if that's the case.
// This is done to compensate for the fact that normally every
// reference to a polytype would have to be a fresh copy of that type,
// but we want to avoid that because it would increase compilation cost.
// See pos/i6682a.scala for a test case where the defensive copying matters.
val ensureFresh = new TypeMap:
def apply(tp: Type) = mapOver(
if tp eq pt then pt.newLikeThis(pt.paramNames, pt.paramInfos, pt.resType)
else tp)
val argTypes = args.tpes.mapConserve(ensureFresh)
if (sameLength(argTypes, paramNames)) pt.instantiate(argTypes)
else wrongNumberOfTypeArgs(fn.tpe, pt.typeParams, args, tree.srcPos)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/i12803.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
trait X {
type Y
}

trait E[A]

trait Test {
val x: X
def wrap(x: X): E[x.Y] = ???
def run[I](i: E[I]): Unit = ???
run(wrap(x))
}