Skip to content

Commit

Permalink
Only apply the restriction to given searches of value types
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Dec 17, 2023
1 parent 43a61ee commit e1cc2bb
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,7 @@ trait Implicits:
else candSucceedsGiven(sym.owner)

ctx.isTyper
&& pt.isValueType
&& !candSym.isOneOf(TermParamOrAccessor | Synthetic)
&& candSym.span.exists
&& candSucceedsGiven(ctx.owner)
Expand All @@ -1592,12 +1593,13 @@ trait Implicits:
def checkResolutionChange(result: SearchResult) = result match
case result: SearchSuccess
if (eligible ne preEligible) && !sourceVersion.isAtLeast(SourceVersion.`future`) =>
val newSym = result.ref.symbol
searchImplicit(preEligible.diff(eligible), contextual) match
case prevResult: SearchSuccess =>
case prevResult: SearchSuccess if prevResult.ref.symbol != newSym =>
report.error(
em"""Warning: result of implicit search for $pt will change.
|current result: ${prevResult.ref.symbol.showLocated}
|result with -source future: ${result.ref.symbol.showLocated}""",
|Current result: ${prevResult.ref.symbol.showLocated}
|Result with -source future: ${newSym.showLocated}""",
srcPos
)
case _ =>
Expand Down
12 changes: 6 additions & 6 deletions tests/neg/i15474.check
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Error: tests/neg/i15474.scala:16:56 ---------------------------------------------------------------------------------
16 | given Ordering[Price] = summon[Ordering[BigDecimal]] // error
| ^
| Warning: result of implicit search for Ordering[BigDecimal] will change.
| current result: given instance given_Ordering_Price in object Price
| result with -source future: object BigDecimal in object Ordering
-- Error: tests/neg/i15474.scala:7:56 ----------------------------------------------------------------------------------
7 | given Ordering[Price] = summon[Ordering[BigDecimal]] // error
| ^
| Warning: result of implicit search for Ordering[BigDecimal] will change.
| Current result: given instance given_Ordering_Price in object Price
| Result with -source future: object BigDecimal in object Ordering
9 changes: 0 additions & 9 deletions tests/neg/i15474.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
//> using options -Xfatal-warnings

import scala.language.implicitConversions

object Test1:
given c: Conversion[ String, Int ] with
def apply(from: String): Int = from.toInt // was error, now avoided

object Test2:
given c: Conversion[ String, Int ] = _.toInt // now avoided, was loop not detected, could be used as a fallback to avoid the warning.

object Prices {
opaque type Price = BigDecimal

Expand Down
5 changes: 5 additions & 0 deletions tests/neg/i15474b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Error: tests/neg/i15474b.scala:7:40 ---------------------------------------------------------------------------------
7 | def apply(from: String): Int = from.toInt // error
| ^^^^^^^^^^
| Infinite loop in function body
| Test1.c.apply(from).toInt
11 changes: 11 additions & 0 deletions tests/neg/i15474b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//> using options -Xfatal-warnings

import scala.language.implicitConversions

object Test1:
given c: Conversion[ String, Int ] with
def apply(from: String): Int = from.toInt // error

object Test2:
given c: Conversion[ String, Int ] = _.toInt // loop not detected, could be used as a fallback to avoid the warning.

4 changes: 2 additions & 2 deletions tests/neg/i6716.check
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
12 | given Monad[Bar] = summon[Monad[Foo]] // error
| ^
| Warning: result of implicit search for Monad[Foo] will change.
| current result: given instance given_Monad_Bar in object Bar
| result with -source future: object given_Monad_Foo in object Foo
| Current result: given instance given_Monad_Bar in object Bar
| Result with -source future: object given_Monad_Foo in object Foo
8 changes: 1 addition & 7 deletions tests/pos/i15474.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
//> using options -Xfatal-warnings

import scala.language.implicitConversions
import language.future

object Test1:
given c: Conversion[ String, Int ] with
def apply(from: String): Int = from.toInt // was error, now avoided

object Test2:
given c: Conversion[ String, Int ] = _.toInt // now avoided, was loop not detected, could be used as a fallback to avoid the warning.

object Prices {
opaque type Price = BigDecimal

Expand Down

0 comments on commit e1cc2bb

Please sign in to comment.