www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11510] New: Relax restriction for overlapped pointer field access in safe code/during CTFE

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11510

           Summary: Relax restriction for overlapped pointer field access
                    in safe code/during CTFE
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: CTFE, spec
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: k.hara.pg gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg gmail.com> 2013-11-13 07:10:31 PST ---
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

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 13 2013
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11510



--- Comment #1 from Kenji Hara <k.hara.pg gmail.com> 2013-11-13 07:11:59 PST ---
(In reply to comment #0)
 // This is necessary to avoid related compiler bug
 S _dummy = S();

The "related bug" is bug 11427. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 13 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11510


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #2 from Kenji Hara <k.hara.pg gmail.com> 2013-11-13 07:35:16 PST ---
https://github.com/D-Programming-Language/dmd/pull/2757

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 13 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11510



--- Comment #3 from github-bugzilla puremagic.com 2013-12-16 11:21:13 PST ---
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

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 16 2013
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11510


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com


--- Comment #4 from Kenji Hara <k.hara.pg gmail.com> 2014-01-14 07:37:12 PST ---
*** Issue 10035 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 14 2014