Skip to content

Default TreeMap is not an identity function #25004

@Jentsch

Description

@Jentsch

Compiler version

Warning found in: 3.7.4, 3.8.0

Minimized example

While working with utest I got false warnings about unused pattern variables where all were unused. A was able to minimize the error to this self contained example:

Macro:

import scala.quoted.*

object TestBuilder:
  transparent inline def test(inline expr: Unit): Any =
    ${ TestBuilder.processTests('expr) }

  def processTests(using Quotes)(body: Expr[Unit]): Expr[Any] =
    import quotes.reflect.*
    val treeMap = new TreeMap {}
    val mapped =
      treeMap.transformTerm(body.asTerm)(Symbol.spliceOwner)
    mapped.asExpr

Actual file:

object Tests:
  TestBuilder.test:
    val start @ _: String = "" // Converting this to a match expression resolves the error
    // Alternative: val Seq(start) = Seq("")
    print(start)

Warning message

[warn] -- [E198] Unused Symbol Warning: /.../Test.scala:2:2
[warn] 2 |  Builder.test:
[warn]   |  ^^^^^
[warn]   |  unused pattern variable
[warn] one warning found

I think this is an error in the compiler, but maybe I got the idea of TreeMap wrong and it's actually an bug in utest.

Observations:

  • the number of ^ in the warning is equal to the length of the pattern variable start (5). If I rename start to end the message shows only three ^.
  • If start is actually unused it's reported correctly at the correct position.
  • mapped.showand body.asTerm.show produce the same output. I couldn't figure out what the actual difference between them is.
  • Returning body.asTerm.changeOwner(Symbol.spliceOwner).asExpr produces no warning, TreeMap seems to be required.

Why this Error/Warning was not helpful

I think this is a false positive.

Suggested improvement

No warning should be shown here.

Metadata

Metadata

Assignees

Labels

area:desugarDesugaring happens after parsing but before typing, see desugar.scalaitype:bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions