Skip to content

Commit 1b3649d

Browse files
authored
Merge pull request #1860 from dotty-staging/fix-#1716-v2
Fix #1716: Don't allow wildcards as type arguments to methods
2 parents d7f3722 + 09e15bd commit 1b3649d

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,9 @@ object Parsers {
783783
*/
784784
def simpleType(): Tree = simpleTypeRest {
785785
if (in.token == LPAREN)
786-
atPos(in.offset) { makeTupleOrParens(inParens(argTypes())) }
786+
atPos(in.offset) {
787+
makeTupleOrParens(inParens(argTypes(namedOK = false, wildOK = true)))
788+
}
787789
else if (in.token == LBRACE)
788790
atPos(in.offset) { RefinedTypeTree(EmptyTree, refinement()) }
789791
else if (isSimpleLiteral) { SingletonTypeTree(literal()) }
@@ -805,7 +807,8 @@ object Parsers {
805807

806808
private def simpleTypeRest(t: Tree): Tree = in.token match {
807809
case HASH => simpleTypeRest(typeProjection(t))
808-
case LBRACKET => simpleTypeRest(atPos(startOffset(t)) { AppliedTypeTree(t, typeArgs(namedOK = true)) })
810+
case LBRACKET => simpleTypeRest(atPos(startOffset(t)) {
811+
AppliedTypeTree(t, typeArgs(namedOK = true, wildOK = true)) })
809812
case _ => t
810813
}
811814

@@ -826,7 +829,7 @@ object Parsers {
826829
/** ArgTypes ::= Type {`,' Type}
827830
* | NamedTypeArg {`,' NamedTypeArg}
828831
*/
829-
def argTypes(namedOK: Boolean = false) = {
832+
def argTypes(namedOK: Boolean, wildOK: Boolean) = {
830833
def otherArgs(first: Tree, arg: () => Tree): List[Tree] = {
831834
val rest =
832835
if (in.token == COMMA) {
@@ -836,16 +839,17 @@ object Parsers {
836839
else Nil
837840
first :: rest
838841
}
842+
def typParser() = if (wildOK) typ() else toplevelTyp()
839843
if (namedOK && in.token == IDENTIFIER)
840-
typ() match {
844+
typParser() match {
841845
case Ident(name) if in.token == EQUALS =>
842846
in.nextToken()
843847
otherArgs(NamedArg(name, typ()), namedTypeArg)
844848
case firstArg =>
845849
if (in.token == EQUALS) println(s"??? $firstArg")
846850
otherArgs(firstArg, typ)
847851
}
848-
else commaSeparated(typ)
852+
else commaSeparated(typParser)
849853
}
850854

851855
/** FunArgType ::= Type | `=>' Type
@@ -873,7 +877,7 @@ object Parsers {
873877
/** TypeArgs ::= `[' Type {`,' Type} `]'
874878
* NamedTypeArgs ::= `[' NamedTypeArg {`,' NamedTypeArg} `]'
875879
*/
876-
def typeArgs(namedOK: Boolean = false): List[Tree] = inBrackets(argTypes(namedOK))
880+
def typeArgs(namedOK: Boolean, wildOK: Boolean): List[Tree] = inBrackets(argTypes(namedOK, wildOK))
877881

878882
/** Refinement ::= `{' RefineStatSeq `}'
879883
*/
@@ -1250,7 +1254,7 @@ object Parsers {
12501254
in.nextToken()
12511255
simpleExprRest(selector(t), canApply = true)
12521256
case LBRACKET =>
1253-
val tapp = atPos(startOffset(t), in.offset) { TypeApply(t, typeArgs(namedOK = true)) }
1257+
val tapp = atPos(startOffset(t), in.offset) { TypeApply(t, typeArgs(namedOK = true, wildOK = false)) }
12541258
simpleExprRest(tapp, canApply = true)
12551259
case LPAREN | LBRACE if canApply =>
12561260
val app = atPos(startOffset(t), in.offset) { Apply(t, argumentExprs()) }
@@ -1501,7 +1505,7 @@ object Parsers {
15011505
def simplePatternRest(t: Tree): Tree = {
15021506
var p = t
15031507
if (in.token == LBRACKET)
1504-
p = atPos(startOffset(t), in.offset) { TypeApply(p, typeArgs()) }
1508+
p = atPos(startOffset(t), in.offset) { TypeApply(p, typeArgs(namedOK = false, wildOK = false)) }
15051509
if (in.token == LPAREN)
15061510
p = atPos(startOffset(t), in.offset) { Apply(p, argumentPatterns()) }
15071511
p

tests/neg/i1716.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Fail {
2+
def f(m: Option[Int]): Unit = {
3+
m match {
4+
case x @ Some[_] => // error
5+
case _ =>
6+
}
7+
}
8+
Some[_] // error
9+
}

0 commit comments

Comments
 (0)