Skip to content

Commit 9d6e87a

Browse files
authored
Merge pull request #15047 from rochala/fix-type-alias-completion
Fix type alias completion, unify completion tests style
2 parents b1f00a7 + b5e9178 commit 9d6e87a

16 files changed

+763
-713
lines changed

compiler/src/dotty/tools/dotc/interactive/Completion.scala

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import dotty.tools.dotc.core.Symbols.{Symbol, defn}
1616
import dotty.tools.dotc.core.StdNames.nme
1717
import dotty.tools.dotc.core.SymDenotations.SymDenotation
1818
import dotty.tools.dotc.core.TypeError
19-
import dotty.tools.dotc.core.Types.{ExprType, MethodOrPoly, NameFilter, NoType, TermRef, Type}
19+
import dotty.tools.dotc.core.Types.{AppliedType, ExprType, MethodOrPoly, NameFilter, NoType, TermRef, Type}
2020
import dotty.tools.dotc.parsing.Tokens
2121
import dotty.tools.dotc.util.Chars
2222
import dotty.tools.dotc.util.SourcePosition
2323

2424
import scala.collection.mutable
2525
import scala.util.control.NonFatal
26+
import dotty.tools.dotc.core.Types.TypeRef
2627

2728
/**
2829
* One of the results of a completion query.
@@ -310,28 +311,30 @@ object Completion {
310311
resultMappings
311312
}
312313

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

319322
/** Completions for selections from a term.
320323
* Direct members take priority over members from extensions
321324
* and so do members from extensions over members from implicit conversions
322325
*/
323326
def selectionCompletions(qual: Tree)(using Context): CompletionMap =
324-
val reducedQual = reduceUnderlyingMatchType(qual)
327+
val adjustedQual = widenQualifier(qual)
325328

326-
implicitConversionMemberCompletions(reducedQual) ++
327-
extensionCompletions(reducedQual) ++
328-
directMemberCompletions(reducedQual)
329+
implicitConversionMemberCompletions(adjustedQual) ++
330+
extensionCompletions(adjustedQual) ++
331+
directMemberCompletions(adjustedQual)
329332

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

379382
/** Completions from implicit conversions including old style extensions using implicit classes */
380383
private def implicitConversionMemberCompletions(qual: Tree)(using Context): CompletionMap =
381-
if qual.tpe.widenDealias.isExactlyNothing || qual.tpe.isNullType then
384+
if qual.tpe.isExactlyNothing || qual.tpe.isNullType then
382385
Map.empty
383386
else
384387
val membersFromConversion =

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,22 +1522,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
15221522
assignType(cpy.Closure(tree)(env1, meth1, target), meth1, target)
15231523
}
15241524

1525-
/** Extractor for match types hidden behind an AppliedType/MatchAlias */
1526-
object MatchTypeInDisguise {
1527-
def unapply(tp: AppliedType)(using Context): Option[MatchType] = tp match {
1528-
case AppliedType(tycon: TypeRef, args) =>
1529-
tycon.info match {
1530-
case MatchAlias(alias) =>
1531-
alias.applyIfParameterized(args) match {
1532-
case mt: MatchType => Some(mt)
1533-
case _ => None
1534-
}
1535-
case _ => None
1536-
}
1537-
case _ => None
1538-
}
1539-
}
1540-
15411525
def typedMatch(tree: untpd.Match, pt: Type)(using Context): Tree =
15421526
tree.selector match {
15431527
case EmptyTree =>
@@ -1565,6 +1549,21 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
15651549
val selType = rawSelectorTpe match
15661550
case c: ConstantType if tree.isInline => c
15671551
case otherTpe => otherTpe.widen
1552+
/** Extractor for match types hidden behind an AppliedType/MatchAlias */
1553+
object MatchTypeInDisguise {
1554+
def unapply(tp: AppliedType)(using Context): Option[MatchType] = tp match {
1555+
case AppliedType(tycon: TypeRef, args) =>
1556+
tycon.info match {
1557+
case MatchAlias(alias) =>
1558+
alias.applyIfParameterized(args) match {
1559+
case mt: MatchType => Some(mt)
1560+
case _ => None
1561+
}
1562+
case _ => None
1563+
}
1564+
case _ => None
1565+
}
1566+
}
15681567

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

0 commit comments

Comments
 (0)