Skip to content

Find more looping implicits #15481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import MegaPhase.MiniPhase
import Contexts.*, Types.*, Symbols.*, SymDenotations.*, Flags.*
import ast.*
import Decorators.*

import StdNames.*

object CheckLoopingImplicits:
val name: String = "checkLoopingImplicits"
Expand Down Expand Up @@ -60,6 +60,9 @@ class CheckLoopingImplicits extends MiniPhase:
case Block(stats, expr) =>
stats.foreach(checkNotLooping)
checkNotLooping(expr)
case Inlined(_, bindings, expr) =>
bindings.foreach(checkNotLooping)
checkNotLooping(expr)
case Typed(expr, _) =>
checkNotLooping(expr)
case Assign(lhs, rhs) =>
Expand All @@ -84,7 +87,9 @@ class CheckLoopingImplicits extends MiniPhase:
checkNotLooping(t.rhs)
case _ =>

if sym.isOneOf(GivenOrImplicit | Lazy | ExtensionMethod) then
if sym.isOneOf(GivenOrImplicit | Lazy | ExtensionMethod)
|| sym.name == nme.apply && sym.owner.is(Module) && sym.owner.sourceModule.isOneOf(GivenOrImplicit)
Copy link
Contributor

@julienrf julienrf Jun 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment to explain the intent behind the condition on the right side of the ||? I wonder if this condition is too strong, but I am not sure. Do we have existing “pos” tests (the PR only contains two “neg” tests)?

then
checkNotLooping(mdef.rhs)
mdef
end transform
Expand Down
12 changes: 12 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i15474.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.language.implicitConversions

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

object Prices {
opaque type Price = BigDecimal

object Price{
given Ordering[Price] = summon[Ordering[BigDecimal]] // error
}
}
132 changes: 132 additions & 0 deletions tests/neg/i13044.check
Original file line number Diff line number Diff line change
@@ -1,3 +1,135 @@
-- Warning: tests/neg/i13044.scala:50:40 -------------------------------------------------------------------------------
50 | implicit def typeSchema: Schema[A] = Schema.gen // error // error
| ^^^^^^^^^^
|Infinite loop in function body
|{
| val SchemaDerivation_this: Schema.type = Schema
| {
| val SchemaDerivation_this: (SchemaDerivation_this : Schema.type) = SchemaDerivation_this
| {
| val $scrutinee1:
| scala.deriving.Mirror.Product{
| MirroredMonoType = A; MirroredType = A; MirroredLabel = ("A" : String); MirroredElemTypes = (A, B);
| MirroredElemLabels = (("a" : String), ("b" : String))
| }
| =
| A.$asInstanceOf[
| scala.deriving.Mirror.Product{
| MirroredMonoType = A; MirroredType = A; MirroredLabel = ("A" : String); MirroredElemTypes = (A, B);
| MirroredElemLabels = (("a" : String), ("b" : String))
| }
| ]
| val m:
| scala.deriving.Mirror.Product{
| MirroredMonoType = A; MirroredType = A; MirroredLabel = ("A" : String); MirroredElemTypes = (A, B);
| MirroredElemLabels = (("a" : String), ("b" : String))
| }
| = $scrutinee1
| lazy val fields: List[Schema[Any]] =
| {
| val SchemaDerivation_this: (SchemaDerivation_this : (SchemaDerivation_this : Schema.type)) =
| SchemaDerivation_this
| {
| val builder: Schema[Any] = TestApp.typeSchema.asInstanceOf[Schema[Any]]
| {
| val SchemaDerivation_this:
| (SchemaDerivation_this : (SchemaDerivation_this : (SchemaDerivation_this : Schema.type)))
| = SchemaDerivation_this
| (
| {
| val builder: Schema[Any] =
| {
| val SchemaDerivation_this: Schema.type = Schema
| (
| {
| val SchemaDerivation_this: (SchemaDerivation_this : Schema.type) = SchemaDerivation_this
| {
| val $scrutinee4:
| scala.deriving.Mirror.Product{
| MirroredMonoType = B; MirroredType = B; MirroredLabel = ("B" : String);
| MirroredElemTypes = C *: EmptyTuple.type
| ; MirroredElemLabels = ("c" : String) *: EmptyTuple.type
| }
| =
| B.$asInstanceOf[
| scala.deriving.Mirror.Product{
| MirroredMonoType = B; MirroredType = B; MirroredLabel = ("B" : String);
| MirroredElemTypes = C *: EmptyTuple.type
| ; MirroredElemLabels = ("c" : String) *: EmptyTuple.type
| }
| ]
| val m:
| scala.deriving.Mirror.Product{
| MirroredMonoType = B; MirroredType = B; MirroredLabel = ("B" : String);
| MirroredElemTypes = C *: EmptyTuple.type
| ; MirroredElemLabels = ("c" : String) *: EmptyTuple.type
| }
| = $scrutinee4
| lazy val fields: List[Schema[Any]] =
| {
| val SchemaDerivation_this:
| (SchemaDerivation_this : (SchemaDerivation_this : Schema.type))
| = SchemaDerivation_this
| {
| val builder: Schema[Any] =
| {
| val SchemaDerivation_this: Schema.type = Schema
| (
| {
| val SchemaDerivation_this: (...SchemaDerivation_this : ....type) =
| SchemaDerivation_this
| {
| val $scrutinee6:
| ...{
| MirroredMonoType...; MirroredType...; MirroredLabel...;
| MirroredElemTypes...
| ; MirroredElemLabels...
| }
| = ....$asInstanceOf[...]
| val m: ... = ...$scrutinee6
| lazy val fields: ... =
| {
| val SchemaDerivation_this: ... = ...
| ...:...
| }
| {
| final class $anon() extends ...(), ... {
| def build: ... = ...
| }
| ...():...
| }
| }:...[...]
| }
| :Schema[C])
| }.asInstanceOf[Schema[Any]]
| SchemaDerivation_this.recurse[EmptyTuple.type].::[Schema[Any]](builder)
| }:List[Schema[Any]]
| }
| {
| final class $anon() extends Object(), Schema[B] {
| def build: B = ???
| }
| new Object with Schema[B] {...}():Schema[B]
| }
| }:Schema[B]
| }
| :Schema[B])
| }.asInstanceOf[Schema[Any]]
| SchemaDerivation_this.recurse[EmptyTuple.type].::[Schema[Any]](builder)
| }
| :List[Schema[Any]])
| }.::[Schema[Any]](builder)
| }:List[Schema[Any]]
| }
| {
| final class $anon() extends Object(), Schema[A] {
| def build: A = ???
| }
| new Object with Schema[A] {...}():Schema[A]
| }
| }:Schema[A]
| }:Schema[A]
|}
-- Error: tests/neg/i13044.scala:50:40 ---------------------------------------------------------------------------------
50 | implicit def typeSchema: Schema[A] = Schema.gen // error // error
| ^^^^^^^^^^
Expand Down