Skip to content

Commit

Permalink
Merge pull request #9183 from som-snytt/issue/restore-unit
Browse files Browse the repository at this point in the history
Exclude unit value as literal type
  • Loading branch information
dwijnand committed Sep 1, 2020
2 parents 6a830aa + fe432b8 commit f46085c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1004,12 +1004,16 @@ self =>
def argType(): Tree
def functionArgType(): Tree

// () must be () => R, otherwise (types) could be tuple or (types) => R
// () must be () => R; (types) could be tuple or (types) => R
private def tupleInfixType(start: Offset) = {
require(in.token == LPAREN)
val ts = inParens{ if (in.token == RPAREN) Nil else functionTypes() }
val ts = inParens { if (in.token == RPAREN) Nil else functionTypes() }
if (in.token == ARROW)
atPos(start, in.skipToken()) { makeSafeFunctionType(ts, typ()) }
else if (ts.isEmpty) {
syntaxError(start, "Illegal literal type (), use Unit instead")
EmptyTree
}
else {
ts foreach checkNotByNameOrVarargs
val tuple = atPos(start) { makeSafeTupleType(ts) }
Expand Down Expand Up @@ -1078,17 +1082,25 @@ self =>
*/
def simpleType(): Tree = {
if (isLiteralToken(in.token) && in.token != NULL)
atPos(in.offset){SingletonTypeTree(literal())}
atPos(in.offset)(SingletonTypeTree(literal()))
else if (in.name == raw.MINUS && lookingAhead(isNumericLit)) {
val start = in.offset
in.nextToken()
atPos(start){ SingletonTypeTree(literal(isNegated = true, start = start)) }
atPos(start)(SingletonTypeTree(literal(isNegated = true, start = start)))
} else {
val start = in.offset
simpleTypeRest(in.token match {
case LPAREN => atPos(start)(makeSafeTupleType(inParens(types())))
case USCORE => wildcardType(in.skipToken())
case _ =>
case LPAREN =>
if (lookingAhead(in.token == RPAREN)) {
in.nextToken()
in.nextToken()
syntaxError(start, "Illegal literal type (), use Unit instead")
EmptyTree
}
else
atPos(start)(makeSafeTupleType(inParens(types())))
case USCORE => wildcardType(in.skipToken())
case _ =>
path(thisOK = false, typeOK = true) match {
case r @ SingletonTypeTree(_) => r
case r => convertToTypeId(r)
Expand Down
22 changes: 22 additions & 0 deletions test/files/neg/sip23-no-unit-type.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
sip23-no-unit-type.scala:6: error: Illegal literal type (), use Unit instead
case _: () => "err"
^
sip23-no-unit-type.scala:7: error: Illegal literal type (), use Unit instead
case _: ().type => "err"
^
sip23-no-unit-type.scala:7: error: '=>' expected but '.' found.
case _: ().type => "err"
^
sip23-no-unit-type.scala:10: error: Illegal literal type (), use Unit instead
val younit: () = ()
^
sip23-no-unit-type.scala:11: error: Illegal literal type (), use Unit instead
val unit: ().type = ()
^
sip23-no-unit-type.scala:11: error: '=' expected but '.' found.
val unit: ().type = ()
^
sip23-no-unit-type.scala:12: error: illegal start of simple expression
}
^
7 errors
12 changes: 12 additions & 0 deletions test/files/neg/sip23-no-unit-type.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

object Test {
def f: () => Int = () => 42
def s = null.asInstanceOf[Any] match {
case () => "ok"
case _: () => "err"
case _: ().type => "err"
case _: Unit => "quite good"
}
val younit: () = ()
val unit: ().type = ()
}

0 comments on commit f46085c

Please sign in to comment.