Skip to content

Support MatchType for SemanticDB #14608

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 15 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/semanticdb/PPrint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class SymbolInformationPrinter (symtab: PrinterSymtab):
if (tparams.infos.nonEmpty)
sb.append(tparams.infos.map(pprintDef).mkString("[", ", ", "]"))
if (lo == hi) {
sb.append(s" = ${pprint(lo)}")
if (lo == Type.Empty) ()
else sb.append(s" = ${pprint(lo)}")
} else {
lo match
case TypeRef(Type.Empty, "scala/Nothing#", Nil) => ()
Expand Down Expand Up @@ -191,7 +192,12 @@ class SymbolInformationPrinter (symtab: PrinterSymtab):
s"=> ${normal(utpe)}"
case RepeatedType(utpe) =>
s"${normal(utpe)}*"
case _ =>
case MatchType(scrutinee, cases) =>
val casesStr = cases.map { caseType =>
s"${pprint(caseType.key)} => ${pprint(caseType.body)}"
}.mkString(", ")
s"${pprint(scrutinee)} match { ${casesStr} }"
case x =>
"<?>"

def normal(tpe: Type): String = tpe match
Expand Down
32 changes: 30 additions & 2 deletions compiler/src/dotty/tools/dotc/semanticdb/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import collection.mutable

import dotty.tools.dotc.{semanticdb => s}
import Scala3.{FakeSymbol, SemanticSymbol, WildcardTypeSymbol, TypeParamRefSymbol, TermParamRefSymbol, RefinementSymbol}
import dotty.tools.dotc.core.Names.Designator

class TypeOps:
import SymbolScopeOps._
Expand Down Expand Up @@ -81,6 +82,10 @@ class TypeOps:
else
enterParamRef(lam.resType)

// for CaseType `case Array[t] => t` which is represented as [t] =>> MatchCase[Array[t], t]
case m: MatchType =>
m.cases.foreach(enterParamRef)

// for class constructor
// class C[T] { ... }
case cls: ClassInfo if sym.info.isInstanceOf[LambdaType] =>
Expand Down Expand Up @@ -276,6 +281,31 @@ class TypeOps:
case ConstantType(const) =>
s.ConstantType(const.toSemanticConst)

case matchType: MatchType =>
val scases = matchType.cases.map { caseType => caseType match {
case lam: HKTypeLambda => // case Array[t] => t
val paramSyms = lam.paramNames.flatMap { paramName =>
val key = (lam, paramName)
paramRefSymtab.get(key)
}.sscope
lam.resType match {
case defn.MatchCase(key, body) =>
s.MatchType.CaseType(
loop(key),
loop(body)
)
case _ => s.MatchType.CaseType() // shouldn't happen
}
case defn.MatchCase(key, body) =>
val skey = loop(key)
val sbody = loop(body)
s.MatchType.CaseType(skey, sbody)
case _ => s.MatchType.CaseType() // shouldn't happen
}}
val sscrutinee = loop(matchType.scrutinee)
val sbound = loop(matchType.bound)
s.MatchType(sscrutinee, scases)

case rt @ RefinedType(parent, name, info) =>
// `X { def x: Int; def y: Int }`
// RefinedType(
Expand Down Expand Up @@ -405,8 +435,6 @@ class TypeOps:
// Not yet supported
case _: HKTypeLambda =>
s.Type.Empty
case _: MatchType =>
s.Type.Empty

case tvar: TypeVar =>
loop(tvar.stripped)
Expand Down
Loading