Skip to content

Commit af4c365

Browse files
committed
Add doc comment that explains what checkLooingImplicits does
1 parent cbdf40a commit af4c365

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@ object CheckLoopingImplicits:
1212
val name: String = "checkLoopingImplicits"
1313
val description: String = "check that implicit defs do not call themselves in an infinite loop"
1414

15-
/** Checks that implicit defs do not call themselves in an infinite loop */
15+
/** Checks that some definitions do not call themselves in an infinite loop
16+
* This is an incomplete check, designed to catch some likely bugs instead
17+
* of being exhaustive. The situations where infinite loops are diagnosed are
18+
* 1. A given method should not directly call itself
19+
* 2. An apply method in a given object should not directly call itself
20+
* 3. A lazy val should not directly force itself
21+
* 4. An extension method should not directly call itself
22+
*
23+
* In all these cases, there are some situations which would not lead to
24+
* an infinite loop at runtime. For instance, the call could go at runtime to an
25+
* overriding version of the method or val which breaks the loop. That's why
26+
* this phase only issues warnings, not errors, and also why we restrict
27+
* checks to the 4 cases above, where a recursion is somewhat hidden.
28+
* There are also other more complicated calling patterns that could also
29+
* be diagnosed as loops with more effort. This could be improved in the future.
30+
*/
1631
class CheckLoopingImplicits extends MiniPhase:
1732
thisPhase =>
1833
import tpd._

tests/neg-custom-args/fatal-warnings/i15474.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import scala.language.implicitConversions
22

3-
given Conversion[ String, Int ] with
4-
def apply(from: String): Int = from.toInt // error
3+
object Test1:
4+
given c: Conversion[ String, Int ] with
5+
def apply(from: String): Int = from.toInt // error
6+
7+
object Test2:
8+
given c: Conversion[ String, Int ] = _.toInt // loop not detected, could be used as a fallback to avoid the warning.
59

610
object Prices {
711
opaque type Price = BigDecimal

0 commit comments

Comments
 (0)