Skip to content

Commit e5a9355

Browse files
authored
improvement: Simplify match types when possible (#23146)
Fixes scalameta/metals#7004
1 parent d9adc0f commit e5a9355

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

presentation-compiler/src/main/dotty/tools/pc/printer/ShortenedTypePrinter.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ class ShortenedTypePrinter(
351351
val paramLabelss = label(methodParams)
352352
val extLabelss = label(extParams)
353353

354-
val returnType = tpe(gtpe.finalResultType)
354+
val retType = gtpe.finalResultType
355+
val simplified = if (retType.typeSymbol.isAliasType) retType else retType.deepDealiasAndSimplify
356+
val returnType = tpe(simplified)
355357
def extensionSignatureString =
356358
val extensionSignature = paramssString(extLabelss, extParams)
357359
if extParams.nonEmpty then

presentation-compiler/src/main/dotty/tools/pc/utils/InteractiveEnrichments.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,13 @@ object InteractiveEnrichments extends CommonMtagsEnrichments:
407407
case app @ AppliedType(tycon, params) =>
408408
AppliedType(tycon, params.map(_.deepDealiasAndSimplify))
409409
case aliasingBounds: AliasingBounds =>
410-
aliasingBounds.derivedAlias(aliasingBounds.alias.dealias)
410+
aliasingBounds.derivedAlias(aliasingBounds.alias.deepDealiasAndSimplify)
411411
case TypeBounds(lo, hi) =>
412412
TypeBounds(lo.dealias, hi.dealias)
413413
case RefinedType(parent, name, refinedInfo) =>
414414
RefinedType(parent.dealias, name, refinedInfo.deepDealiasAndSimplify)
415415
case dealised => dealised
416-
if tpe.isNamedTupleType then dealiased.simplified else dealiased
416+
dealiased.simplified
417417

418418
extension[T] (list: List[T])
419419
def get(n: Int): Option[T] = if 0 <= n && n < list.size then Some(list(n)) else None

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala

+9-9
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,18 @@ class CompletionSuite extends BaseCompletionSuite:
9494
|newBuilder[A]: Builder[A, List[A]]
9595
|apply[A](elems: A*): List[A]
9696
|concat[A](xss: Iterable[A]*): List[A]
97-
|fill[A](n1: Int, n2: Int)(elem: => A): List[List[A] @uncheckedVariance]
98-
|fill[A](n1: Int, n2: Int, n3: Int)(elem: => A): List[List[List[A]] @uncheckedVariance]
99-
|fill[A](n1: Int, n2: Int, n3: Int, n4: Int)(elem: => A): List[List[List[List[A]]] @uncheckedVariance]
100-
|fill[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(elem: => A): List[List[List[List[List[A]]]] @uncheckedVariance]
97+
|fill[A](n1: Int, n2: Int)(elem: => A): List[List[A]]
98+
|fill[A](n1: Int, n2: Int, n3: Int)(elem: => A): List[List[List[A]]]
99+
|fill[A](n1: Int, n2: Int, n3: Int, n4: Int)(elem: => A): List[List[List[List[A]]]]
100+
|fill[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(elem: => A): List[List[List[List[List[A]]]]]
101101
|fill[A](n: Int)(elem: => A): List[A]
102102
|iterate[A](start: A, len: Int)(f: A => A): List[A]
103103
|range[A: Integral](start: A, end: A): List[A]
104104
|range[A: Integral](start: A, end: A, step: A): List[A]
105-
|tabulate[A](n1: Int, n2: Int)(f: (Int, Int) => A): List[List[A] @uncheckedVariance]
106-
|tabulate[A](n1: Int, n2: Int, n3: Int)(f: (Int, Int, Int) => A): List[List[List[A]] @uncheckedVariance]
107-
|tabulate[A](n1: Int, n2: Int, n3: Int, n4: Int)(f: (Int, Int, Int, Int) => A): List[List[List[List[A]]] @uncheckedVariance]
108-
|tabulate[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(f: (Int, Int, Int, Int, Int) => A): List[List[List[List[List[A]]]] @uncheckedVariance]
105+
|tabulate[A](n1: Int, n2: Int)(f: (Int, Int) => A): List[List[A]]
106+
|tabulate[A](n1: Int, n2: Int, n3: Int)(f: (Int, Int, Int) => A): List[List[List[A]]]
107+
|tabulate[A](n1: Int, n2: Int, n3: Int, n4: Int)(f: (Int, Int, Int, Int) => A): List[List[List[List[A]]]]
108+
|tabulate[A](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(f: (Int, Int, Int, Int, Int) => A): List[List[List[List[List[A]]]]]
109109
|tabulate[A](n: Int)(f: Int => A): List[A]
110110
|unapplySeq[A](x: List[A] @uncheckedVariance): UnapplySeqWrapper[A]
111111
|unfold[A, S](init: S)(f: S => Option[(A, S)]): List[A]
@@ -116,7 +116,7 @@ class CompletionSuite extends BaseCompletionSuite:
116116
|ensuring(cond: List.type => Boolean, msg: => Any): List.type
117117
|fromSpecific(from: Any)(it: IterableOnce[Nothing]): List[Nothing]
118118
|fromSpecific(it: IterableOnce[Nothing]): List[Nothing]
119-
|nn: List.type & List.type
119+
|nn: List.type
120120
|runtimeChecked scala.collection.immutable
121121
|toFactory(from: Any): Factory[Nothing, List[Nothing]]
122122
|formatted(fmtstr: String): String

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionTypeSuite.scala

+30
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,33 @@ class CompletionTypeSuite extends BaseCompletionSuite:
2121
s"""|method[T <: FS](a: T): Nothing
2222
|""".stripMargin
2323
)
24+
25+
@Test def `long-types-match` =
26+
check(
27+
s"""|@main
28+
|def run =
29+
| (1, 2).to@@
30+
|""".stripMargin,
31+
s"""|toString(): String
32+
|toArray: Array[Object]
33+
|toIArray: IArray[Object]
34+
|toList: List[Int]
35+
|productIterator: Iterator[Any]
36+
|asInstanceOf[X0]: X0
37+
|isInstanceOf[X0]: Boolean
38+
|""".stripMargin
39+
)
40+
41+
@Test def `union-types` =
42+
check(
43+
s"""|
44+
|def itsAUnionType(): Int | String = ???
45+
|
46+
|def hello =
47+
| its@@
48+
|
49+
|
50+
|""".stripMargin,
51+
s"""|itsAUnionType(): Int | String
52+
|""".stripMargin
53+
)

0 commit comments

Comments
 (0)