Skip to content

Add safe-init test to prevent adding cold elements to hot arrays #14895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/init/neg/cold-insert-hot-array.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object A:
def foo[T](x: T, array: Array[T]): Unit = array(0) = x

class B {
var a = new Array[B](2)
A.foo(this, a) // error
println(a(0).i)
val i = 99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here i is not cold. You can use e.g. this instead. It's better to only leave the method foo in the global object A, and move other code to a class --- this way, we can be sure that the method call foo(...) will be A.foo(...) instead of A.this.foo(...).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not clear enough in the above. I think something like the following will serve the purpose:

class B {
  var a = new Array[B](2)
  A.foo(this, a)
  println(a(0).i)
  val i = 99
}

}