-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add safe-init test to prevent adding cold elements to hot arrays #14895
Conversation
Test currently fails due to case not being handled correctly |
@Xavientois FYI opening PR branches directly in the lampepfl/dotty repository causes CI to run twice for every push: https://github.com/lampepfl/dotty/actions/runs/2116548617 which is one of the reasons we don't normally do that. |
The usual thing to do is to push a branch to https://github.com/dotty-staging/dotty (or a fork in your own account). |
def foo[T](x: T, array: Array[T]): Unit = array(0) = x | ||
var a = Array.apply(1, 2, 3) | ||
foo(i, a) | ||
val i = 99 |
There was a problem hiding this comment.
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(...)
.
There was a problem hiding this comment.
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
}
Sorry about that! I'll be sure to open PRs from my fork in the future |
9eaa220
to
2ccbe8c
Compare
val i = 99 | ||
} | ||
|
||
val b = new B() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this line.
8a1adb2
to
28ca899
Compare
|
||
class B { | ||
var a = new Array[B](2) | ||
A.foo(this, a) //error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a space before error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
For the record, I think that with all the interesting dimensions of freedom, the precise formatting of |
This adds a negative test to ensure that the initialization checker does nt allow cold values to be inserted into hot arrays through a method call.
132b278
to
7c26c7e
Compare
This adds a negative test to ensure that the initialization checker doesn't allow cold values to be inserted into hot arrays through a method call.