Skip to content

Commit d93ac50

Browse files
committed
Allow toplevel implicits
1 parent c0e3456 commit d93ac50

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,8 @@ object desugar {
960960
def packageDef(pdef: PackageDef)(implicit ctx: Context): PackageDef = {
961961
def needsObject(stat: Tree) = stat match {
962962
case _: ValDef | _: PatDef | _: DefDef => true
963-
case stat: TypeDef => !stat.isClassDef
963+
case stat: ModuleDef => stat.mods.is(Implicit)
964+
case stat: TypeDef => !stat.isClassDef || stat.mods.is(Implicit)
964965
case _ => false
965966
}
966967
val (nestedStats, topStats) = pdef.stats.partition(needsObject)

tests/run/toplevel-implicits.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(C(),C())
2+
(a D,a D)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Test extends App {
2+
import implicits._
3+
4+
val c = new C
5+
val c2 = c.pair(c)
6+
println(c2)
7+
8+
val d = new D
9+
val d2 = d.pair(d)
10+
println(d2)
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package implicits
2+
3+
case class C()
4+
5+
implicit object Cops {
6+
def (x: C) pair (y: C) = (x, y)
7+
}
8+
9+
class D {
10+
override def toString = "a D"
11+
}
12+
13+
implicit class Ddeco(x: D) {
14+
def pair(y: D) = (x, y)
15+
}

0 commit comments

Comments
 (0)