Skip to content
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

Support ? as wildcard marker under -Xsource:3 #9560

Merged
merged 1 commit into from Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
}