Skip to content

improvement: Don't dealias named tuples for type hints #23013

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 12 additions & 9 deletions presentation-compiler/src/main/dotty/tools/pc/HoverProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,23 @@ object HoverProvider:
if symbol.name == nme.selectDynamic || symbol.name == nme.applyDynamic =>
fallbackToDynamics(path, printer, contentType)
case symbolTpes @ ((symbol, tpe, None) :: _) =>
val exprTpw = tpe.widenTermRefExpr.deepDealias
val exprTpw = dealiased(tpe.widenTermRefExpr)
val hoverString =
tpw match
// https://github.com/scala/scala3/issues/8891
case tpw: ImportType =>
printer.hoverSymbol(symbol, symbol.paramRef)
case _ =>
val (tpe, sym) =
val (innerTpe, sym) =
if symbol.isType then (symbol.typeRef, symbol)
else enclosing.head.seenFrom(symbol)

val finalTpe =
if tpe != NoType then tpe
if tpe.isNamedTupleType then tpe.widenTermRefExpr
else if innerTpe != NoType then innerTpe
else tpw

printer.hoverSymbol(sym, finalTpe.deepDealias)
printer.hoverSymbol(sym, dealiased(finalTpe))
end match
end hoverString

Expand All @@ -134,7 +135,7 @@ object HoverProvider:
.map(_.docstring())
.mkString("\n")

