Skip to content

Runtime semantics difference under better-fors #25077

@WojciechMazur

Description

@WojciechMazur

Compiler version

3.8

Workaround: Run with -source:3.7

Minimized code

def f(x: Int): (Int, Int) = (1, 0)

val result: Iterable[(Int, Int)] =
  for
    (k, v)   <- Map(1 -> 1, 2 -> 1, 3 -> 1)
    x        = k + v
    (a, b)   = f(x)
  yield (a, b)

@main def Test() = 
  assert(result.size == 3, "Expected 3 elements, got ${result.size}")

Output

> scala-cli run -S 3.7 bug.scala
Compiling project (Scala 3.7.4, JVM (21))
Compiled project (Scala 3.7.4, JVM (21))
> scala-cli run -S 3.nightly bug.scala
Compiling project (Scala 3.8.3-RC1-bin-20260123-506b8a0-NIGHTLY, JVM (21))
Compiled project (Scala 3.8.3-RC1-bin-20260123-506b8a0-NIGHTLY, JVM (21))
Exception in thread "main" java.lang.AssertionError: assertion failed: Expected 3 elements, got 1
        at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:10)
        at bug$package$.Test(bug.scala:11)
        at Test.main(bug.scala:10)

Expectation

Should produce the same results as version without better-fors (using Scala 3.7, or 3.8 with -source:3.7)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions