Skip to content

Commit 29e3b79

Browse files
committed
Check there are no forward dependencies to method parameters
1 parent 515a519 commit 29e3b79

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@ object Checking {
441441
case List(param) =>
442442
if (param.is(Mutable))
443443
ctx.error("value class parameter must not be a var", param.pos)
444-
445444
case _ =>
446445
ctx.error("value class needs to have exactly one val parameter", clazz.pos)
447446
}
@@ -625,6 +624,24 @@ trait Checking {
625624
case _ =>
626625
}
627626
}
627+
628+
/** Check that method parameter types do not reference their own parameter
629+
* or later parameters in the same parameter section.
630+
*/
631+
def checkNoForwardDependencies(vparams: List[ValDef])(implicit ctx: Context): Unit = vparams match {
632+
case vparam :: vparams1 =>
633+
val check = new TreeTraverser {
634+
def traverse(tree: Tree)(implicit ctx: Context) = tree match {
635+
case id: Ident if vparams.exists(_.symbol == id.symbol) =>
636+
ctx.error("illegal forward reference to method parameter", id.pos)
637+
case _ =>
638+
traverseChildren(tree)
639+
}
640+
}
641+
check.traverse(vparam.tpt)
642+
checkNoForwardDependencies(vparams1)
643+
case Nil =>
644+
}
628645
}
629646

630647
trait NoChecking extends Checking {
@@ -642,4 +659,5 @@ trait NoChecking extends Checking {
642659
override def checkNotSingleton(tpt: Tree, where: String)(implicit ctx: Context): Tree = tpt
643660
override def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(implicit ctx: Context) = ()
644661
override def checkTraitInheritance(parentSym: Symbol, cls: ClassSymbol, pos: Position)(implicit ctx: Context) = ()
662+
override def checkNoForwardDependencies(vparams: List[ValDef])(implicit ctx: Context): Unit = ()
645663
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
12231223
completeAnnotations(ddef, sym)
12241224
val tparams1 = tparams mapconserve (typed(_).asInstanceOf[TypeDef])
12251225
val vparamss1 = vparamss nestedMapconserve (typed(_).asInstanceOf[ValDef])
1226+
vparamss1.foreach(checkNoForwardDependencies)
12261227
if (sym is Implicit) checkImplicitParamsNotSingletons(vparamss1)
12271228
var tpt1 = checkSimpleKinded(typedType(tpt))
12281229

0 commit comments

Comments
 (0)