Skip to content

Commit a008ca2

Browse files
committed
Find more looping implicits
1. Also check apply methods of companions of implicit or given classes. Specifically apply methods of implicit Conversions. 2. Look inside Inlined nodes to detect loops. Fixes #15474 Fixes #10947
1 parent f58c158 commit a008ca2

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

compiler/src/dotty/tools/dotc/transform/CheckLoopingImplicits.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import MegaPhase.MiniPhase
66
import Contexts.*, Types.*, Symbols.*, SymDenotations.*, Flags.*
77
import ast.*
88
import Decorators.*
9-
9+
import StdNames.*
1010

1111
object CheckLoopingImplicits:
1212
val name: String = "checkLoopingImplicits"
@@ -60,6 +60,9 @@ class CheckLoopingImplicits extends MiniPhase:
6060
case Block(stats, expr) =>
6161
stats.foreach(checkNotLooping)
6262
checkNotLooping(expr)
63+
case Inlined(_, bindings, expr) =>
64+
bindings.foreach(checkNotLooping)
65+
checkNotLooping(expr)
6366
case Typed(expr, _) =>
6467
checkNotLooping(expr)
6568
case Assign(lhs, rhs) =>
@@ -84,7 +87,9 @@ class CheckLoopingImplicits extends MiniPhase:
8487
checkNotLooping(t.rhs)
8588
case _ =>
8689

87-
if sym.isOneOf(GivenOrImplicit | Lazy | ExtensionMethod) then
90+
if sym.isOneOf(GivenOrImplicit | Lazy | ExtensionMethod)
91+
|| sym.name == nme.apply && sym.owner.is(Module) && sym.owner.sourceModule.isOneOf(GivenOrImplicit)
92+
then
8893
checkNotLooping(mdef.rhs)
8994
mdef
9095
end transform
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.language.implicitConversions
2+
3+
given Conversion[ String, Int ] with
4+
def apply(from: String): Int = from.toInt // error
5+
6+
object Prices {
7+
opaque type Price = BigDecimal
8+
9+
object Price{
10+
given Ordering[Price] = summon[Ordering[BigDecimal]] // error
11+
}
12+
}

0 commit comments

Comments
 (0)