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
memory corruption for seq.add(seq) with gc:arc and d:useMalloc #14983
Comments
Maybe this is the same as here #14472, my nim version is from |
|
It also happens with
and
|
Retested. |
I can't confirm that it works with
and
Update: I now compiled the latest nim version from source and the result is the same as before. Compiling with |
Here is a similar code showing the problem more obviously: Compile with
proc add2*[T](x: var seq[T], y: openArray[T]) =
var y2 = @y # works without this line
let xl = x.len
setLen(x, xl + y.len)
for i in 0..high(y):
x[xl+i] = y[i]
var s = @[1,2,3]
add2(s, s)
echo s |
So, it is the parameter aliasing problem. First parameter is IMO, we are leaning towards that the compiler should not allow it, detecting it is the aliasing problem. In future it would not compile and users will have to write: var s = @[1,2,3]
add2(s, copy(s))
echo s |
I'm leaning to the compiler producing a copy instead when the aliasing can cause trouble. No need to break code. |
@Araq should I open a new issue for the generic problem like shown in #14983 (comment) ? |
The more general problem is covered in https://nim-lang.org/docs/manual_experimental.html#aliasing-restrictions-in-parameter-passing with an expected implementation in 2021. |
* fixes nim-lang#14983 * allow bootstrapping with 0.20 * added a test case for the new system.add with a sink parameter * make npeg green again
Example
nim c -d:release -d:useMalloc --gc:arc file.nim
Current Output
Expected Output
Possible Solution
s2
which is ashallowCopy
ofs
as second parameter foradd
Additional Information
The text was updated successfully, but these errors were encountered: