From 2ba368df4c6bd3fe237ab912fe1e57c802711eba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Rochala?= <48657087+rochala@users.noreply.github.com> Date: Wed, 17 Apr 2024 13:11:08 +0200 Subject: [PATCH] Interactive: handle context bounds in extension construct workaround (#20201) Fixes https://github.com/scala/scala3/issues/19971 --- .../tools/dotc/interactive/Completion.scala | 7 +++- .../pc/tests/completion/CompletionSuite.scala | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/interactive/Completion.scala b/compiler/src/dotty/tools/dotc/interactive/Completion.scala index 2ff8ad1c6535..f2b63cbec8d5 100644 --- a/compiler/src/dotty/tools/dotc/interactive/Completion.scala +++ b/compiler/src/dotty/tools/dotc/interactive/Completion.scala @@ -186,7 +186,12 @@ object Completion: )(using Context): List[tpd.Tree] = untpdPath.collectFirst: case untpd.ExtMethods(paramss, _) => - val enclosingParam = paramss.flatten.find(_.span.contains(pos.span)) + val enclosingParam = paramss.flatten + .find(_.span.contains(pos.span)) + .flatMap: + case untpd.TypeDef(_, bounds: untpd.ContextBounds) => bounds.cxBounds.find(_.span.contains(pos.span)) + case other => Some(other) + enclosingParam.map: param => ctx.typer.index(paramss.flatten) val typedEnclosingParam = ctx.typer.typed(param) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala index 10a57f705ceb..f4f659db1541 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala @@ -1838,3 +1838,42 @@ class CompletionSuite extends BaseCompletionSuite: |""".stripMargin, topLines = Some(3) ) + + @Test def `context-bound-in-extension-construct` = + check( + """ + |object x { + | extension [T: Orde@@] + |} + |""".stripMargin, + """Ordered[T] scala.math + |Ordering[T] scala.math + |""".stripMargin, + topLines = Some(2) + ) + + @Test def `context-bounds-in-extension-construct` = + check( + """ + |object x { + | extension [T: Ordering: Orde@@] + |} + |""".stripMargin, + """Ordered[T] scala.math + |Ordering[T] scala.math + |""".stripMargin, + topLines = Some(2) + ) + + @Test def `type-bound-in-extension-construct` = + check( + """ + |object x { + | extension [T <: Orde@@] + |} + |""".stripMargin, + """Ordered[T] scala.math + |Ordering[T] scala.math + |""".stripMargin, + topLines = Some(2) + )