Skip to content

Commit 2429854

Browse files
authored
Merge pull request scala#10790 from som-snytt/tweak/13004-any2stringadd
any2stringadd is ineligible under -Xsource:3-cross
2 parents 0638638 + 0754408 commit 2429854

File tree

8 files changed

+74
-12
lines changed

8 files changed

+74
-12
lines changed

src/compiler/scala/tools/nsc/typechecker/Contexts.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,14 @@ trait Contexts { self: Analyzer =>
173173
else RootImports.completeList
174174

175175
def rootContext(unit: CompilationUnit, tree: Tree = EmptyTree, throwing: Boolean = false, checking: Boolean = false): Context = {
176-
val rootImportsContext = rootImports(unit).foldLeft(startContext)((c, sym) =>
177-
c.make(gen.mkWildcardImport(sym), unit = unit)
178-
)
176+
val rootImportsContext = rootImports(unit).foldLeft(startContext) { (c, sym) =>
177+
val imp =
178+
if ((sym eq PredefModule) && currentRun.sourceFeatures.any2StringAdd)
179+
gen.mkImportFromSelector(sym, ImportSelector.mask(nme.any2stringadd) :: ImportSelector.wildList)
180+
else
181+
gen.mkWildcardImport(sym)
182+
c.make(tree = imp, unit = unit)
183+
}
179184

180185
// there must be a scala.xml package when xml literals were parsed in this unit
181186
if (unit.hasXml && ScalaXmlPackage == NoSymbol)

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,19 +1414,15 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
14141414
case _ =>
14151415
}
14161416
inferView(qual, qual.tpe, searchTemplate, reportAmbiguous, saveErrors) match {
1417-
case EmptyTree => qual
1418-
case coercion =>
1417+
case EmptyTree => qual
1418+
case coercion =>
14191419
if (settings.logImplicitConv.value)
14201420
context.echo(qual.pos, s"applied implicit conversion from ${qual.tpe} to ${searchTemplate} = ${coercion.symbol.defString}")
14211421

1422-
if (currentRun.isScala3 && coercion.symbol == currentRun.runDefinitions.Predef_any2stringaddMethod) {
1423-
if (currentRun.sourceFeatures.any2StringAdd) qual
1424-
else {
1422+
if (currentRun.isScala3 && coercion.symbol == currentRun.runDefinitions.Predef_any2stringaddMethod)
1423+
if (!currentRun.sourceFeatures.any2StringAdd)
14251424
runReporting.warning(qual.pos, s"Converting to String for concatenation is not supported in Scala 3 (or with -Xsource-features:any2stringadd).", Scala3Migration, coercion.symbol)
1426-
typedQualifier(atPos(qual.pos)(new ApplyImplicitView(coercion, List(qual))))
1427-
}
1428-
}
1429-
else typedQualifier(atPos(qual.pos)(new ApplyImplicitView(coercion, List(qual))))
1425+
typedQualifier(atPos(qual.pos)(new ApplyImplicitView(coercion, List(qual))))
14301426
}
14311427
}
14321428
else qual

test/files/neg/t13004b.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
t13004b.scala:14: applied implicit conversion from Money to ?{def + : ?} = final implicit def any2stringadd[A](self: A): any2stringadd[A]
2+
Money(3.14) + Money(1.7)
3+
^
4+
t13004b.scala:14: error: type mismatch;
5+
found : Money
6+
required: String
7+
Money(3.14) + Money(1.7)
8+
^
9+
1 error

test/files/neg/t13004b.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//> using options -Vimplicit-conversions -Xlint -Xsource:3 -Xsource-features:any2stringadd
2+
3+
import scala.Predef.*
4+
5+
case class Money(value: Double)
6+
object Money {
7+
implicit class MoneySyntax(private val self: Money) extends AnyVal {
8+
def +(other: Money): Money = Money(self.value + other.value)
9+
}
10+
}
11+
12+
object Test extends App {
13+
println {
14+
Money(3.14) + Money(1.7)
15+
}
16+
}

test/files/run/t13004.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
t13004.scala:12: applied implicit conversion from Money to ?{def + : ?} = implicit def MoneySyntax(self: Money): Money.MoneySyntax
2+
Money(3.14) + Money(1.7)
3+
^
4+
Money(4.84)

test/files/run/t13004.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//> using options -Xsource:3 -Xsource-features:any2stringadd -Vimplicit-conversions
2+
3+
case class Money(value: Double)
4+
object Money {
5+
implicit class MoneySyntax(private val self: Money) extends AnyVal {
6+
def +(other: Money): Money = Money(self.value + other.value)
7+
}
8+
}
9+
10+
object Test extends App {
11+
println {
12+
Money(3.14) + Money(1.7)
13+
}
14+
}

test/files/run/t13004c.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
t13004c.scala:9: applied implicit conversion from Money to ?{def + : ?} = final implicit def any2stringadd[A](self: A): any2stringadd[A]
2+
Money(3.14) + " is a lotta dough!"
3+
^
4+
t13004c.scala:9: warning: method any2stringadd in object Predef is deprecated (since 2.13.0): Implicit injection of + is deprecated. Convert to String to call +
5+
Money(3.14) + " is a lotta dough!"
6+
^
7+
Money(3.14) is a lotta dough!

test/files/run/t13004c.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//> using options -Vimplicit-conversions -Xlint -Xsource:3 -Xsource-features:any2stringadd
2+
3+
import scala.Predef.*
4+
5+
case class Money(value: Double)
6+
7+
object Test extends App {
8+
println {
9+
Money(3.14) + " is a lotta dough!"
10+
}
11+
}

0 commit comments

Comments
 (0)