Simplification of @trusted

jmh530 john.michael.hall at gmail.com
Wed Jun 16 12:53:21 UTC 2021


On Wednesday, 16 June 2021 at 12:15:51 UTC, RazvanN wrote:
> [snip]
>
> I'm not sure what you are referring to. Whenever `a` is used 
> outside the trusted block, the compiler will apply the normal 
> safety constraints. When `a` will be used, the trusted block 
> has already been analyzed and any information regarding to it 
> will be present.

Below makes clear what I was thinking. In both you void 
initialize a pointer. However, the `foo` assigns to the pointer 
within the @trusted block and `bar` assigns it outside the 
@trusted block. It is easier to verify for another person to 
verify the @trusted block is correct in `foo` than in `bar`. More 
of a best practice than anything else.

```d
void foo() @safe
{
     int x;
     @trusted
     {
         int* p = void;
         p = &x;
     }
     ...
}

void bar() @safe
{
     int x;
     @trusted
     {
         int* p = void;
     }
     ...
     p = &x;
}
```


More information about the Digitalmars-d mailing list