Skip to content

Commit d754d25

Browse files
authored
bugfix: Fix wrong type in InferredType code action (scala#25469)
This was noticed in scalafix, which uses the same code action. ## How much have your relied on LLM-based tools in this contribution? Extensively, I let LLM do it's job, then simplified the result. I also wrote the tests beforehand. ## How was the solution tested? Automatic tests
1 parent 478cafe commit d754d25

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import scala.meta as m
1212
import dotty.tools.dotc.ast.Trees.*
1313
import dotty.tools.dotc.ast.untpd
1414
import dotty.tools.dotc.core.Contexts.*
15+
import dotty.tools.dotc.core.Flags.*
1516
import dotty.tools.dotc.core.NameOps.*
1617
import dotty.tools.dotc.core.Names.*
1718
import dotty.tools.dotc.core.Symbols.*
@@ -88,6 +89,11 @@ final class InferredTypeProvider(
8889
sourceText.substring(0, nameEnd).nn +
8990
sourceText.substring(tptEnd + 1, sourceText.length())
9091

92+
def widenJavaEnumSingleton(tpe: Type)(using Context): Type = tpe match
93+
case tp: TermRef if tp.termSymbol.isAllOf(JavaEnum) =>
94+
tp.widen
95+
case _ => tpe
96+
9197
def optDealias(tpe: Type): Type =
9298
def isInScope(tpe: Type): Boolean =
9399
tpe match
@@ -99,8 +105,9 @@ final class InferredTypeProvider(
99105
case AppliedType(tycon, args) =>
100106
isInScope(tycon) && args.forall(isInScope)
101107
case _ => true
102-
if isInScope(tpe) then tpe
103-
else tpe.deepDealiasAndSimplify
108+
val widened = widenJavaEnumSingleton(tpe)
109+
if isInScope(widened) then widened
110+
else widened.deepDealiasAndSimplify
104111

105112
val printer = ShortenedTypePrinter(
106113
symbolSearch,

presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
4444
|}""".stripMargin
4545
)
4646

47+
@Test def `java-enum` =
48+
checkEdit(
49+
"""|object A{
50+
| final val <<javaEnum>> = java.util.Locale.Category.DISPLAY
51+
|}""".stripMargin,
52+
"""|import java.util.Locale.Category
53+
|object A{
54+
| final val javaEnum: Category = java.util.Locale.Category.DISPLAY
55+
|}""".stripMargin
56+
)
57+
4758
@Test def `wrong-val2` =
4859
checkEdit(
4960
"""|object A{

0 commit comments

Comments
 (0)