Skip to content

Commit

Permalink
Simplify ByName handling in initialization checker
Browse files Browse the repository at this point in the history
Since ByName nodes are now explicit, no need to introduce them separately in the
checker.
  • Loading branch information
odersky committed Jan 19, 2022
1 parent 2066020 commit 0f9ddab
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions compiler/src/dotty/tools/dotc/transform/init/Semantic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1054,19 +1054,18 @@ object Semantic {
exprs.map { expr => eval(expr, thisV, klass) }

/** Evaluate arguments of methods */
def evalArgs(args: List[Arg], thisV: Ref, klass: ClassSymbol): Contextual[(List[Error], List[ArgInfo])] =
def evalArgs(args: List[Tree], thisV: Ref, klass: ClassSymbol): Contextual[(List[Error], List[ArgInfo])] =
val errors = new mutable.ArrayBuffer[Error]
val argInfos = new mutable.ArrayBuffer[ArgInfo]
args.foreach { arg =>
val res =
if arg.isByName then
val fun = Fun(arg.tree, thisV, klass, env)
val res = arg match
case ByName(_) =>
val fun = Fun(arg, thisV, klass, env)
Result(fun, Nil)
else
eval(arg.tree, thisV, klass)

case _ =>
eval(arg, thisV, klass)
errors ++= res.errors
argInfos += ArgInfo(res.value, arg.tree)
argInfos += ArgInfo(res.value, arg)
}
(errors.toList, argInfos.toList)

Expand Down Expand Up @@ -1491,31 +1490,17 @@ object Semantic {
case hklambda: HKTypeLambda => typeRefOf(hklambda.resType)
}

opaque type Arg = Tree | ByNameArg // ^^^ can be simplified
case class ByNameArg(tree: Tree)

extension (arg: Arg)
def isByName = arg.isInstanceOf[ByNameArg]
def tree: Tree = arg match
case t: Tree => t
case ByNameArg(t) => t

object Call {

private def isDelayed(tp: Type)(using Context) =
tp.isInstanceOf[MethodicType] || tp.isByName

def unapply(tree: Tree)(using Context): Option[(Tree, List[List[Arg]])] =
def unapply(tree: Tree)(using Context): Option[(Tree, List[List[Tree]])] =
tree match
case Apply(fn, args) =>
val argTps = fn.tpe.widen match
case mt: MethodType => mt.paramInfos
val normArgs: List[Arg] = args.zip(argTps).map { (arg, formal) =>
if formal.isByName then ByNameArg(arg.dropByName) else arg
}
unapply(fn) match
case Some((ref, args0)) => Some((ref, args0 :+ normArgs))
case None => None
case Some((ref, args0)) => Some((ref, args0 :+ args))
case None => None

case TypeApply(fn, targs) =>
unapply(fn)
Expand All @@ -1527,7 +1512,7 @@ object Semantic {
}

object NewExpr {
def unapply(tree: Tree)(using Context): Option[(TypeRef, New, Symbol, List[List[Arg]])] =
def unapply(tree: Tree)(using Context): Option[(TypeRef, New, Symbol, List[List[Tree]])] =
tree match
case Call(fn @ Select(newTree: New, init), argss) if init == nme.CONSTRUCTOR =>
val tref = typeRefOf(newTree.tpe)
Expand Down

0 comments on commit 0f9ddab

Please sign in to comment.