Skip to content

Commit ab0ea23

Browse files
committed
Allow toplevel implicits
1 parent b069f8d commit ab0ea23

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
@@ -1028,7 +1028,8 @@ object desugar {
10281028
def packageDef(pdef: PackageDef)(implicit ctx: Context): PackageDef = {
10291029
def needsObject(stat: Tree) = stat match {
10301030
case _: ValDef | _: PatDef | _: DefDef => true
1031-
case stat: TypeDef => !stat.isClassDef
1031+
case stat: ModuleDef => stat.mods.is(Implicit)
1032+
case stat: TypeDef => !stat.isClassDef || stat.mods.is(Implicit)
10321033
case _ => false
10331034
}
10341035
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)