Skip to content

Fix #1716: Don't allow wildcards as type arguments to methods #1860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 26, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,9 @@ object Parsers {
*/
def simpleType(): Tree = simpleTypeRest {
if (in.token == LPAREN)
atPos(in.offset) { makeTupleOrParens(inParens(argTypes())) }
atPos(in.offset) {
makeTupleOrParens(inParens(argTypes(namedOK = false, wildOK = true)))
}
else if (in.token == LBRACE)
atPos(in.offset) { RefinedTypeTree(EmptyTree, refinement()) }
else if (isSimpleLiteral) { SingletonTypeTree(literal()) }
Expand All @@ -805,7 +807,8 @@ object Parsers {

private def simpleTypeRest(t: Tree): Tree = in.token match {
case HASH => simpleTypeRest(typeProjection(t))
case LBRACKET => simpleTypeRest(atPos(startOffset(t)) { AppliedTypeTree(t, typeArgs(namedOK = true)) })
case LBRACKET => simpleTypeRest(atPos(startOffset(t)) {
AppliedTypeTree(t, typeArgs(namedOK = true, wildOK = true)) })
case _ => t
}

Expand All @@ -826,7 +829,7 @@ object Parsers {
/** ArgTypes ::= Type {`,' Type}
* | NamedTypeArg {`,' NamedTypeArg}
*/
def argTypes(namedOK: Boolean = false) = {
def argTypes(namedOK: Boolean, wildOK: Boolean) = {
def otherArgs(first: Tree, arg: () => Tree): List[Tree] = {
val rest =
if (in.token == COMMA) {
Expand All @@ -836,16 +839,17 @@ object Parsers {
else Nil
first :: rest
}
def typParser() = if (wildOK) typ() else toplevelTyp()
if (namedOK && in.token == IDENTIFIER)
typ() match {
typParser() match {
case Ident(name) if in.token == EQUALS =>
in.nextToken()
otherArgs(NamedArg(name, typ()), namedTypeArg)
case firstArg =>
if (in.token == EQUALS) println(s"??? $firstArg")
otherArgs(firstArg, typ)
}
else commaSeparated(typ)
else commaSeparated(typParser)
}

/** FunArgType ::= Type | `=>' Type
Expand Down Expand Up @@ -873,7 +877,7 @@ object Parsers {
/** TypeArgs ::= `[' Type {`,' Type} `]'
* NamedTypeArgs ::= `[' NamedTypeArg {`,' NamedTypeArg} `]'
*/
def typeArgs(namedOK: Boolean = false): List[Tree] = inBrackets(argTypes(namedOK))
def typeArgs(namedOK: Boolean, wildOK: Boolean): List[Tree] = inBrackets(argTypes(namedOK, wildOK))

/** Refinement ::= `{' RefineStatSeq `}'
*/
Expand Down Expand Up @@ -1250,7 +1254,7 @@ object Parsers {
in.nextToken()
simpleExprRest(selector(t), canApply = true)
case LBRACKET =>
val tapp = atPos(startOffset(t), in.offset) { TypeApply(t, typeArgs(namedOK = true)) }
val tapp = atPos(startOffset(t), in.offset) { TypeApply(t, typeArgs(namedOK = true, wildOK = false)) }
simpleExprRest(tapp, canApply = true)
case LPAREN | LBRACE if canApply =>
val app = atPos(startOffset(t), in.offset) { Apply(t, argumentExprs()) }
Expand Down Expand Up @@ -1501,7 +1505,7 @@ object Parsers {
def simplePatternRest(t: Tree): Tree = {
var p = t
if (in.token == LBRACKET)
p = atPos(startOffset(t), in.offset) { TypeApply(p, typeArgs()) }
p = atPos(startOffset(t), in.offset) { TypeApply(p, typeArgs(namedOK = false, wildOK = false)) }
if (in.token == LPAREN)
p = atPos(startOffset(t), in.offset) { Apply(p, argumentPatterns()) }
p
Expand Down
9 changes: 9 additions & 0 deletions tests/pending/neg/i1716.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object Fail {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a slip here: move the test from tests/pending/neg to tests/neg

def f(m: Option[Int]): Unit = {
m match {
case x @ Some[_] => // error
case _ =>
}
}
Some[_] // error
}