----- void main() @safe { Object[] objs = [ new Object() ]; void[] arr1 = objs; void[] arr2 = [ 123, 345, 567 ]; arr1[] = arr2[]; // overwrites pointers with arbitrary ints } ----- It should be illegal to copy the contents of one void[] to another void[], since void[] by definition is a type-erased array and can represent any arbitrary type, including types with indirections. Since type information has been erased, there is no way to verify that the destination array has no indirections, so to guarantee @safety, such an operation must not be allowed in @safe code.
Shouldn't we just disallow all writes to a void[] in safe code?
It's not just writing to void[] that's the problem. Consider: ---- int[] intArr = [ 1,2,3,4,5 ]; void[] voidArr = intArr; // OK, every array converts to void[] int*[] ptrArr; ptrArr.length = 5; ptrArr[] = voidArr[]; // reinterpret intArr as pointers ptrArr[0] = 1; // oops ---- Basically, *anything* that leads to reinterpretation of something as pointer values cannot be allowed in @safe.
(In reply to hsteoh from comment #2) > It's not just writing to void[] that's the problem. Consider: > > ---- > int[] intArr = [ 1,2,3,4,5 ]; > void[] voidArr = intArr; // OK, every array converts to void[] > int*[] ptrArr; > ptrArr.length = 5; > ptrArr[] = voidArr[]; // reinterpret intArr as pointers Wait, does this really work (I didn't think it did)? If so, isn't it still implicitly doing this: (cast(void[])ptrArr)[] = voidArr[]; Which is still writing void data.
Oh, you're right, it doesn't compile because implicit conversion from void[] to int*[] is not allowed. OK, nevermind what I said, then. :-D
https://github.com/dlang/dmd/pull/5877
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/378e6e3ff01e8e1afd5b5bb97d259ae68918ef9e fix Issue 15704 - @safe code should not allow copying to/from void[] https://github.com/dlang/dmd/commit/8ed696695c913234d7bed276215c9dcae8a9cc66 Merge pull request #5877 from WalterBright/fix15704 fix Issue 15704 - @safe code should not allow copying to/from void[]
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/378e6e3ff01e8e1afd5b5bb97d259ae68918ef9e fix Issue 15704 - @safe code should not allow copying to/from void[] https://github.com/dlang/dmd/commit/8ed696695c913234d7bed276215c9dcae8a9cc66 Merge pull request #5877 from WalterBright/fix15704