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

Fix type alias completion, unify completion tests style #15047

Merged
merged 4 commits into from
Apr 28, 2022
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
25 changes: 14 additions & 11 deletions compiler/src/dotty/tools/dotc/interactive/Completion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import dotty.tools.dotc.core.Symbols.{Symbol, defn}
import dotty.tools.dotc.core.StdNames.nme
import dotty.tools.dotc.core.SymDenotations.SymDenotation
import dotty.tools.dotc.core.TypeError
import dotty.tools.dotc.core.Types.{ExprType, MethodOrPoly, NameFilter, NoType, TermRef, Type}
import dotty.tools.dotc.core.Types.{AppliedType, ExprType, MethodOrPoly, NameFilter, NoType, TermRef, Type}
import dotty.tools.dotc.parsing.Tokens
import dotty.tools.dotc.util.Chars
import dotty.tools.dotc.util.SourcePosition

import scala.collection.mutable
import scala.util.control.NonFatal
import dotty.tools.dotc.core.Types.TypeRef

/**
* One of the results of a completion query.
Expand Down Expand Up @@ -310,28 +311,30 @@ object Completion {
resultMappings
}

/** Replaces underlying type with reduced one, when it's MatchType */
def reduceUnderlyingMatchType(qual: Tree)(using Context): Tree=
qual.tpe.widen match
case ctx.typer.MatchTypeInDisguise(mt) => qual.withType(mt)
/** Widen only those types which are applied or are exactly nothing
*/
def widenQualifier(qual: Tree)(using Context): Tree =
qual.tpe.widenDealias match
case widenedType if widenedType.isExactlyNothing => qual.withType(widenedType)
case appliedType: AppliedType => qual.withType(appliedType)
case _ => qual

/** Completions for selections from a term.
* Direct members take priority over members from extensions
* and so do members from extensions over members from implicit conversions
*/
def selectionCompletions(qual: Tree)(using Context): CompletionMap =
val reducedQual = reduceUnderlyingMatchType(qual)
val adjustedQual = widenQualifier(qual)

implicitConversionMemberCompletions(reducedQual) ++
extensionCompletions(reducedQual) ++
directMemberCompletions(reducedQual)
rochala marked this conversation as resolved.
Show resolved Hide resolved
implicitConversionMemberCompletions(adjustedQual) ++
extensionCompletions(adjustedQual) ++
directMemberCompletions(adjustedQual)

/** Completions for members of `qual`'s type.
* These include inherited definitions but not members added by extensions or implicit conversions
*/
def directMemberCompletions(qual: Tree)(using Context): CompletionMap =
if qual.tpe.widenDealias.isExactlyNothing then
if qual.tpe.isExactlyNothing then
Map.empty
else
accessibleMembers(qual.tpe).groupByName
Expand Down Expand Up @@ -378,7 +381,7 @@ object Completion {

/** Completions from implicit conversions including old style extensions using implicit classes */
private def implicitConversionMemberCompletions(qual: Tree)(using Context): CompletionMap =
if qual.tpe.widenDealias.isExactlyNothing || qual.tpe.isNullType then
if qual.tpe.isExactlyNothing || qual.tpe.isNullType then
Map.empty
else
val membersFromConversion =
Expand Down
31 changes: 15 additions & 16 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1522,22 +1522,6 @@ 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 @@ -1565,6 +1549,21 @@ 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 */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverts changes from https://github.com/lampepfl/dotty/pull/14639/files#diff-8c9ece1772bd78160fc1c31e988664586c9df566a1d22ff99ef99dd6d5627a90R1518

Match Type completion is also fixed by this commit without using this extractor.

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
}
}

/** Does `tree` has the same shape as the given match type?
* We only support typed patterns with empty guards, but
Expand Down
Loading