bug
bug copied to clipboard
warn on implicit def without explicit result type
The following code snipet : https://gist.github.com/1427587 does not compile.
import java.util.Date
trait TDate
trait TT[A1,T1]
trait TTFactory[F,G] {
def create(f: F) : TT[F,G]
def sample: F
}
object Impls {
// If the c1 is declared before c2, it compiles fine
// or if the implicit's result type is specified explicitly
implicit def c2(s: Date)/* : TT[Date, TDate] */ = c1.create(s)
implicit val c1 = new TTFactory[Date,TDate] {
def create(v: Date): TT[Date,TDate] = sys.error("")
def sample = new Date
}
}
Imported From: https://issues.scala-lang.org/browse/SI-5265?orig=1 Reporter: maxime.levesque Affected Versions: 2.9.1 See #5348, #2206, #801
@adriaanm said: see #2206, #801 and a plethora of duplicates
@adriaanm said: warn unless enabled by feature flag
@retronym said: I have a feeling there actually isn't a cycle in this example. Even if there is, we should report it.
Instead, I think the problem here is that typedBlock can trigger a type completion which causes the typedBlock for the same block to be reentered. Crucially, this happens after the DefTrees in the block have the symbols assigned. In the second run through typedBlock, the namer sees the attributed trees and decides not to enter them in scope.
The presentation compiler has a more robust namer to account for the sort of symptom, as caused by cancellation. I've moved that in to the regular namer in this WIP branch.
https://github.com/retronym/scala/tree/ticket/5265
That said, I definetely agree we should also have a warning / migration for unannotated implicits.
@retronym said: That branch fails on (at least)
// t4716.scala
trait Bug2[ +A] extends TraversableOnce[A] {
def ++[B >: A](that: TraversableOnce[B]) = {
lazy val it = that.toIterator
it
}
}
I'm going to park this ticket again.
This'd be a great project for somebody.
It has increased significance lately given that Scala 3 requires the explicit result type:
scala> implicit def x = 3
-- Error: ----------------------------------------------------------------------
1 |implicit def x = 3
| ^
| result type of implicit definition needs to be given explicitly
1 error found
So I would say that -Xlint should warn about this by default (since including explicit result types has been known at least since 2011 to be good practice), and -Xsource:3 should turn that warning into error.
Is there a volunteer who would like to tackle it?
To whoever would like to take this on, note that the warning should not apply to local definitions, i.e., those for which sym.isLocalToBlock is true. Such definitions are only visible after they're defined, and hence doing cause problematic cycles. Early experimentation in dotty discovered that they had to be allowed to preserve reasonable code patterns.
This'd be a great project for somebody.
Possible typo for Som, homebody. Or is that a portmanteau?
Possible noise reduction if it accepted
implicit def f = foo.asInstanceOf[Bar]
implicit def g = foo: Bar
The first form, in particular, adds information. The second could be transposed.
The current exemption is for override in Scala 3/-Xsource:3.