Skip to content

Commit ae768dd

Browse files
committed
Rewrite underscore with optional space
1 parent 7f27eaf commit ae768dd

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,15 +2007,19 @@ object Parsers {
20072007
Ident(tpnme.USCOREkw).withSpan(Span(start, in.lastOffset, start))
20082008
else
20092009
if !inMatchPattern then
2010-
report.errorOrMigrationWarning(
2011-
em"`_` is deprecated for wildcard arguments of types: use `?` instead${rewriteNotice(`3.4-migration`)}",
2012-
in.sourcePos(),
2013-
MigrationVersion.WildcardType)
2014-
if MigrationVersion.WildcardType.needsPatch then
2015-
patch(source, Span(in.offset, in.offset + 1), "?")
2016-
end if
2010+
val msg =
2011+
em"`_` is deprecated for wildcard arguments of types: use `?` instead${rewriteNotice(`3.4-migration`)}"
2012+
report.errorOrMigrationWarning(msg, in.sourcePos(), MigrationVersion.WildcardType)
20172013
val start = in.skipToken()
20182014
typeBounds().withSpan(Span(start, in.lastOffset, start))
2015+
.tap: tbt =>
2016+
if !inMatchPattern && MigrationVersion.WildcardType.needsPatch then
2017+
val offset_? = tbt.span.start
2018+
if Chars.isOperatorPart(source(offset_? + 1)) then
2019+
patch(source, tbt.span, "?" + ctx.printer.toText(tbt).mkString())
2020+
else
2021+
patch(source, Span(offset_?, offset_? + 1), "?")
2022+
20192023
// Allow symbols -_ and +_ through for compatibility with code written using kind-projector in Scala 3 underscore mode.
20202024
// While these signify variant type parameters in Scala 2 + kind-projector, we ignore their variance markers since variance is inferred.
20212025
else if (isIdent(nme.MINUS) || isIdent(nme.PLUS)) && in.lookahead.token == USCORE && ctx.settings.XkindProjector.value == "underscores" then

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class CompilationTests {
8888
compileFile("tests/rewrites/i22731b.scala", defaultOptions.and("-rewrite", "-source:3.7-migration")),
8989
compileFile("tests/rewrites/implicit-to-given.scala", defaultOptions.and("-rewrite", "-Yimplicit-to-given")),
9090
compileFile("tests/rewrites/i22792.scala", defaultOptions.and("-rewrite")),
91+
compileFile("tests/rewrites/i23449.scala", defaultOptions.and("-rewrite", "-source:3.4-migration")),
9192
).checkRewrites()
9293
}
9394

tests/rewrites/i23449.check

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
trait T
2+
class C[A]
3+
4+
def f(x: C[? <: T]) = ()
5+
6+
def g(x: C[? >: T]) = ()
7+
8+
def h(x: C[? <: T]) = ()
9+
10+
def k(x: C[? >: T]) = ()
11+
12+
def m(x: C[? >: Nothing <: T]) = ()
13+
14+
def n(x: C[ ? >: Nothing <: T ]) = ()

tests/rewrites/i23449.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
trait T
2+
class C[A]
3+
4+
def f(x: C[_<:T]) = ()
5+
6+
def g(x: C[_>:T]) = ()
7+
8+
def h(x: C[_<: T]) = ()
9+
10+
def k(x: C[_ >: T]) = ()
11+
12+
def m(x: C[_>:Nothing<:T]) = ()
13+
14+
def n(x: C[ _>:Nothing <:T ]) = ()

0 commit comments

Comments
 (0)