Skip to content

Commit e1cc2bb

Browse files
committed
Only apply the restriction to given searches of value types
1 parent 43a61ee commit e1cc2bb

File tree

7 files changed

+30
-27
lines changed

7 files changed

+30
-27
lines changed

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,7 @@ trait Implicits:
15821582
else candSucceedsGiven(sym.owner)
15831583

15841584
ctx.isTyper
1585+
&& pt.isValueType
15851586
&& !candSym.isOneOf(TermParamOrAccessor | Synthetic)
15861587
&& candSym.span.exists
15871588
&& candSucceedsGiven(ctx.owner)
@@ -1592,12 +1593,13 @@ trait Implicits:
15921593
def checkResolutionChange(result: SearchResult) = result match
15931594
case result: SearchSuccess
15941595
if (eligible ne preEligible) && !sourceVersion.isAtLeast(SourceVersion.`future`) =>
1596+
val newSym = result.ref.symbol
15951597
searchImplicit(preEligible.diff(eligible), contextual) match
1596-
case prevResult: SearchSuccess =>
1598+
case prevResult: SearchSuccess if prevResult.ref.symbol != newSym =>
15971599
report.error(
15981600
em"""Warning: result of implicit search for $pt will change.
1599-
|current result: ${prevResult.ref.symbol.showLocated}
1600-
|result with -source future: ${result.ref.symbol.showLocated}""",
1601+
|Current result: ${prevResult.ref.symbol.showLocated}
1602+
|Result with -source future: ${newSym.showLocated}""",
16011603
srcPos
16021604
)
16031605
case _ =>

tests/neg/i15474.check

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
-- Error: tests/neg/i15474.scala:16:56 ---------------------------------------------------------------------------------
2-
16 | given Ordering[Price] = summon[Ordering[BigDecimal]] // error
3-
| ^
4-
| Warning: result of implicit search for Ordering[BigDecimal] will change.
5-
| current result: given instance given_Ordering_Price in object Price
6-
| result with -source future: object BigDecimal in object Ordering
1+
-- Error: tests/neg/i15474.scala:7:56 ----------------------------------------------------------------------------------
2+
7 | given Ordering[Price] = summon[Ordering[BigDecimal]] // error
3+
| ^
4+
| Warning: result of implicit search for Ordering[BigDecimal] will change.
5+
| Current result: given instance given_Ordering_Price in object Price
6+
| Result with -source future: object BigDecimal in object Ordering

tests/neg/i15474.scala

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
//> using options -Xfatal-warnings
22

3-
import scala.language.implicitConversions
4-
5-
object Test1:
6-
given c: Conversion[ String, Int ] with
7-
def apply(from: String): Int = from.toInt // was error, now avoided
8-
9-
object Test2:
10-
given c: Conversion[ String, Int ] = _.toInt // now avoided, was loop not detected, could be used as a fallback to avoid the warning.
11-
123
object Prices {
134
opaque type Price = BigDecimal
145

tests/neg/i15474b.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Error: tests/neg/i15474b.scala:7:40 ---------------------------------------------------------------------------------
2+
7 | def apply(from: String): Int = from.toInt // error
3+
| ^^^^^^^^^^
4+
| Infinite loop in function body
5+
| Test1.c.apply(from).toInt

tests/neg/i15474b.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//> using options -Xfatal-warnings
2+
3+
import scala.language.implicitConversions
4+
5+
object Test1:
6+
given c: Conversion[ String, Int ] with
7+
def apply(from: String): Int = from.toInt // error
8+
9+
object Test2:
10+
given c: Conversion[ String, Int ] = _.toInt // loop not detected, could be used as a fallback to avoid the warning.
11+

tests/neg/i6716.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
12 | given Monad[Bar] = summon[Monad[Foo]] // error
33
| ^
44
| Warning: result of implicit search for Monad[Foo] will change.
5-
| current result: given instance given_Monad_Bar in object Bar
6-
| result with -source future: object given_Monad_Foo in object Foo
5+
| Current result: given instance given_Monad_Bar in object Bar
6+
| Result with -source future: object given_Monad_Foo in object Foo

tests/pos/i15474.scala

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
//> using options -Xfatal-warnings
2+
23
import scala.language.implicitConversions
34
import language.future
45

5-
object Test1:
6-
given c: Conversion[ String, Int ] with
7-
def apply(from: String): Int = from.toInt // was error, now avoided
8-
9-
object Test2:
10-
given c: Conversion[ String, Int ] = _.toInt // now avoided, was loop not detected, could be used as a fallback to avoid the warning.
11-
126
object Prices {
137
opaque type Price = BigDecimal
148

0 commit comments

Comments
 (0)