Skip to content

Fix #8069: Disallow parameter dependent case classes #8074

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 1 commit into from
Jan 23, 2020
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
25 changes: 19 additions & 6 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -683,15 +683,28 @@ object desugar {
val mods = constr1.mods
mods.is(Private) || (!mods.is(Protected) && mods.hasPrivateWithin)
}

/** Does one of the parameter's types (in the first param clause)
* mention a preceding parameter?
*/
def isParamDependent = constrVparamss match
case vparams :: _ =>
val paramNames = vparams.map(_.name).toSet
vparams.exists(_.tpt.existsSubTree {
case Ident(name: TermName) => paramNames.contains(name)
case _ => false
})
case _ => false

val companionParent =
if (constrTparams.nonEmpty ||
constrVparamss.length > 1 ||
mods.is(Abstract) ||
restrictedAccess ||
isEnumCase) anyRef
if constrTparams.nonEmpty
|| constrVparamss.length > 1
|| mods.is(Abstract)
|| restrictedAccess
|| isParamDependent
|| isEnumCase
then anyRef
else
// todo: also use anyRef if constructor has a dependent method type (or rule that out)!
constrVparamss.foldRight(classTypeRef)((vparams, restpe) => Function(vparams map (_.tpt), restpe))
def widenedCreatorExpr =
widenDefs.foldLeft(creatorExpr)((rhs, meth) => Apply(Ident(meth.name), rhs :: Nil))
Expand Down
8 changes: 8 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/untpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -747,4 +747,12 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
class UntypedDeepFolder[X](f: (X, Tree) => X) extends UntypedTreeAccumulator[X] {
def apply(x: X, tree: Tree)(implicit ctx: Context): X = foldOver(f(x, tree), tree)
}

/** Is there a subtree of this tree that satisfies predicate `p`? */
def (tree: Tree).existsSubTree(p: Tree => Boolean)(implicit ctx: Context): Boolean = {
val acc = new UntypedTreeAccumulator[Boolean] {
def apply(x: Boolean, t: Tree)(implicit ctx: Context) = x || p(t) || foldOver(x, t)
}
acc(false, tree)
}
}
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1130,8 +1130,13 @@ class Namer { typer: Typer =>

index(constr)
index(rest)(localCtx)
symbolOfTree(constr).ensureCompleted()

symbolOfTree(constr).info.stripPoly match // Completes constr symbol as a side effect
case mt: MethodType if cls.is(Case) && mt.isParamDependent =>
// See issue #8073 for background
ctx.error(
i"""Implementation restriction: case classes cannot have dependencies between parameters""",
cls.sourcePos)
case _ =>
denot.info = savedInfo
}

Expand Down
8 changes: 8 additions & 0 deletions tests/neg/i8069.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
trait A
type B

enum Test
case Test(a: A, b: a.B) // error: Implementation restriction: case classes cannot have dependencies between parameters

case class Test2(a: A, b: a.B) // error: Implementation restriction: case classes cannot have dependencies between parameters

12 changes: 12 additions & 0 deletions tests/pos/i8069.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class A {
class B
}

class C(val a: A, val b: a.B)

@main def Test =
val a = A()
val b = a.B()
val c = C(a, b)
val d = c.b
val d1: c.a.B = d