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 8 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
29 changes: 16 additions & 13 deletions compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -420,19 +420,22 @@ class ExtractSemanticDB extends Phase:
else
val symkinds = mutable.HashSet.empty[SymbolKind]
tree match
case tree: ValDef =>
if !tree.symbol.is(Param) then
symkinds += (if tree.mods is Mutable then SymbolKind.Var else SymbolKind.Val)
if tree.rhs.isEmpty && !tree.symbol.isOneOf(TermParam | CaseAccessor | ParamAccessor) then
symkinds += SymbolKind.Abstract
case tree: DefDef =>
if tree.isSetterDef then
symkinds += SymbolKind.Setter
else if tree.rhs.isEmpty then
symkinds += SymbolKind.Abstract
case tree: Bind =>
symkinds += SymbolKind.Val
case _ =>
case tree: ValDef =>
if !tree.symbol.is(Param) then
symkinds += (if tree.mods is Mutable then SymbolKind.Var else SymbolKind.Val)
if tree.rhs.isEmpty && !tree.symbol.isOneOf(TermParam | CaseAccessor | ParamAccessor) then
symkinds += SymbolKind.Abstract
case tree: DefDef =>
if tree.isSetterDef then
symkinds += SymbolKind.Setter
else if tree.rhs.isEmpty then
symkinds += SymbolKind.Abstract
// if symbol isType, it's type variable
case tree: Bind if (!tree.symbol.isType) =>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added !tree.symbol.isType here, to misinterpret the type variable symbol as Val
This change makes type variables like local0 => case val method N$1 <: Nat to local0 => case type N$1 <: Nat.

Other diffs: just indented.

symkinds += SymbolKind.Val
case tree: Bind if (tree.symbol.isType) =>
symkinds += SymbolKind.TypeVal
case _ =>
symkinds.toSet

private def ctorParams(
Expand Down
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/semanticdb/PPrint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/semanticdb/Scala3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,13 @@ object Scala3:
enum SymbolKind derives CanEqual:
kind =>

case Val, Var, Setter, Abstract
case Val, Var, Setter, Abstract, TypeVal

def isVar: Boolean = kind match
case Var | Setter => true
case _ => false
def isVal: Boolean = kind == Val
def isTypeVal: Boolean = kind == TypeVal
def isVarOrVal: Boolean = kind.isVar || kind.isVal

end SymbolKind
Expand Down Expand Up @@ -309,7 +310,9 @@ object Scala3:
props |= SymbolInformation.Property.IMPLICIT.value
if sym.is(Lazy, butNot=Module) then
props |= SymbolInformation.Property.LAZY.value
if sym.isAllOf(Case | Module) || sym.is(CaseClass) || sym.isAllOf(EnumCase) then
if sym.isAllOf(Case | Module) ||
(sym.is(CaseClass) && !symkinds.exists(_.isTypeVal)) || // `t` of `case List[t] =>` (which has `CaseClass` flag) shouldn't be `CASE`
sym.isAllOf(EnumCase) then
props |= SymbolInformation.Property.CASE.value
if sym.is(Covariant) then
props |= SymbolInformation.Property.COVARIANT.value
Expand Down
51 changes: 48 additions & 3 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 @@ -228,11 +233,21 @@ class TypeOps:
val ssym = sym.symbolName
s.TypeRef(spre, ssym, Seq.empty)

case tr @ TypeRef(pre, _) if tr.symbol != NoSymbol =>
val spre = if tpe.hasTrivialPrefix then s.Type.Empty else loop(pre)
val ssym = tr.symbol.symbolName
s.TypeRef(spre, ssym, Seq.empty)

case TermRef(pre, sym: Symbol) =>
val spre = if(tpe.hasTrivialPrefix) s.Type.Empty else loop(pre)
val spre = if tpe.hasTrivialPrefix then s.Type.Empty else loop(pre)
val ssym = sym.symbolName
s.SingleType(spre, ssym)

case tr @ TermRef(pre, _) if tr.symbol != NoSymbol =>
val spre = if(tpe.hasTrivialPrefix) s.Type.Empty else loop(pre)
val ssym = tr.symbol.symbolName
s.SingleType(spre, ssym)

case ThisType(TypeRef(_, sym: Symbol)) =>
s.ThisType(sym.symbolName)

Expand Down Expand Up @@ -276,6 +291,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 +445,6 @@ class TypeOps:
// Not yet supported
case _: HKTypeLambda =>
s.Type.Empty
case _: MatchType =>
s.Type.Empty

case tvar: TypeVar =>
loop(tvar.stripped)
Expand All @@ -420,11 +458,18 @@ class TypeOps:
private def hasTrivialPrefix(using Context): Boolean =
def checkTrivialPrefix(pre: Type, sym: Symbol)(using Context): Boolean =
pre =:= sym.owner.thisType
// Make sure `tr.symbol != NoSymbol` where `tr @ TypeRef(...)`, that happens
// when TypeRef refers the refinement of RefinedType e.g.
// TypeRef for `foo.B` in `trait T[A] { val foo: { type B = A } = ???; def bar(b: foo.B) = () }` has NoSymbol
tpe match {
case TypeRef(pre, sym: Symbol) =>
checkTrivialPrefix(pre, sym)
case tr @ TypeRef(pre, _) if tr.symbol != NoSymbol =>
checkTrivialPrefix(pre, tr.symbol)
case TermRef(pre, sym: Symbol) =>
checkTrivialPrefix(pre, sym)
case tr @ TermRef(pre, _) if tr.symbol != NoSymbol =>
checkTrivialPrefix(pre, tr.symbol)
case _ => false
}

Expand Down
Loading