bug icon indicating copy to clipboard operation
bug copied to clipboard

Compilation error (`None.get`) when adding `private val` to a case class field

Open guizmaii opened this issue 1 year ago • 5 comments

Scala version: 2.13.13

private[zio] final case class FiberRefStack[@specialized(SpecializeInt) A] private[FiberRefs] (
    private val headFiberId: FiberId.Runtime, // Doesn't compile with the `private val` but does compile without it
    headValue: A,
    headVersion: Int,
    tail: List[FiberRefStackEntry[?]],
    depth: Int
  )

If we remove the private val of the private val headFiberId the code compiles. It seems to me that this private val headFiberId is valid Scala code and should compile

Reproducer: https://github.com/guizmaii/zio/pull/1

Note that I don't manage to reproduce the error in scastie: https://scastie.scala-lang.org/wynbilFLRTWlyVL3fjT7WQ

guizmaii avatar Apr 20 '24 05:04 guizmaii

Unlike the PR code, the Scastie example is missing an @inline def accessor to the field that is marked (or not) as private val, so it is at the intersection of private val, specialize, and @inline def that is causing the error. This Scastie thus show the error https://scastie.scala-lang.org/jBdfI5ofRA2KxBPnCmTHAg

diesalbla avatar Apr 20 '24 12:04 diesalbla

Not sure what the scastie intends to convey, but the zio scala_bug branch compileJVM fails. (Edit: the second scastie is missing specialized(Int).)

I see the code is trying to sort case class params by correlating non-method param accessors (which have correct sorting) with case accessor methods (unsorted and name-mangled).

A quick debug println shows the mismatch

NOACC in class FiberRefStack for method headFiberId$access$0 among List((zio$FiberRefs$FiberRefStack$$headFiberId,0), (headVersion,2), (headValue,1), (depth,4), (tail,3)) from List(value headValue, value headVersion, value tail, value depth, method headFiberId$access$0) and name List(zio$FiberRefs$FiberRefStack$$headFiberId, headValue, headVersion, tail, depth) or [zio$FiberRefs$FiberRefStack$$headFiberId ,headValue ,headVersion ,tail ,depth ]

it's looking for headFiberId prefix, not suffix. If access$0 is the private val mangling, I don't know offhand where the other mangling is introduced. The example snippet doesn't fail standalone.

som-snytt avatar Apr 20 '24 16:04 som-snytt

So this is the reproucer, right?

object example {
  final case class Toto[@specialized(Int) A] (
    private val a: String,
    b: A,
   ) {
     @inline def theA: String = a
   }
  def toto[A](a: A): Toto[A] = Toto("", a)
}

lrytz avatar Apr 22 '24 07:04 lrytz

@lrytz Yes, I think that the minimal reproducer

guizmaii avatar Apr 22 '24 08:04 guizmaii

I should have said thanks for the reproductions. The inline is relevant. I took a look but have been occupied elsewhere.

som-snytt avatar Apr 22 '24 09:04 som-snytt