Skip to content

Commit

Permalink
Fix unused method types propagation to next ()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Oct 19, 2017
1 parent 28a8554 commit 35b7541
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 31 deletions.
14 changes: 7 additions & 7 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1843,12 +1843,12 @@ object Parsers {
def typeParamClauseOpt(ownerKind: ParamOwner.Value): List[TypeDef] =
if (in.token == LBRACKET) typeParamClause(ownerKind) else Nil

/** ClsParamClauses ::= {ClsParamClause} [[nl] `(' `implicit' ClsParams `)']
* ClsParamClause ::= [nl] `(' [ClsParams] ')'
/** ClsParamClauses ::= {ClsParamClause} [[nl] `(' `implicit' [`unused'] ClsParams `)']
* ClsParamClause ::= [nl] `(' [`unused'] [ClsParams] ')'
* ClsParams ::= ClsParam {`' ClsParam}
* ClsParam ::= {Annotation} [{Modifier} (`val' | `var') | `inline'] Param
* DefParamClauses ::= {DefParamClause} [[nl] `(' `implicit' DefParams `)']
* DefParamClause ::= [nl] `(' [DefParams] ')'
* DefParamClauses ::= {DefParamClause} [[nl] `(' `implicit' [`unused'] DefParams `)']
* DefParamClause ::= [nl] `(' [`unused'] [DefParams] ')'
* DefParams ::= DefParam {`,' DefParam}
* DefParam ::= {Annotation} [`inline'] Param
* Param ::= id `:' ParamType [`=' Expr]
Expand Down Expand Up @@ -1907,7 +1907,6 @@ object Parsers {
if (in.token == RPAREN) Nil
else {
if (in.token == IMPLICIT || in.token == UNUSED) {
imods = EmptyModifiers
if (in.token == IMPLICIT) {
implicitOffset = in.offset
imods = implicitMods(imods)
Expand All @@ -1920,12 +1919,13 @@ object Parsers {
}
def clauses(): List[List[ValDef]] = {
newLineOptWhenFollowedBy(LPAREN)
if (in.token == LPAREN)
if (in.token == LPAREN) {
imods = EmptyModifiers
paramClause() :: {
firstClauseOfCaseClass = false
if (imods is Implicit) Nil else clauses()
}
else Nil
} else Nil
}
val start = in.offset
val result = clauses()
Expand Down
26 changes: 10 additions & 16 deletions compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -447,24 +447,16 @@ object Erasure {
private def runtimeCallWithProtoArgs(name: Name, pt: Type, args: Tree*)(implicit ctx: Context): Tree = {
val meth = defn.runtimeMethodRef(name)
val followingParams = meth.symbol.info.firstParamTypes.drop(args.length)
val followingArgs = protoArgs(pt).zipWithConserve(followingParams)(typedExpr).asInstanceOf[List[tpd.Tree]]
val followingArgs = protoArgs(pt, meth.widen).zipWithConserve(followingParams)(typedExpr).asInstanceOf[List[tpd.Tree]]
ref(meth).appliedToArgs(args.toList ++ followingArgs)
}

private def protoArgs(pt: Type): List[untpd.Tree] = pt match {
case pt: FunProto => pt.args ++ protoArgs(pt.resType)
private def protoArgs(pt: Type, tp: Type): List[untpd.Tree] = (pt, tp) match {
case (pt: FunProto, tp: MethodType) if tp.isUnusedMethod => protoArgs(pt.resType, tp.resType)
case (pt: FunProto, tp: MethodType) => pt.args ++ protoArgs(pt.resType, tp.resType)
case _ => Nil
}

// TODO: merge this whit protoArgs if it is actually needed
private def protoArgs2(pt: Type, tp: Type): List[untpd.Tree] = (pt, tp) match {
case (pt: FunProto, tp: MethodType) if tp.isUnusedMethod => protoArgs2(pt.resType, tp.resType)
case (pt: FunProto, tp: MethodType) => pt.args ++ protoArgs2(pt.resType, tp.resType)
case _ =>
assert(!tp.isInstanceOf[MethodOrPoly], tp)
Nil
}

override def typedTypeApply(tree: untpd.TypeApply, pt: Type)(implicit ctx: Context) = {
val ntree = interceptTypeApply(tree.asInstanceOf[TypeApply])(ctx.withPhase(ctx.erasurePhase))

Expand Down Expand Up @@ -495,8 +487,8 @@ object Erasure {
fun1.tpe.widen match {
case mt: MethodType =>
val outers = outer.args(fun.asInstanceOf[tpd.Tree]) // can't use fun1 here because its type is already erased
var args0 = protoArgs2(pt, tree.typeOpt)
if (!mt.isUnusedMethod) args0 = args ::: args0
var args0 = protoArgs(pt, tree.typeOpt)
if (mt.paramNames.nonEmpty && !mt.isUnusedMethod) args0 = args ::: args0
args0 = outers ::: args0

if (args0.length > MaxImplementedFunctionArity && mt.paramInfos.length == 1) {
Expand All @@ -505,8 +497,10 @@ object Erasure {
args0 = bunchedArgs :: Nil
}
// Arguments are phantom if an only if the parameters are phantom, guaranteed by the separation of type lattices
val args1 = args0.filterConserve(arg => !wasPhantom(arg.typeOpt)).zipWithConserve(mt.paramInfos)(typedExpr)
untpd.cpy.Apply(tree)(fun1, args1) withType mt.resultType
val args1 = args0.filterConserve(arg => !wasPhantom(arg.typeOpt))
assert(args1 hasSameLengthAs mt.paramInfos)
val args2 = args1.zipWithConserve(mt.paramInfos)(typedExpr)
untpd.cpy.Apply(tree)(fun1, args2) withType mt.resultType
case _ =>
throw new MatchError(i"tree $tree has unexpected type of function ${fun1.tpe.widen}, was ${fun.typeOpt.widen}")
}
Expand Down
4 changes: 2 additions & 2 deletions tests/run/unused-4.check
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
foo
foo2
fun
fun 42
foo
foo2
fun2
fun2 abc
4 changes: 2 additions & 2 deletions tests/run/unused-4.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ object Test {
}

def fun(a: Int)(unused b: String): Unit = {
println("fun")
println("fun " + a)
}

def fun2(unused a: Int)(b: String): Unit = {
println("fun2")
println("fun2 " + b)
}
}
4 changes: 2 additions & 2 deletions tests/run/unused-5.check
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ foo
foo
foo
foo
fun
fun 42 42
foo
foo
foo
foo
fun2
fun2 42 42
4 changes: 2 additions & 2 deletions tests/run/unused-5.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ object Test {
}

def fun(a: Int)(unused b: Int)(c: Int)(unused d: Int): Unit = {
println("fun")
println("fun " + a + " " + c)
}

def fun2(unused a2: Int)(b2: Int)(unused c2: Int)(d2: Int): Unit = {
println("fun2")
println("fun2 " + b2 + " " + d2)
}
}

0 comments on commit 35b7541

Please sign in to comment.