Skip to content

Commit 59cbbe1

Browse files
committed
Support implicit classes
1 parent 636a430 commit 59cbbe1

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package tests.anImplicitClass
2+
3+
implicit class AddingOps(n: Int):
4+
def inc: Int = n + 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package tests.justmethods
2+
3+
trait Animal
4+
5+
def whatSoundDoIMake(animal: Animal): String = "IoIo"

scaladoc/src/dotty/tools/scaladoc/tasty/InkuireSupport.scala

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,20 @@ trait InkuireSupport:
7272
!s.flags.is(Flags.Override) &&
7373
!s.flags.is(Flags.Synthetic)
7474

75+
if classDef.symbol.isImplicitClass then // Implicit classes
76+
classDef.symbol.maybeOwner.declarations
77+
.filter { methodSymbol =>
78+
methodSymbol.name == classDef.symbol.name && methodSymbol.flags.is(Flags.Implicit) && methodSymbol.flags.is(Flags.Method)
79+
}
80+
.foreach(handleImplicitConversion(_, variableNames))
81+
7582
classDef.symbol.declaredMethods
7683
.filter(viableSymbol)
7784
.tap { _.foreach { // Loop for implicit conversions
7885
case implicitConversion: Symbol if implicitConversion.flags.is(Flags.Implicit)
7986
&& classDef.symbol.flags.is(Flags.Module)
8087
&& implicitConversion.owner.fullName == ("scala.Predef$") =>
81-
val defdef = implicitConversion.tree.asInstanceOf[DefDef]
82-
val to = defdef.returnTpt.asInkuire(variableNames)
83-
val from = defdef.paramss.flatMap(_.params).collectFirst {
84-
case v: ValDef => v.tpt.asInkuire(variableNames)
85-
}
86-
(from, to) match
87-
case (Some(from: Inkuire.Type), to: Inkuire.Type) => Inkuire.db = Inkuire.db.copy(implicitConversions = Inkuire.db.implicitConversions :+ (from.itid.get -> to))
88-
case _ =>
88+
handleImplicitConversion(implicitConversion, variableNames)
8989
case _ =>
9090
}}
9191
.tap { _.foreach { // Loop for functions and vals
@@ -150,6 +150,17 @@ trait InkuireSupport:
150150
}
151151
}
152152

153+
private def handleImplicitConversion(implicitConversion: Symbol, variableNames: Set[String]) = {
154+
val defdef = implicitConversion.tree.asInstanceOf[DefDef]
155+
val to = defdef.returnTpt.asInkuire(variableNames)
156+
val from = defdef.paramss.flatMap(_.params).collectFirst {
157+
case v: ValDef => v.tpt.asInkuire(variableNames)
158+
}
159+
(from, to) match
160+
case (Some(from: Inkuire.Type), to: Inkuire.Type) => Inkuire.db = Inkuire.db.copy(implicitConversions = Inkuire.db.implicitConversions :+ (from.itid.get -> to))
161+
case _ =>
162+
}
163+
153164
private def nameAndOwnerName(classDef: ClassDef, symbol: Symbol): (String, String) =
154165
if classDef.symbol.flags.is(Flags.Module)
155166
&& (classDef.symbol.companionClass != Symbol.noSymbol || (Seq("apply", "unapply").contains(symbol.name))) then
@@ -168,6 +179,7 @@ trait InkuireSupport:
168179
else if sym == defn.EmptyPackageClass then List.empty
169180
else if sym == defn.RootPackage then List.empty
170181
else if sym == defn.RootClass then List.empty
182+
else if sym.normalizedName.contains("$package") then ownerNameChain(sym.owner)
171183
else ownerNameChain(sym.owner) :+ sym.normalizedName
172184

173185
private def paramsForClass(classDef: ClassDef, vars: Set[String]): Seq[Inkuire.Variance] =

scaladoc/src/dotty/tools/scaladoc/tasty/SymOps.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ object SymOps:
1212

1313
extension (using Quotes)(sym: reflect.Symbol)
1414

15+
def isImplicitClass: Boolean =
16+
import reflect._
17+
sym.maybeOwner != Symbol.noSymbol
18+
&& sym.maybeOwner.declaredMethods.exists { methodSymbol =>
19+
methodSymbol.name == sym.name && methodSymbol.flags.is(Flags.Implicit) && methodSymbol.flags.is(Flags.Method)
20+
}
21+
1522
def packageName: String =
1623
if (sym.isPackageDef) sym.fullName
1724
else sym.maybeOwner.packageName
@@ -91,7 +98,9 @@ object SymOps:
9198
Flags.Open -> Modifier.Open,
9299
Flags.Override -> Modifier.Override,
93100
Flags.Case -> Modifier.Case,
94-
).collect { case (flag, mod) if sym.flags.is(flag) => mod }
101+
).collect {
102+
case (flag, mod) if sym.flags.is(flag) => mod
103+
} ++ Seq(Modifier.Implicit).filter(_ => sym.isImplicitClass)
95104

96105
def isHiddenByVisibility(using dctx: DocContext): Boolean =
97106
import VisibilityScope._

0 commit comments

Comments
 (0)