Skip to content

False positive dead code elimination on pure expressions #17317

Closed
@kubukoz

Description

@kubukoz

Compiler version

3.3.0-RC3, 3.2.2

Minimized code

Sample 1

//> using scala "3.2.2"

package object foo {

  object HelloGen {
    println("hello world")
  }

  val Hello = HelloGen
}

import foo.Hello

object Main {

  def main(args: Array[String]): Unit = Hello: Unit
}

Sample 2

Instead of a package object, use a top-level definition.

//> using scala "3.2.2"


object HelloGen {
  println("hello world")
}

val Hello = HelloGen


object Main {

  def main(args: Array[String]): Unit = Hello: Unit
}

Output

Compiling project (Scala 3.3.0-RC3, JVM)
[warn] ./main.scala:13:5
[warn] A pure expression does nothing in statement position; you may be omitting necessary parentheses
[warn]     Hello: Unit
[warn]     ^^^^^
Compiled project (Scala 3.3.0-RC3, JVM)

Expectation

Compiling project (Scala 3.3.0-RC3, JVM)
Compiled project (Scala 3.3.0-RC3, JVM)
hello world

Notes:

  1. Here's javap verbose for main:
public void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=0, locals=2, args_size=2
         0: return
      LineNumberTable:
        line 16: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       1     0  this   LMain$;
            0       1     1  args   [Ljava/lang/String;
    Signature: #27                          // ([Ljava/lang/String;)V
    MethodParameters:
      Name                           Flags
      args                           final

Notably the code is just a return, as the call is erased from the output.

  1. This doesn't happen in Scala 2.13 or if you fully qualify foo.Hello.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions