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 5 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
40 changes: 38 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 @@ -228,6 +233,14 @@ class TypeOps:
val ssym = sym.symbolName
s.TypeRef(spre, ssym, Seq.empty)

case TypeRef(pre, sym: Name) =>
// val spre = if tpe.hasTrivialPrefix then s.Type.Empty else loop(pre)
// Craft semanticdb symbol
// prefix symbol (e.g. "scala/") + name (e.g. "Nothing") + Global symbol suffix ("#")
// see: https://scalameta.org/docs/semanticdb/specification.html#symbol
val ssym = s"${pre.typeSymbol.symbolName}${sym.mangledString}#"
s.TypeRef(s.Type.Empty, ssym, Seq.empty)
Copy link
Member Author

Choose a reason for hiding this comment

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

x *: xs part of

type Concat[Xs <: Tuple, +Ys <: Tuple] <: Tuple = Xs match
  case EmptyTuple => Ys
  case x *: xs => x *: Concat[xs, Ys]

have TypeRef(..., Nothing) (where Nothing is just a Name instead of Symbol), and we can't access the symbol for Nothing here. Therefore, craft the symbol for TyperRef for this.

Copy link
Member

Choose a reason for hiding this comment

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

Can we also try to compute the type of the prefix in case is is not empty?

Copy link
Member

@bishabosha bishabosha Mar 21, 2022

Choose a reason for hiding this comment

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

also perhaps this could be merged with the branch above (so only test for case tr @ TypeRef(pre, _)) - tr.symbol will just return the symbol if there already is one

Copy link
Member Author

Choose a reason for hiding this comment

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

also perhaps this could be merged with the branch above

Right!

2693983

Copy link
Member

Choose a reason for hiding this comment

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

it seems calling checkTrivialPrefix is causing a lot of crashes because it is potentially checking the owner of NoSymbol


case TermRef(pre, sym: Symbol) =>
val spre = if(tpe.hasTrivialPrefix) s.Type.Empty else loop(pre)
val ssym = sym.symbolName
Expand Down Expand Up @@ -276,6 +289,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 +443,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