Skip to content
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

add completions for specific MatchType cases #14639

Merged
merged 4 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/interactive/Completion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ object Completion {
def selectionCompletions(qual: Tree)(using Context): CompletionMap =
implicitConversionMemberCompletions(qual) ++
extensionCompletions(qual) ++
matchTypeCompletions(qual) ++
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this shouldn't be a separate case but we should rather ensure that match types used as method receivers are properly reduced in all other cases. We should also add test cases for them. For some reason in case of extension methods surprisingly this seemed to work even without this PR but still we should add a test to avoid regressions. We should also make sure this works well with implicit conversions.

directMemberCompletions(qual)

/** Completions for members of `qual`'s type.
Expand Down Expand Up @@ -362,6 +363,12 @@ object Completion {
implicitConversionTargets(qual)(using ctx.fresh.setExploreTyperState()).flatMap(accessibleMembers)
membersFromConversion.toSeq.groupByName

/** Completions for derived members of `MatchType`'s type. */
def matchTypeCompletions(qual: Tree)(using Context): CompletionMap =
qual.tpe.widenDealias match
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should only dealias here but not widen

case ctx.typer.MatchTypeInDisguise(mt) => accessibleMembers(mt.reduced).groupByName
case _ => Map.empty

/** Completions from extension methods */
private def extensionCompletions(qual: Tree)(using Context): CompletionMap =
def asDefLikeType(tpe: Type): Type = tpe match
Expand Down
31 changes: 16 additions & 15 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,22 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
assignType(cpy.Closure(tree)(env1, meth1, target), meth1, target)
}

/** Extractor for match types hidden behind an AppliedType/MatchAlias */
object MatchTypeInDisguise {
def unapply(tp: AppliedType)(using Context): Option[MatchType] = tp match {
case AppliedType(tycon: TypeRef, args) =>
tycon.info match {
case MatchAlias(alias) =>
alias.applyIfParameterized(args) match {
case mt: MatchType => Some(mt)
case _ => None
}
case _ => None
}
case _ => None
}
}

def typedMatch(tree: untpd.Match, pt: Type)(using Context): Tree =
tree.selector match {
case EmptyTree =>
Expand Down Expand Up @@ -1526,21 +1542,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
val selType = rawSelectorTpe match
case c: ConstantType if tree.isInline => c
case otherTpe => otherTpe.widen
/** Extractor for match types hidden behind an AppliedType/MatchAlias */
object MatchTypeInDisguise {
def unapply(tp: AppliedType): Option[MatchType] = tp match {
case AppliedType(tycon: TypeRef, args) =>
tycon.info match {
case MatchAlias(alias) =>
alias.applyIfParameterized(args) match {
case mt: MatchType => Some(mt)
case _ => None
}
case _ => None
}
case _ => None
}
}

/** Does `tree` has the same shape as the given match type?
* We only support typed patterns with empty guards, but
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1121,4 +1121,20 @@ class CompletionTest {
| val x = Bar.`fo${m1}"""
.withSource.completion(m1, expected)
}

@Test def matchTypeCompletion: Unit = {
val expected = Set(
("map", Method, "[B](f: Int => B): Foo[B]"),
)
code"""trait Foo[A] {
| def map[B](f: A => B): Foo[B]
|}
|case class Bar[F[_]](bar: F[Int])
|type M[T] = T match {
| case Int => Foo[Int]
|}
|def foo(x: Bar[M]) = x.bar.m${m1}"""
.withSource.completion(m1, expected)

}
}