On Sun, 25 Aug 2024 at 19:26, Dom DiSc via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
On Saturday, 24 August 2024 at 17:10:39 UTC, Manu wrote:
> We need to have:
>
> @trusted {
>   some_code;
> }

We have:

() @trusted {
    some_code;
}();

Which works kind of the same, beside it's ugly as hell.
Until we have real trusted blocks, I use this (heavily).


..."kind of the same"


int normal_scope(int x, int y)
{
    int t;

    // normal scope
    {
        t = x + y;
    }

    return t;
}

        assume  CS:.text._D7example12normal_scopeFiiZi
                push    RBP
                mov     RBP,RSP
                sub     RSP,020h
                mov     -010h[RBP],EDI
                mov     -8[RBP],ESI
                mov     dword ptr -018h[RBP],0
                mov     EAX,-8[RBP]
                add     EAX,-010h[RBP]
                mov     -018h[RBP],EAX
                leave
                ret



int stupid_hack(int x, int y)
{
    int t;

    // stupid hack
    () @trusted {
        t = x + y;
    }();

    return t;
}

 
        assume  CS:.text._D7example11stupid_hackFiiZi
                push    RBP
                mov     RBP,RSP
                sub     RSP,020h
                mov     -010h[RBP],EDI
                mov     -8[RBP],ESI
                mov     dword ptr -018h[RBP],0
                mov     RDI,RBP
                call    qword ptr pure nothrow @nogc @trusted void example.stupid_hack(int, int).__lambda4()@GOTPCREL[RIP]
                mov     EAX,-018h[RBP]
                leave
                ret
        assume  CS:.text.pure nothrow @nogc @trusted void example.stupid_hack(int, int).__lambda4()
pure nothrow @nogc @trusted void example.stupid_hack(int, int).__lambda4():
                push    RBP
                mov     RBP,RSP
                sub     RSP,010h
                mov     -8[RBP],RDI
                mov     RAX,-8[RBP]
                mov     ECX,-8[RAX]
                add     ECX,-010h[RAX]
                mov     -018h[RAX],ECX
                leave
                ret
                add     [RAX],AL


I hope we can agree that this is definitely not 'kind of the same'...

And that's to say nothing about the damage it causes to the debug info, and the ability to breakpoint and step through the code in a sane way.
Completely unacceptable hack. I won't give this pattern the dignity of my fingertips approval under any circumstances. It should not be legitimised.