Skip to content

Commit

Permalink
Give better error message when function arity limit exceeded
Browse files Browse the repository at this point in the history
 - Addresses scala/bug#10737
 - Also change tuple error messages to conform to new wording
  • Loading branch information
jonathanfrawley authored and adriaanm committed Jun 14, 2018
1 parent 211bc44 commit dcefe2c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,22 @@ self =>
makeTupleTerm(elems)
}

/** Create a function Tree. If the arity is not supported, a syntax error is emitted. */
def makeSafeFunctionType(argtpes: List[Tree], offset: Offset, restpe: Tree) = {
if (checkFunctionArity(argtpes, offset)) makeFunctionTypeTree(argtpes, restpe)
else makeFunctionTypeTree(Nil, restpe) // create a dummy node
}

private[this] def checkTupleSize(elems: List[Tree], offset: Offset): Boolean =
elems.lengthCompare(definitions.MaxTupleArity) <= 0 || {
val msg = s"too many elements for tuple: ${elems.length}, allowed: ${definitions.MaxTupleArity}"
val msg = s"tuples may not have more than ${definitions.MaxFunctionArity} elements, but ${elems.length} given"
syntaxError(offset, msg, skipIt = false)
false
}

private[this] def checkFunctionArity(argtpes: List[Tree], offset: Offset): Boolean =
argtpes.lengthCompare(definitions.MaxFunctionArity) <= 0 || {
val msg = s"function values may not have more than ${definitions.MaxFunctionArity} parameters, but ${argtpes.length} given"
syntaxError(offset, msg, skipIt = false)
false
}
Expand Down Expand Up @@ -965,7 +978,7 @@ self =>
val ts = functionTypes()
accept(RPAREN)
if (in.token == ARROW)
atPos(start, in.skipToken()) { makeFunctionTypeTree(ts, typ()) }
atPos(start, in.skipToken()) { makeSafeFunctionType(ts, start, typ()) }
else {
ts foreach checkNotByNameOrVarargs
val tuple = atPos(start) { makeSafeTupleType(ts, start) }
Expand Down
4 changes: 4 additions & 0 deletions test/files/neg/func-max-args.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
func-max-args.scala:3: error: function values may not have more than 22 parameters, but 23 given
val func23: (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w) => Int
^
one error found
4 changes: 4 additions & 0 deletions test/files/neg/func-max-args.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trait T {
val func22: (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v) => Int
val func23: (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w) => Int
}
4 changes: 2 additions & 2 deletions test/files/neg/t9572.check
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
t9572.scala:3: error: too many elements for tuple: 23, allowed: 22
t9572.scala:3: error: tuples may not have more than 22 elements, but 23 given
val term23 = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
^
t9572.scala:5: error: too many elements for tuple: 23, allowed: 22
t9572.scala:5: error: tuples may not have more than 22 elements, but 23 given
val type23: (Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int) = null
^
two errors found

0 comments on commit dcefe2c

Please sign in to comment.