From 186b6d89df66943cec6674512c480078a5f8e186 Mon Sep 17 00:00:00 2001 From: Jan Chyb <48855024+jchyb@users.noreply.github.com> Date: Tue, 12 Aug 2025 19:46:40 +0200 Subject: [PATCH] Fix issue with pc breaking in requiredMethod on newly overloaded valueOf (#23708) We recently started having issues when running completions in metals, with the presentation compiler crashing: ```scala dotty.tools.dotc.core.TypeError$.apply(TypeErrors.scala:54) dotty.tools.dotc.core.Denotations$MultiDenotation.suchThat(Denotations.scala:1280) dotty.tools.dotc.core.Denotations$Denotation.requiredSymbol(Denotations.scala:305) dotty.tools.dotc.core.Denotations$Denotation.requiredMethod(Denotations.scala:321) dotty.tools.pc.completions.Completions.isUninterestingSymbol$lzyINIT1(Completions.scala:744) dotty.tools.pc.completions.Completions.isUninterestingSymbol(Completions.scala:725) dotty.tools.pc.completions.Completions.includeSymbol(Completions.scala:90) dotty.tools.pc.completions.Completions.visit$3(Completions.scala:697) dotty.tools.pc.completions.Completions.filterInteresting$$anonfun$1(Completions.scala:715) scala.collection.immutable.List.foreach(List.scala:334) dotty.tools.pc.completions.Completions.filterInteresting(Completions.scala:715) dotty.tools.pc.completions.Completions.enrichedCompilerCompletions(Completions.scala:118) dotty.tools.pc.completions.Completions.completions(Completions.scala:136) dotty.tools.pc.completions.CompletionProvider.completions(CompletionProvider.scala:139) dotty.tools.pc.ScalaPresentationCompiler.complete$$anonfun$1(ScalaPresentationCompiler.scala:194) dotty.tools.dotc.core.TypeError$$anon$1: Failure to disambiguate overloaded reference with method valueOf in object Predef: [T]: T and method valueOf in object Predef: [T](implicit vt: ValueOf[T]): T ``` This happened after the recent changes to the stdlib, with the, I believe, newly added valueOf overload, and the previously used `requiredMethod` by design not handling these cases. I also noticed that in the same piece of code we had a bit of an empty Symbol factory situation going on, with MultiDenotation being changed into a Symbol (always resulting in an empty symbol), and only later flattened with the `alternatives`, so I changed that too. I can't really test this properly, as the pc tests seem to use an older stdlib, but at least the `wait` methods do resolve properly after these changes, so I have no reason to think the valueOf methods would be any different. [Cherry-picked 1b5e93b9128a4eec82c22fe350115e81add751ce] --- .../main/dotty/tools/pc/completions/Completions.scala | 11 +++++++---- .../tools/pc/tests/completion/CompletionSuite.scala | 3 --- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala index e7902ef8aa44..b396dd780cc0 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala @@ -734,15 +734,18 @@ class Completions( defn.Object_notifyAll, defn.Object_notify, defn.Predef_undefined, - defn.ObjectClass.info.member(nme.wait_).symbol, // NOTE(olafur) IntelliJ does not complete the root package and without this filter // then `_root_` would appear as a completion result in the code `foobar(_)` defn.RootPackage, // NOTE(gabro) valueOf was added as a Predef member in 2.13. We filter it out since is a niche // use case and it would appear upon typing 'val' - defn.ValueOfClass.info.member(nme.valueOf).symbol, - defn.ScalaPredefModule.requiredMethod(nme.valueOf) - ).flatMap(_.alternatives.map(_.symbol)).toSet + defn.ValueOfClass + ) ++ ( + Set( + defn.ObjectClass.info.member(nme.wait_), + defn.ScalaPredefModule.info.member(nme.valueOf) + ).flatMap(_.alternatives.map(_.symbol)).toSet + ) private def isNotLocalForwardReference(sym: Symbol)(using Context): Boolean = !sym.isLocalToBlock || 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 05037cffca94..277a579ba4ce 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala @@ -129,9 +129,6 @@ class CompletionSuite extends BaseCompletionSuite: |isInstanceOf[X0]: Boolean |synchronized[X0](x$0: X0): X0 |toString(): String - |wait(): Unit - |wait(x$0: Long): Unit - |wait(x$0: Long, x$1: Int): Unit |""".stripMargin )