Skip to content

Commit

Permalink
Merge pull request #9560 from smarter/wildcard-qmark
Browse files Browse the repository at this point in the history
Support `?` as wildcard marker under -Xsource:3
  • Loading branch information
SethTisue committed Apr 8, 2021
2 parents df03c9a + 3c5f6aa commit 411c1f7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Expand Up @@ -705,6 +705,10 @@ self =>
def isRawBar = isRawIdent && in.name == raw.BAR
def isRawIdent = in.token == IDENTIFIER

def isWildcardType =
in.token == USCORE ||
settings.isScala3 && isRawIdent && in.name == raw.QMARK

def isIdent = in.token == IDENTIFIER || in.token == BACKQUOTED_IDENT
def isMacro = in.token == IDENTIFIER && in.name == nme.MACROkw

Expand Down Expand Up @@ -1105,12 +1109,14 @@ self =>
}
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)
}
if (isWildcardType)
wildcardType(in.skipToken())
else
path(thisOK = false, typeOK = true) match {
case r @ SingletonTypeTree(_) => r
case r => convertToTypeId(r)
}
})
}
}
Expand Down Expand Up @@ -1976,18 +1982,16 @@ self =>
final def functionArgType(): Tree = argType()
final def argType(): Tree = {
val start = in.offset
in.token match {
case USCORE =>
if (isWildcardType) {
in.nextToken()
if (in.token == SUBTYPE || in.token == SUPERTYPE) wildcardType(start)
else atPos(start) { Bind(tpnme.WILDCARD, EmptyTree) }
case _ =>
typ() match {
case Ident(name: TypeName) if nme.isVariableName(name) =>
atPos(start) { Bind(name, EmptyTree) }
case t => t
}
}
} else
typ() match {
case Ident(name: TypeName) if nme.isVariableName(name) =>
atPos(start) { Bind(name, EmptyTree) }
case t => t
}
}

/** {{{
Expand Down
1 change: 1 addition & 0 deletions src/reflect/scala/reflect/internal/StdNames.scala
Expand Up @@ -960,6 +960,7 @@ trait StdNames {
final val PLUS : NameType = nameType("+")
final val STAR : NameType = nameType("*")
final val TILDE: NameType = nameType("~")
final val QMARK: NameType = nameType("?")

final val isUnary: Set[Name] = Set(MINUS, PLUS, TILDE, BANG)
}
Expand Down
21 changes: 21 additions & 0 deletions test/files/pos/wildcards-future.scala
@@ -0,0 +1,21 @@
// scalac: -Xsource:3
//
object Test {
val xs: List[?] = List(1, 2, 3)
val ys: Map[? <: AnyRef, ? >: Null] = Map()

def foo(x: Any) = x match {
case x: List[?] => x
case _ => x
}

// Only allowed in Scala 3 under -source 3.0-migration
type ? = Int

val xs2: List[`?`] = List(1)
val xs3: List[Int] = xs2

def foo2(x: List[`?`]): List[Int] = x match {
case x: List[`?`] => x
}
}

0 comments on commit 411c1f7

Please sign in to comment.