val expresionTypeOpt =
val expresionTypeOpt =
if symbol.name == StdNames.nme.??? then
InferExpectedType(search, driver, params).infer()
else printer.expressionType(exprTpw)
Expand All @@ -161,7 +162,7 @@ object HoverProvider:
ju.Optional.empty().nn
end match
case (_, tpe, Some(namedTupleArg)) :: _ =>
val exprTpw = tpe.widenTermRefExpr.deepDealias
val exprTpw = dealiased(tpe.widenTermRefExpr)
printer.expressionType(exprTpw) match
case Some(tpe) =>
ju.Optional.of(
Expand All @@ -179,6 +180,8 @@ object HoverProvider:
end if
end hover

private def dealiased(tpe: Type)(using Context): Type = if tpe.isNamedTupleType then tpe.deepDealias.simplified else tpe.deepDealias

extension (pos: SourcePosition)
private def isPoint: Boolean = pos.start == pos.end

Expand All @@ -194,7 +197,7 @@ object HoverProvider:
val resultType =
rest match
case Select(_, asInstanceOf) :: TypeApply(_, List(tpe)) :: _ if asInstanceOf == nme.asInstanceOfPM =>
tpe.tpe.widenTermRefExpr.deepDealias
dealiased(tpe.tpe.widenTermRefExpr)
case _ if n == nme.selectDynamic => tpe.resultType
case _ => tpe

Expand All @@ -220,9 +223,9 @@ object HoverProvider:
findRefinement(parent)
case _ => None

val refTpe = sel.typeOpt.widen.deepDealias match
val refTpe = dealiased(sel.typeOpt.widen) match
case r: RefinedType => Some(r)
case t: (TermRef | TypeProxy) => Some(t.termSymbol.info.deepDealias)
case t: (TermRef | TypeProxy) => Some(dealiased(t.termSymbol.info))
case _ => None

refTpe.flatMap(findRefinement).asJava
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ final class InferredTypeProvider(
case AppliedType(tycon, args) =>
isInScope(tycon) && args.forall(isInScope)
case _ => true
if isInScope(tpe)
then tpe
if isInScope(tpe) then tpe
else if tpe.isNamedTupleType then tpe.dealias.simplified
else tpe.deepDealias

val printer = ShortenedTypePrinter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ object MetalsInteractive:
// Handle select on named tuples
case (Apply(Apply(TypeApply(fun, List(t1, t2)), List(ddef)), List(Literal(Constant(i: Int))))) :: _
if fun.symbol.exists && fun.symbol.name == nme.apply &&
fun.symbol.owner.exists && fun.symbol.owner == getModuleIfDefined("scala.NamedTuple").moduleClass =>
fun.symbol.owner.exists && fun.symbol.owner == defn.NamedTupleModule.moduleClass =>
def getIndex(t: Tree): Option[Type] =
t.tpe.dealias match
case AppliedType(_, args) => args.get(i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class PcInlayHintsProvider(
if isInScope(tpe) then tpe
else tpe.deepDealias(using indexedCtx.ctx)

val dealiased = optDealias(tpe)
val dealiased = if tpe.isNamedTupleType then optDealias(tpe).simplified else optDealias(tpe)
val tpeStr = printer.tpe(dealiased)
val usedRenames = printer.getUsedRenames
val parts = partsFromType(dealiased, usedRenames)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ class ShortenedTypePrinter(
case ConstantType(const) => toText(const)
case _ => toTextRef(tp) ~ ".type"

def tpe(tpe: Type): String = toText(tpe).mkString(defaultWidth, false)
def tpe(tpe: Type): String =
val dealiased = if (tpe.isNamedTupleType) tpe.deepDealias.simplified else tpe
toText(dealiased).mkString(defaultWidth, false)

def hoverSymbol(sym: Symbol, info: Type)(using Context): String =
val typeSymbol = info.typeSymbol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,21 @@ class CompletionSuite extends BaseCompletionSuite:
filter = _.contains("name")
)

@Test def `namedTuple-completions-2` =
check(
"""|import scala.NamedTuple.*
|
|def hello = (path = ".", num = 5)++ (line = 1)
|val hello2 = (path = ".", num = 5)++ (line = 1)
|
|@main def bla =
| hello@@
|""".stripMargin,
"""|hello2: (path : String, num : Int, line : Int)
|hello: (path : String, num : Int, line : Int)
""".stripMargin,
)

@Test def `Selectable with namedTuple Fields member` =
check(
"""|import scala.NamedTuple.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,26 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
|""".stripMargin
)

@Test def `named-tuples` =
checkEdit(
"""|def hello = (path = ".", num = 5)
|
|def <<test>> =
| hello ++ (line = 1)
|
|@main def bla =
| val x: (path: String, num: Int, line: Int) = test
|""".stripMargin,
"""|def hello = (path = ".", num = 5)
|
|def test: (path : String, num : Int, line : Int) =
| hello ++ (line = 1)
|
|@main def bla =
| val x: (path: String, num: Int, line: Int) = test
|""".stripMargin
)

def checkEdit(
original: String,
expected: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,47 @@ class HoverTermSuite extends BaseHoverSuite:
"name: String".hover
)


@Test def `named-tuples3`: Unit =
check(
"""|def hello = (path = ".", num = 5)
|
|def test =
| hello ++ (line = 1)
|
|@main def bla =
| val x: (path: String, num: Int, line: Int) = t@@est
|""".stripMargin,
"def test: (path : String, num : Int, line : Int)".hover
)


@Test def `named-tuples4`: Unit =
check(
"""|def hello = (path = ".", num = 5)
|
|def test =
| hel@@lo ++ (line = 1)
|
|@main def bla =
| val x: (path: String, num: Int, line: Int) = test
|""".stripMargin,
"def hello: (path : String, num : Int)".hover
)

@Test def `named-tuples5`: Unit =
check(
"""|def hello = (path = ".", num = 5)
|
|def test(x: (path: String, num: Int)) =
| x ++ (line = 1)
|
|@main def bla =
| val x: (path: String, num: Int, line: Int) = t@@est(hello)
|""".stripMargin,
"def test(x: (path : String, num : Int)): (path : String, num : Int, line : Int)".hover
)

@Test def `value-of`: Unit =
check(
"""|enum Foo(val key: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1055,4 +1055,24 @@ class InlayHintsSuite extends BaseInlayHintsSuite {
|}
|""".stripMargin
)

@Test def `named-tuples` =
check(
"""|def hello = (path = ".", num = 5)
|
|def test =
| hello ++ (line = 1)
|
|@main def bla =
| val x: (path: String, num: Int, line: Int) = test
|""".stripMargin,
"""|def hello/*: (path : String<<java/lang/String#>>, num : Int<<scala/Int#>>)*/ = (path = ".", num = 5)/*[(String<<java/lang/String#>>, Int<<scala/Int#>>)]*/
|
|def test/*: (path : String<<java/lang/String#>>, num : Int<<scala/Int#>>, line : Int<<scala/Int#>>)*/ =
| hello ++/*[Tuple1<<scala/Tuple1#>>["line"], Tuple1<<scala/Tuple1#>>[Int<<scala/Int#>>]]*/ (line = 1)/*(using refl<<scala/`<:<`.refl().>>)*//*[Tuple1<<scala/Tuple1#>>[Int<<scala/Int#>>]]*/
|
|@main def bla/*: Unit<<scala/Unit#>>*/ =
| val x: (path: String, num: Int, line: Int) = test
|""".stripMargin
)
}
Loading