Skip to content

[SemanticDB] Support TypeApply Synthetic #13481

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 1 commit into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ class ExtractSemanticDB extends Phase:

case tree: Inlined =>
traverse(tree.call)

case tree: TypeApply =>
synth.tryFindSynthetic(tree).foreach(synthetics.addOne)
traverseChildren(tree)

case tree: TypeTree =>
tree.typeOpt match
// Any types could be appear inside of `TypeTree`, but
Expand Down
48 changes: 44 additions & 4 deletions compiler/src/dotty/tools/dotc/semanticdb/SyntheticsExtractor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,44 @@ import scala.collection.mutable
class SyntheticsExtractor:
import Scala3.{_, given}

val visited = collection.mutable.HashSet[Tree]()

def tryFindSynthetic(tree: Tree)(using Context, SemanticSymbolBuilder, TypeOps): Option[s.Synthetic] =
extension (synth: s.Synthetic)
def toOpt: Some[s.Synthetic] = Some(synth)

if tree.span.isSynthetic then
val forSynthetic = tree match // not yet supported (for synthetics)
case tree: Apply if isForSynthetic(tree) => true
case tree: TypeApply if isForSynthetic(tree) => true
case _ => false

if visited.contains(tree) || forSynthetic then None
else
tree match
case tree: Apply if isForSynthetic(tree) =>
None // not yet supported (for synthetics)
case tree: TypeApply
if tree.span.isSynthetic &&
tree.args.forall(arg => !arg.symbol.is(Scala2x)) &&
!tree.span.isZeroExtent =>
visited.add(tree)
val fnTree = tree.fun match
// Something like `List.apply[Int](1,2,3)`
case select @ Select(qual, _) if isSyntheticName(select) =>
s.SelectTree(
s.OriginalTree(range(qual.span, tree.source)),
Some(select.toSemanticId)
)
case _ =>
s.OriginalTree(
range(tree.fun.span, tree.source)
)
val targs = tree.args.map(targ => targ.tpe.toSemanticType(targ.symbol)(using LinkMode.SymlinkChildren))
s.Synthetic(
range(tree.span, tree.source),
s.TypeApplyTree(
fnTree, targs
)
).toOpt

case tree: Apply
if tree.args.nonEmpty &&
tree.args.forall(arg =>
Expand Down Expand Up @@ -46,7 +76,6 @@ class SyntheticsExtractor:
).toOpt

case _ => None
else None

private given TreeOps: AnyRef with
extension (tree: Tree)
Expand Down Expand Up @@ -95,4 +124,15 @@ class SyntheticsExtractor:
case select: Select => isForComprehensionSyntheticName(select)
case _ => false

private def isSyntheticName(select: Select): Boolean =
select.span.toSynthetic == select.qualifier.span.toSynthetic && (
select.name == nme.apply ||
select.name == nme.update ||
select.name == nme.foreach ||
select.name == nme.withFilter ||
select.name == nme.flatMap ||
select.name == nme.map ||
select.name == nme.unapplySeq ||
select.name == nme.unapply)

end SyntheticsExtractor
Loading