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 1 commit
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
40 changes: 28 additions & 12 deletions compiler/src/dotty/tools/dotc/semanticdb/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TypeOps:
)(using Context): Option[Symbol] =
symtab.get((binder, name))

extension [T <: LambdaType](symtab: mutable.Map[(T, Name), Symbol])
extension [T <: LambdaType | RefinedType](symtab: mutable.Map[(T, Name), Symbol])
private def lookupOrErr(
binder: T,
name: Name,
Expand Down Expand Up @@ -228,26 +228,45 @@ class TypeOps:
val stpe = loop(tpe)
s.ByNameType(stpe)

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

// sym of `TypeRef(_, sym)` may not be a Symbol but Name in some cases
// e.g. in MatchType,
// case x *: xs => x *: Concat[xs, Ys]
// x and xs should have a typebounds <: Any, >: Nothing
// but Any (and Nothing) are represented as TypeRef(<scala>, "Any" <- Name)
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) =>
// 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
case TypeRef(pre, name: Name) =>
val spre = if tpe.hasTrivialPrefix then s.Type.Empty else loop(pre)
val ssym = sym.symbolName
s.SingleType(spre, ssym)
val maybeSym = pre.widen.dealias match
case rt: RefinedType =>
refinementSymtab.lookupOrErr(rt, name, rt.typeSymbol)
case _ => None
maybeSym match
case Some(sym) =>
s.TypeRef(spre, sym.symbolName, Seq.empty)
case None => s.Type.Empty

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 TermRef(pre, name: Name) =>
val spre = if tpe.hasTrivialPrefix then s.Type.Empty else loop(pre)
val maybeSym = pre.widen.dealias match
case rt: RefinedType =>
refinementSymtab.lookupOrErr(rt, name, rt.typeSymbol)
case _ => None
maybeSym match
case Some(sym) =>
s.SingleType(spre, sym.symbolName)
case None => s.Type.Empty

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

Expand Down Expand Up @@ -458,9 +477,6 @@ 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)
Expand Down
9 changes: 9 additions & 0 deletions tests/semanticdb/expect/dep-match.expect.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object Test_depmatch/*<-_empty_::Test_depmatch.*/ {
type Foo/*<-_empty_::Test_depmatch.Foo#*/ = Int/*->scala::Int#*/ { type U/*<-local0*/ }
type Bar/*<-_empty_::Test_depmatch.Bar#*/[T/*<-_empty_::Test_depmatch.Bar#[T]*/] = T/*->_empty_::Test_depmatch.Bar#[T]*/ match {
case Unit/*->scala::Unit#*/ => Unit/*->scala::Unit#*/
}
inline def baz/*<-_empty_::Test_depmatch.baz().*/(foo/*<-_empty_::Test_depmatch.baz().(foo)*/: Foo/*->_empty_::Test_depmatch.Foo#*/): Unit/*->scala::Unit#*/ = {
val v/*<-local1*/: Bar/*->_empty_::Test_depmatch.Bar#*/[foo/*->_empty_::Test_depmatch.baz().(foo)*/.U] = ???/*->scala::Predef.`???`().*/
}
}
9 changes: 9 additions & 0 deletions tests/semanticdb/expect/dep-match.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object Test_depmatch {
type Foo = Int { type U }
type Bar[T] = T match {
case Unit => Unit
}
inline def baz(foo: Foo): Unit = {
val v: Bar[foo.U] = ???
}
}
44 changes: 42 additions & 2 deletions tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ advanced/Structural# => class Structural extends Object { self: Structural => +6
advanced/Structural#T# => trait T [typeparam A ] extends Object { self: T[A] => +4 decls }
advanced/Structural#T#[A] => typeparam A
advanced/Structural#T#`<init>`(). => primary ctor <init> [typeparam A ](): T[A]
advanced/Structural#T#bar(). => method bar (param b: <?>): Unit
advanced/Structural#T#bar().(b) => param b: <?>
advanced/Structural#T#bar(). => method bar (param b: foo.B): Unit
advanced/Structural#T#bar().(b) => param b: foo.B
advanced/Structural#T#foo. => val method foo Object { type B = A }
advanced/Structural#`<init>`(). => primary ctor <init> (): Structural
advanced/Structural#s1(). => method s1 => Object { abstract val method x Int }
Expand Down Expand Up @@ -3703,6 +3703,46 @@ Occurrences:
[4:18..4:21): Int -> scala/Int#
[4:26..4:30): Unit -> scala/Unit#

expect/dep-match.scala
----------------------

Summary:
Schema => SemanticDB v4
Uri => dep-match.scala
Text => empty
Language => Scala
Symbols => 8 entries
Occurrences => 17 entries

Symbols:
_empty_/Test_depmatch. => final object Test_depmatch extends Object { self: Test_depmatch.type => +4 decls }
_empty_/Test_depmatch.Bar# => type Bar [typeparam T ] = T match { Unit => Unit }
_empty_/Test_depmatch.Bar#[T] => typeparam T
_empty_/Test_depmatch.Foo# => type Foo = Int { type U }
_empty_/Test_depmatch.baz(). => inline macro baz (param foo: Foo): Unit
_empty_/Test_depmatch.baz().(foo) => param foo: Foo
local0 => type U
local1 => val local v: Bar[foo.U]

Occurrences:
[0:7..0:20): Test_depmatch <- _empty_/Test_depmatch.
[1:7..1:10): Foo <- _empty_/Test_depmatch.Foo#
[1:13..1:16): Int -> scala/Int#
[1:24..1:25): U <- local0
[2:7..2:10): Bar <- _empty_/Test_depmatch.Bar#
[2:11..2:12): T <- _empty_/Test_depmatch.Bar#[T]
[2:16..2:17): T -> _empty_/Test_depmatch.Bar#[T]
[3:9..3:13): Unit -> scala/Unit#
[3:17..3:21): Unit -> scala/Unit#
[5:13..5:16): baz <- _empty_/Test_depmatch.baz().
[5:17..5:20): foo <- _empty_/Test_depmatch.baz().(foo)
[5:22..5:25): Foo -> _empty_/Test_depmatch.Foo#
[5:28..5:32): Unit -> scala/Unit#
[6:8..6:9): v <- local1
[6:11..6:14): Bar -> _empty_/Test_depmatch.Bar#
[6:15..6:18): foo -> _empty_/Test_depmatch.baz().(foo)
[6:24..6:27): ??? -> scala/Predef.`???`().

expect/exports-example-Codec.scala
----------------------------------

Expand Down