Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix named tuple crash + actually filter used names in named tuples
Browse files Browse the repository at this point in the history
rochala committed Dec 20, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
lilnasy Arsh
1 parent 61cc4ae commit cc0c444
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -93,7 +93,8 @@ object InterCompletionType:
case UnApply(fun, _, pats) :: _ =>
val ind = pats.indexWhere(_.span.contains(span))
if ind < 0 then None
else Some(UnapplyArgs(fun.tpe.finalResultType, fun, pats, NoSourcePosition).argTypes(ind))
else
UnapplyArgs(fun.tpe.finalResultType, fun, pats, NoSourcePosition).argTypes.get(ind)
// f(@@)
case (app: Apply) :: rest =>
val param =
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dotty.tools.pc.completions

import dotty.tools.dotc.ast.tpd.*
import dotty.tools.dotc.ast.untpd
import dotty.tools.dotc.core.Flags
import dotty.tools.dotc.core.Names.Name
import dotty.tools.dotc.core.StdNames.*
@@ -10,6 +11,8 @@ import dotty.tools.dotc.core.Symbols.defn
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Types.*
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.dotc.ast.NavigateAST

import scala.meta.internal.pc.CompletionFuzzy

object NamedPatternCompletions:
@@ -38,8 +41,13 @@ object NamedPatternCompletions:
rest.collectFirst: // We can't complete names without knowing the type of selector
case Match(selector, _) => selector
.flatMap: selector =>
// Named patterns are desugared to normal binds without original arg name info
val patterns = NavigateAST.untypedPath(unapply).collectFirst:
case untpd.Tuple(elems) => elems
.getOrElse(Nil)

if selector.tpe.widenDealias.isNamedTupleType then
Some(selector.tpe.widenDealias.namedTupleElementTypes.toMap, unapply.patterns)
Some(selector.tpe.widenDealias.namedTupleElementTypes.toMap, patterns)
else None

// case User(nam@@
@@ -89,10 +97,10 @@ object NamedPatternCompletions:
def contribute(
completionPos: CompletionPos,
namesToArgs: Map[Name, Type],
patterns: List[Tree]
patterns: List[untpd.Tree]
)(using Context): List[CompletionValue] =
val usedNames = patterns.collect:
case NamedArg(name, _) => name.asTermName
case untpd.NamedArg(name, _) => name.asTermName

val remainingParams = namesToArgs -- usedNames
remainingParams
Original file line number Diff line number Diff line change
@@ -50,8 +50,7 @@ class CompletionNamedPatternSuite extends BaseCompletionSuite:
|def idsWithName(name: String) = user match
| case (name = supername, na@@
|""".stripMargin,
"""name = : String
|surname = : String""".stripMargin
"""surname = : String""".stripMargin
)

@Test def `named-tuples-3` =
@@ -65,8 +64,7 @@ class CompletionNamedPatternSuite extends BaseCompletionSuite:
|def idsWithName(name: String) = user match
| case (na@@, name = name) =>
|""".stripMargin,
"""name = : String
|surname = : String""".stripMargin
"""surname = : String""".stripMargin
)

@Test def `named-tuples-synthetic` =

0 comments on commit cc0c444

Please sign in to comment.