[Issue 17284] ref returning function template allows bypassing @safe on unions

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Mar 29 12:53:49 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=17284

ZombineDev <petar.p.kirov at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|opDispatch allows bypassing |ref returning function
                   |@safe on unions             |template allows bypassing
                   |                            |@safe on unions

--- Comment #2 from ZombineDev <petar.p.kirov at gmail.com> ---
I reduced it a bit further:

Case 1:

```
class C { }
union U {
    C c;
    int i;
}

ref C getC1(ref U u) { return u.c; }
ref C getC2(T)(ref T u) { return u.c; }

void main() @safe {
    U u;
    u.getC2() = new C; // compiles (!!!)
}
```

Case 2:

```
class C { }
union U {
    C c;
    int i;
}

ref C getC1(ref U u) { return u.c; }
ref C getC2(T)(ref T u) { return u.c; }

void main() @safe {
    U u;
    u.getC1() = new C; // (Line 12) Doesn't compile
}
```

main.d(12): Error: @safe function 'D main' cannot call @system function
'main.getC1'

Case 3:

```
class C { }
union U {
    C c;
    int i;
}

@safe:

ref C getC1(ref U u) { return u.c; }     // (Line 9)
ref C getC2(T)(ref T u) { return u.c; }

void main() @safe {
    U u;
    u.getC1() = new C;
}
```
main.d(9): Error: field U.c cannot access pointers in @safe code that overlap
other fields

Case 4:

```
class C { }
union U {
    C c;
    int i;
}

@safe:

ref C getC1(ref U u) { return u.c; }      // (Line 9)
ref C getC2(T)(ref T u) { return u.c; }   // (Line 10)

void main() @safe {
    U u;
    u.getC2() = new C;                    // (Line 14)
}
```

main.d(9): Error: field U.c cannot access pointers in @safe code that overlap
other fields                                                                   
                                          main.d(10): Error: field U.c cannot
access pointers in @safe code that overlap other fields                        
                                                                               
    main.d(14): Error: template instance main.getC2!(U) error instantiating

--


More information about the Digitalmars-d-bugs mailing list