Skip to content

Vacuous test in LongMap likely results in incorrect behavior #13166

@Ichoran

Description

@Ichoran

Reproduction steps

Standard library bug. Example in Scala 3 REPL:

scala> val lm = collection.mutable.LongMap.empty[String]
val lm: scala.collection.mutable.LongMap[String] = LongMap()

scala> val minA = lm.put(Long.MinValue, "eel")
val minA: Option[String] = None
                                                                                
scala> val minB = lm.put(Long.MinValue, "cod")
val minB: Option[String] = None

Problem

An apparently LLM-generated PR noticed that line 294 in collection.mutable.LongMap contains a vacuous test:

val ans = if ((extraKeys&2) == 1) Some(minValue.asInstanceOf[V]) else None

Because of this, when LongMap has a pre-existing Long.MinValue key, indicated by bit 0x2 set in extraKeys, it incorrectly finds nothing and returns None. The map remains correct; the only problem is that the stored value is lost rather than being returned by put as its contract requires.

The LLM correctly guessed that it was meant to be == 2, but did not test the fix or submit a test. I also do not have time right now to do this, but I understand the context and that there is genuinely incorrect behavior.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions