From cc0c4445af2ac8a667151ed98890fdb33b200cd8 Mon Sep 17 00:00:00 2001 From: rochala Date: Fri, 20 Dec 2024 15:06:04 +0100 Subject: [PATCH] Fix named tuple crash + actually filter used names in named tuples --- .../main/dotty/tools/pc/InferExpectedType.scala | 3 ++- .../pc/completions/NamedPatternCompletions.scala | 14 +++++++++++--- .../completion/CompletionNamedPatternSuite.scala | 6 ++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/InferExpectedType.scala b/presentation-compiler/src/main/dotty/tools/pc/InferExpectedType.scala index d2a004075196..41bd2b61e080 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/InferExpectedType.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/InferExpectedType.scala @@ -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 = diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/NamedPatternCompletions.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/NamedPatternCompletions.scala index bfa01cd128e1..27f48faaf7b9 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/NamedPatternCompletions.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/NamedPatternCompletions.scala @@ -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 diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionNamedPatternSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionNamedPatternSuite.scala index 4c77a1d85edf..5a0409fc8725 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionNamedPatternSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionNamedPatternSuite.scala @@ -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` =