-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Check that right hand sides of implicit defs don't loop (directly) #13589
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
compiler/src/dotty/tools/dotc/transform/CheckLoopingImplicits.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package dotty.tools.dotc | ||
package transform | ||
|
||
import core.* | ||
import MegaPhase.MiniPhase | ||
import Contexts.*, Types.*, Symbols.*, SymDenotations.*, Flags.* | ||
import ast.* | ||
import Trees.* | ||
import Decorators.* | ||
|
||
import annotation.threadUnsafe | ||
|
||
object CheckLoopingImplicits: | ||
val name: String = "checkLoopingImplicits" | ||
|
||
/** Checks that implicit defs do not call themselves in an infinite loop */ | ||
class CheckLoopingImplicits extends MiniPhase: | ||
thisPhase => | ||
import tpd._ | ||
|
||
override def phaseName: String = CheckLoopingImplicits.name | ||
|
||
override def transformDefDef(mdef: DefDef)(using Context): DefDef = | ||
val sym = mdef.symbol | ||
|
||
def checkNotSelfRef(t: RefTree) = | ||
if t.symbol eq sym then | ||
report.warning( | ||
em"""Infinite loop in function body | ||
|${mdef.rhs}""", | ||
mdef.rhs.srcPos | ||
) | ||
|
||
def checkNotLooping(t: Tree): Unit = t match | ||
case t: Ident => | ||
checkNotSelfRef(t) | ||
case t @ Select(qual, _) => | ||
checkNotSelfRef(t) | ||
checkNotLooping(qual) | ||
case Apply(fn, args) => | ||
checkNotLooping(fn) | ||
fn.tpe.widen match | ||
case mt: MethodType => | ||
args.lazyZip(mt.paramInfos).foreach { (arg, pinfo) => | ||
if !pinfo.isInstanceOf[ExprType] then checkNotLooping(arg) | ||
} | ||
case _ => | ||
case TypeApply(fn, _) => | ||
checkNotLooping(fn) | ||
case Block(stats, expr) => | ||
stats.foreach(checkNotLooping) | ||
checkNotLooping(expr) | ||
case Typed(expr, _) => | ||
checkNotLooping(expr) | ||
case Assign(lhs, rhs) => | ||
checkNotLooping(lhs) | ||
checkNotLooping(rhs) | ||
case If(cond, _, _) => | ||
checkNotLooping(cond) | ||
case Match(selector, _) => | ||
checkNotLooping(selector) | ||
case Labeled(_, expr) => | ||
checkNotLooping(expr) | ||
case Return(expr, _) => | ||
checkNotLooping(expr) | ||
case WhileDo(cond, _) => | ||
checkNotLooping(cond) | ||
case Try(block, _, finalizer) => | ||
checkNotLooping(block) | ||
checkNotLooping(finalizer) | ||
case SeqLiteral(elems, _) => | ||
elems.foreach(checkNotLooping) | ||
case t: ValDef => | ||
if !t.symbol.is(Lazy) then checkNotLooping(t.rhs) | ||
case _ => | ||
|
||
if sym.isOneOf(GivenOrImplicit) then | ||
checkNotLooping(mdef.rhs) | ||
mdef | ||
end transformDefDef | ||
end CheckLoopingImplicits |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import scala.language.implicitConversions | ||
|
||
case class Foo(i: Int) extends AnyVal: | ||
def toFoo = this | ||
|
||
case class Bar(i: Int) extends AnyVal | ||
|
||
class BarOps(bar: Bar): | ||
def toFoo = Foo(bar.i) | ||
|
||
implicit def augmentBar(bar: Bar): BarOps = BarOps(bar) | ||
|
||
def lazyIdentity[T](x: => T): T = x | ||
def repIdentity[T](x: T*): T = x(0) | ||
|
||
val x1 = | ||
implicit def barToFoo(bar: Bar): Foo = bar.toFoo // error: infinite loop in function body | ||
val foo: Foo = Bar(1) | ||
|
||
val x2 = | ||
implicit def barToFoo2(bar: Bar): Foo = | ||
identity(bar.toFoo) // error | ||
val foo: Foo = Bar(1) | ||
|
||
val x3 = | ||
implicit def barToFoo3(bar: Bar): Foo = | ||
lazyIdentity(bar.toFoo) // OK | ||
val foo: Foo = Bar(1) | ||
|
||
val x4 = | ||
implicit def barToFoo4(bar: Bar): Foo = | ||
repIdentity(bar.toFoo) // error | ||
val foo: Foo = Bar(1) | ||
|
||
val x5 = | ||
implicit def barToFoo4(bar: Bar): Foo = | ||
val y = bar.toFoo // error | ||
y | ||
val foo: Foo = Bar(1) | ||
|
||
val x6 = | ||
implicit def barToFoo4(bar: Bar): Foo = | ||
lazy val y = bar.toFoo // OK | ||
if false then y else ??? | ||
val foo: Foo = Bar(1) | ||
|
||
|
||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to not extend this to non-implicit definitions too? It would help with extension methods too for example: #9880