Skip to content

Commit 0754408

Browse files
author
Som Snytt
committed
Exclude any2stringadd from root import
1 parent f056508 commit 0754408

File tree

7 files changed

+57
-15
lines changed

7 files changed

+57
-15
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/Implicits.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,9 +1040,7 @@ trait Implicits extends splain.SplainData {
10401040

10411041
private def isIneligible(info: ImplicitInfo) = (
10421042
info.isCyclicOrErroneous
1043-
|| isView && ((info.sym eq Predef_conforms) || (info.sym eq SubType_refl) ||
1044-
currentRun.sourceFeatures.any2StringAdd && (info.sym eq currentRun.runDefinitions.Predef_any2stringaddMethod)
1045-
) // as implicit conversions, Predef.$conforms and <:<.refl are no-op, so exclude them
1043+
|| isView && ((info.sym eq Predef_conforms) || (info.sym eq SubType_refl)) // as implicit conversions, Predef.$conforms and <:<.refl are no-op, so exclude them
10461044
|| (!context.macrosEnabled && info.sym.isTermMacro)
10471045
)
10481046

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/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)