Skip to content

Commit f866a7b

Browse files
committed
improvement: Also don't dealias when inserting type or on hover
1 parent 11c3afa commit f866a7b

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

presentation-compiler/src/main/dotty/tools/pc/HoverProvider.scala

+9-7
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ object HoverProvider:
110110
if symbol.name == nme.selectDynamic || symbol.name == nme.applyDynamic =>
111111
fallbackToDynamics(path, printer, contentType)
112112
case symbolTpes @ ((symbol, tpe, None) :: _) =>
113-
val exprTpw = tpe.widenTermRefExpr.deepDealias
113+
val exprTpw = dealiased(tpe.widenTermRefExpr)
114114
val hoverString =
115115
tpw match
116116
// https://github.com/scala/scala3/issues/8891
@@ -125,7 +125,7 @@ object HoverProvider:
125125
if tpe != NoType then tpe
126126
else tpw
127127

128-
printer.hoverSymbol(sym, finalTpe.deepDealias)
128+
printer.hoverSymbol(sym, dealiased(finalTpe))
129129
end match
130130
end hoverString
131131

@@ -134,7 +134,7 @@ object HoverProvider:
134134
.map(_.docstring())
135135
.mkString("\n")
136136

137-
val expresionTypeOpt =
137+
val expresionTypeOpt =
138138
if symbol.name == StdNames.nme.??? then
139139
InferExpectedType(search, driver, params).infer()
140140
else printer.expressionType(exprTpw)
@@ -161,7 +161,7 @@ object HoverProvider:
161161
ju.Optional.empty().nn
162162
end match
163163
case (_, tpe, Some(namedTupleArg)) :: _ =>
164-
val exprTpw = tpe.widenTermRefExpr.deepDealias
164+
val exprTpw = dealiased(tpe.widenTermRefExpr)
165165
printer.expressionType(exprTpw) match
166166
case Some(tpe) =>
167167
ju.Optional.of(
@@ -179,6 +179,8 @@ object HoverProvider:
179179
end if
180180
end hover
181181

182+
private def dealiased(tpe: Type)(using Context): Type = if tpe.isNamedTupleType then tpe else tpe.deepDealias
183+
182184
extension (pos: SourcePosition)
183185
private def isPoint: Boolean = pos.start == pos.end
184186

@@ -194,7 +196,7 @@ object HoverProvider:
194196
val resultType =
195197
rest match
196198
case Select(_, asInstanceOf) :: TypeApply(_, List(tpe)) :: _ if asInstanceOf == nme.asInstanceOfPM =>
197-
tpe.tpe.widenTermRefExpr.deepDealias
199+
dealiased(tpe.tpe.widenTermRefExpr)
198200
case _ if n == nme.selectDynamic => tpe.resultType
199201
case _ => tpe
200202

@@ -220,9 +222,9 @@ object HoverProvider:
220222
findRefinement(parent)
221223
case _ => None
222224

223-
val refTpe = sel.typeOpt.widen.deepDealias match
225+
val refTpe = dealiased(sel.typeOpt.widen) match
224226
case r: RefinedType => Some(r)
225-
case t: (TermRef | TypeProxy) => Some(t.termSymbol.info.deepDealias)
227+
case t: (TermRef | TypeProxy) => Some(dealiased(t.termSymbol.info))
226228
case _ => None
227229

228230
refTpe.flatMap(findRefinement).asJava

presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ final class InferredTypeProvider(
9999
case AppliedType(tycon, args) =>
100100
isInScope(tycon) && args.forall(isInScope)
101101
case _ => true
102-
if isInScope(tpe)
103-
then tpe
102+
if isInScope(tpe) then tpe
103+
else if tpe.isNamedTupleType then tpe
104104
else tpe.deepDealias
105105

106106
val printer = ShortenedTypePrinter(

presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala

+21
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,27 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
970970
|""".stripMargin
971971
)
972972

973+
@Test def `named-tuples` =
974+
checkEdit(
975+
"""|def hello = (path = ".", num = 5)
976+
|
977+
|def <<test>> =
978+
| hello ++ (line = 1)
979+
|
980+
|@main def bla =
981+
| val x: (path: String, num: Int, line: Int) = test
982+
|""".stripMargin,
983+
"""|import scala.NamedTuple.Concat
984+
|def hello = (path = ".", num = 5)
985+
|
986+
|def test: Concat[(path : String, num : Int), (line : Int)] =
987+
| hello ++ (line = 1)
988+
|
989+
|@main def bla =
990+
| val x: (path: String, num: Int, line: Int) = test
991+
|""".stripMargin
992+
)
993+
973994
def checkEdit(
974995
original: String,
975996
expected: String

presentation-compiler/test/dotty/tools/pc/tests/hover/HoverTermSuite.scala

+29
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,35 @@ class HoverTermSuite extends BaseHoverSuite:
745745
"name: String".hover
746746
)
747747

748+
749+
@Test def `named-tuples3`: Unit =
750+
check(
751+
"""|def hello = (path = ".", num = 5)
752+
|
753+
|def test =
754+
| hello ++ (line = 1)
755+
|
756+
|@main def bla =
757+
| val x: (path: String, num: Int, line: Int) = t@@est
758+
|""".stripMargin,
759+
"def test: Concat[(path : String, num : Int), (line : Int)]".hover
760+
)
761+
762+
763+
@Test def `named-tuples4`: Unit =
764+
check(
765+
"""|def hello = (path = ".", num = 5)
766+
|
767+
|def test =
768+
| hel@@lo ++ (line = 1)
769+
|
770+
|@main def bla =
771+
| val x: (path: String, num: Int, line: Int) = test
772+
|""".stripMargin,
773+
"def hello: (path : String, num : Int)".hover
774+
)
775+
776+
748777
@Test def `value-of`: Unit =
749778
check(
750779
"""|enum Foo(val key: String) {

0 commit comments

Comments
 (0)