Skip to content

AnyRefMap iterates its way to ((null, null)) #8154

Closed
@scabug

Description

@scabug

You can't cache the result of hasNext on an iterator showing a live view of a mutable collection.

def f() = {
  val xs = scala.collection.mutable.AnyRefMap[String, Int]("a" -> 1)
  val it = xs.iterator
  it.hasNext
  xs.clear()

  if (it.hasNext) Some(it.next)
  else None
}
// scala> f
// res0: Option[(String, Int)] = Some((null,null))

The funny thing is that the AnyRefMap iterator makes copies of _keys and _values in what might be an attempt to avoid this, but then it uses the originals instead of the copies (this is something -Xlint would tell you if you used it) and it wouldn't avoid this even if iterator referenced kz and vz instead of _keys and _values. Maybe something else is in mind there.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions