Skip to content

Commit

Permalink
Fix named tuple crash + actually filter used names in named tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
rochala committed Dec 20, 2024
1 parent 61cc4ae commit cc0c444
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
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.*
Expand All @@ -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:
Expand Down Expand Up @@ -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@@
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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` =
Expand All @@ -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` =
Expand Down

0 comments on commit cc0c444

Please sign in to comment.