Bug 11510 – Relax restriction for overlapped pointer field access in safe code/during CTFE

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-13T07:10:00Z
Last change time
2014-01-14T07:37:12Z
Keywords
CTFE, pull, spec
Assigned to
nobody
Creator
k.hara.pg
Blocks
11504

Comments

Comment #0 by k.hara.pg — 2013-11-13T07:10:31Z
Currently in @safe code, declaring struct variable which contains any overlapped pointer(==reference) fields is entirely disallowed. struct S { union { size_t x; int* y; // pointer field } int[] arr; } // This is necessary to avoid related compiler bug S _dummy = S(); void test() @safe { S s; // Error: variable s unions containing pointers are not allowed // in @safe functions } However I think this is too limited behavior. Even if S.y is an overlapped pointer field, 1. Declaring a variable typed S 2. Both reading and writing unoverlapped field S.arr 3. Both reading and writing overlapped field S.x 4. Writing overlapped pointer field S.y should be allowed. Especially, by combining #3 and #4, you can reinterpret int* to size_t under the @safe code. But it is nothing wrong, as same as declaring size_t variable with void initializer. void test() @safe { size_t num = void; } Even the value of 'num' is garbage, using it won't cause any memory corruption in @safe. So currently it is properly accepted by compiler. --- And the semantics should also work during CTFE. For CTFE, one following restriction is necessary. - Any field value reinterpretation by using two overlapped fields is disallowed. If it's detected in CTFE, should raise compile-time error. Therefore, following code should work as expected. bool test() { S s; // declaration is OK s.y = [1,2,3].ptr; // writing overlapped pointer field is OK assert(s.y[0..3] == [1,2,3]); // reading valid field is OK s.x = 10; assert(s.x == 10); // There's no reinterpretation between S.x and S.y return true; } static assert(test()); // run CTFE
Comment #1 by k.hara.pg — 2013-11-13T07:11:59Z
(In reply to comment #0) > // This is necessary to avoid related compiler bug > S _dummy = S(); The "related bug" is bug 11427.
Comment #2 by k.hara.pg — 2013-11-13T07:35:16Z
Comment #3 by github-bugzilla — 2013-12-16T11:21:13Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/a2932981ee03abe13e9fdacf07ad29d162fc4b16 fix Issue 11510 - Relax restriction for overlapped pointer field access in safe code/during CTFE Check overlapped field default initializations immediately after the struct size is determined. https://github.com/D-Programming-Language/dmd/commit/475c5437525d759891be381961c076d2e1dc3e2b Merge pull request #2757 from 9rnsr/fix11510 Issue 11510 - Relax restriction for overlapped pointer field access in safe code/during CTFE
Comment #4 by k.hara.pg — 2014-01-14T07:37:12Z
*** Issue 10035 has been marked as a duplicate of this issue. ***