Skip to content

Commit

Permalink
Merge pull request scala#10873 from cptwunderlich/quickfix-companion-…
Browse files Browse the repository at this point in the history
…as-function

Add quickfix for "synthetic case companion used as a function" error.
  • Loading branch information
lrytz authored Oct 10, 2024
2 parents 4e4f316 + 8bdf764 commit ce78754
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/compiler/scala/tools/nsc/typechecker/Typers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1295,8 +1295,11 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
adapt(tree.setType(repeatedToSeq(tree.tpe)), mode, pt, original = EmptyTree)
else if (tree.tpe <:< pt) {
val sym = tree.symbol
if (sym != null && !isPastTyper && currentRun.isScala3 && isFunctionType(pt) && sym.isModule && sym.isSynthetic && sym.companion.isCase)
context.warning(tree.pos, s"Synthetic case companion used as a function. In Scala 3 (or with -Xsource-features:case-companion-function), case companions no longer extend FunctionN. Use ${sym.name}.apply instead.", Scala3Migration)
if (sym != null && !isPastTyper && currentRun.isScala3 && isFunctionType(pt) && sym.isModule && sym.isSynthetic && sym.companion.isCase) {
val msg = s"Synthetic case companion used as a function. In Scala 3 (or with -Xsource-features:case-companion-function), case companions no longer extend FunctionN. Use ${sym.name}.apply instead."
val action = runReporting.codeAction("add `.apply`", tree.pos.focusEnd, ".apply", msg)
context.warning(tree.pos, msg, Scala3Migration, action)
}
tree
} else if (mode.inPatternMode && { inferModulePattern(tree, pt); isPopulated(tree.tpe, approximateAbstracts(pt)) })
tree
Expand Down
4 changes: 2 additions & 2 deletions test/files/neg/t3664.check
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
t3664.scala:10: error: Synthetic case companion used as a function. In Scala 3 (or with -Xsource-features:case-companion-function), case companions no longer extend FunctionN. Use C.apply instead.
t3664.scala:10: error: Synthetic case companion used as a function. In Scala 3 (or with -Xsource-features:case-companion-function), case companions no longer extend FunctionN. Use C.apply instead. [quickfixable]
Scala 3 migration messages are issued as errors under -Xsource:3. Use -Wconf or @nowarn to demote them to warnings or suppress.
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=scala3-migration, site=Test.f
def f(xs: List[Int]): List[C] = xs.map(C) // ident
^
t3664.scala:11: error: Synthetic case companion used as a function. In Scala 3 (or with -Xsource-features:case-companion-function), case companions no longer extend FunctionN. Use D.apply instead.
t3664.scala:11: error: Synthetic case companion used as a function. In Scala 3 (or with -Xsource-features:case-companion-function), case companions no longer extend FunctionN. Use D.apply instead. [quickfixable]
Scala 3 migration messages are issued as errors under -Xsource:3. Use -Wconf or @nowarn to demote them to warnings or suppress.
Applicable -Wconf / @nowarn filters for this fatal warning: msg=<part of the message>, cat=scala3-migration, site=Test.g
def g(xs: List[Int]): List[O.D] = xs.map(O.D) // select
Expand Down
16 changes: 16 additions & 0 deletions test/junit/scala/tools/nsc/QuickfixTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,20 @@ class QuickfixTest extends BytecodeTesting {
|"""
testQuickfix(a, b, "-Wunnamed-boolean-literal -quickfix:any")
}

@Test def `synthetic case companion used as a function error has a fix`: Unit = {
val a =
sm"""|case class C(i: Int)
|object Test {
| def test = List(1, 2, 3).map(C)
|}
|"""
val b =
sm"""|case class C(i: Int)
|object Test {
| def test = List(1, 2, 3).map(C.apply)
|}
|"""
testQuickfix(a, b, "-Xsource:3 -quickfix:any")
}
}

0 comments on commit ce78754

Please sign in to comment.