Jump to page: 1 215  
Page
Thread overview
Simplification of @trusted
Jun 16, 2021
RazvanN
Jun 16, 2021
jmh530
Jun 16, 2021
RazvanN
Jun 16, 2021
jmh530
Jun 16, 2021
jmh530
Jun 18, 2021
Nick Treleaven
Jun 16, 2021
kinke
Jun 16, 2021
H. S. Teoh
Jun 16, 2021
RazvanN
Jun 16, 2021
Alexandru Ermicioi
Jun 16, 2021
IGotD-
Jun 16, 2021
H. S. Teoh
Jun 17, 2021
Alexandru Ermicioi
Jun 17, 2021
ag0aep6g
Jun 17, 2021
Paulo Pinto
Jun 17, 2021
Paulo Pinto
Jun 17, 2021
ag0aep6g
Jun 17, 2021
Mathias LANG
Jun 17, 2021
ag0aep6g
Jun 17, 2021
rikki cattermole
Jun 17, 2021
Dukc
Jun 17, 2021
Dukc
Jun 17, 2021
Dukc
Jun 17, 2021
Dukc
Jun 18, 2021
Elronnd
Jun 19, 2021
Alexandru Ermicioi
Jun 16, 2021
H. S. Teoh
Jul 11, 2021
Per Nordlöw
Jul 13, 2021
Bruce Carneal
Jun 20, 2021
Per Nordlöw
Jun 16, 2021
Sönke Ludwig
Jun 16, 2021
jmh530
Jun 16, 2021
Walter Bright
Jun 16, 2021
Timon Gehr
Jun 17, 2021
Walter Bright
Jun 18, 2021
Timon Gehr
Jun 18, 2021
Walter Bright
Jun 16, 2021
GrimMaple
Jun 16, 2021
jmh530
Jun 16, 2021
Bruce Carneal
Jun 16, 2021
max haughton
Jun 16, 2021
Bruce Carneal
Jun 17, 2021
H. S. Teoh
Jun 17, 2021
Walter Bright
Jun 17, 2021
H. S. Teoh
Jun 18, 2021
Walter Bright
Jun 17, 2021
Walter Bright
Jun 23, 2021
jmh530
Jun 23, 2021
Timon Gehr
Jun 17, 2021
Sönke Ludwig
Jun 18, 2021
Walter Bright
Jun 19, 2021
Sönke Ludwig
Jun 20, 2021
Max Samukha
Jun 20, 2021
Lorenso
Jun 16, 2021
Ogi
Jun 16, 2021
GrimMaple
Jun 16, 2021
RazvanN
Jun 16, 2021
GrimMaple
Jun 16, 2021
RazvanN
Jun 16, 2021
GrimMaple
Jun 16, 2021
IGotD-
Jun 16, 2021
Paul Backus
Jun 16, 2021
Paul Backus
Jun 16, 2021
Max Samukha
Jun 16, 2021
Timon Gehr
Jun 16, 2021
Bruce Carneal
Jun 16, 2021
Paul Backus
Jun 16, 2021
jmh530
Jun 16, 2021
Timon Gehr
Jun 16, 2021
Bruce Carneal
Jun 16, 2021
Bruce Carneal
Jun 16, 2021
Bruce Carneal
Jun 18, 2021
Bruce Carneal
Jun 17, 2021
Paul Backus
Jun 17, 2021
Bruce Carneal
Jun 17, 2021
Bruce Carneal
Jun 17, 2021
jmh530
Jun 17, 2021
Paul Backus
Jun 17, 2021
Paolo Invernizzi
Jun 17, 2021
Paul Backus
Jun 17, 2021
H. S. Teoh
Jun 18, 2021
Bruce Carneal
Jun 17, 2021
ag0aep6g
Jun 17, 2021
Paul Backus
Jun 17, 2021
Paul Backus
Jun 17, 2021
Paul Backus
Jun 17, 2021
ag0aep6g
Jun 17, 2021
Paul Backus
Jun 17, 2021
ag0aep6g
Jun 17, 2021
ag0aep6g
Jun 17, 2021
ag0aep6g
Jun 17, 2021
Paolo Invernizzi
Jun 16, 2021
ikod
Jun 16, 2021
Dukc
Programming Languages on Crack
Jun 17, 2021
Walter Bright
Jun 17, 2021
Max Samukha
Jun 17, 2021
Walter Bright
Jun 17, 2021
Max Samukha
Jun 17, 2021
Dennis
Jun 17, 2021
Walter Bright
Jun 17, 2021
Ogi
Jun 17, 2021
IGotD-
Jun 17, 2021
Walter Bright
Jun 18, 2021
Elronnd
Jun 19, 2021
Walter Bright
Jun 17, 2021
Alexandru Ermicioi
Jun 17, 2021
Walter Bright
Jun 17, 2021
ag0aep6g
Jun 17, 2021
Walter Bright
Jun 17, 2021
vit
Jun 25, 2021
jfondren
Jul 10, 2021
Per Nordlöw
June 16, 2021

Currently, @trusted applies only to functions. This is most of the times a pain when you want trusted code blocks inside functions. Why not simplify it a bit by using trusted scope blocks? E.g. this:

void foo() @safe
{
    () @trusted { ... }();
}

becomes this:

void foo() @safe
{
    @trusted
    {
       ....
    }
}

To make things easier, @trusted does not insert a scope (similar to static if).

Of course, the feature would be additive (you can have both trusted functions and code blocks).

That would also provide an elegant workaround if void initialization is rejected in @safe code [1][2]. For example:

void foo() @safe
{
    @trusted
    {
        int[100] a = void;
    }
    ...
}

What do you think?

Cheers,
RazvanN

[1] https://issues.dlang.org/show_bug.cgi?id=17566
[2] https://github.com/dlang/dlang.org/pull/2260

June 16, 2021

On Wednesday, 16 June 2021 at 11:38:54 UTC, RazvanN wrote:

>

[snip]

void foo() @safe
{
    @trusted
    {
        int[100] a = void;
    }
    ...
}

[snip]

The documentation related to these @trusted blocks should emphasize that the block should be large enough to encompass enough information to verify the safety of what would normally require the function to be labelled @system. For instance, in your above example, just void initializing is @system, but if you fill a later outside the @trusted block later, then it is harder to verify that it is actually @safe.

June 16, 2021

On Wednesday, 16 June 2021 at 12:06:56 UTC, jmh530 wrote:

>

On Wednesday, 16 June 2021 at 11:38:54 UTC, RazvanN wrote:

>

[snip]

void foo() @safe
{
    @trusted
    {
        int[100] a = void;
    }
    ...
}

[snip]

The documentation related to these @trusted blocks should emphasize that the block should be large enough to encompass enough information to verify the safety of what would normally require the function to be labelled @system. For instance, in your above example, just void initializing is @system, but if you fill a later outside the @trusted block later, then it is harder to verify that it is actually @safe.

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.

June 16, 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.

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

void bar() @safe
{
    int x;
    @trusted
    {
        int* p = void;
    }
    ...
    p = &x;
}
June 16, 2021

On Wednesday, 16 June 2021 at 11:38:54 UTC, RazvanN wrote:

>

What do you think?

Absolutely love it, I've wanted this for ages, similar to C# unsafe {} blocks. I absolutely hate the trusted lambdas 'idiom'.

June 16, 2021
Am 16.06.2021 um 13:38 schrieb RazvanN:
> Currently, @trusted applies only to functions. This is most of the times a pain when you want trusted code blocks inside functions. Why not simplify it a bit by using trusted scope blocks?

Yes, please! There are 800 of these in vibe.d alone. There has also been an issue where the delegate workaround was erroneously flagged as a heap delegate, causing considerable GC memory load.

`@trusted` *should* probably not even be available for functions (of course it is not a desirable breaking change to disallow that now, though).
June 16, 2021
On 6/16/21 8:06 AM, jmh530 wrote:
> On Wednesday, 16 June 2021 at 11:38:54 UTC, RazvanN wrote:
>> [snip]
>>
>> ```d
>> void foo() @safe
>> {
>>     @trusted
>>     {
>>         int[100] a = void;
>>     }
>>     ...
>> }
>> ```
>>
>> [snip]
> 
> The documentation related to these @trusted blocks should emphasize that the block should be large enough to encompass enough information to verify the safety of what would normally require the function to be labelled @system. For instance, in your above example, just void initializing is @system, but if you fill `a` later outside the @trusted block later, then it is harder to verify that it is actually @safe.

You mean like it does now?

```d
void foo() @safe
{
   int[100] a = () @trusted {int[100] a = void; return a; }();
}
```

(in LDC, this compiles equivalent to Razvan's code above, not sure about DMD)

For @trusted blocks or inner @trusted functions, it's really difficult to say what parts are trusted and what parts are safe. See my [dconf online 2020 talk](http://dconf.org/2020/online/index.html#steven).

Right now, safe has 2 meanings, one is that code within it is safe, one is that code marked @safe is mechanically checked by the compiler. Only the mechanical checking is guaranteed, the semantic meaning that the code actually is safe is easily thwarted by inner trusted code.

This is the impetus behind [DIP1035](https://github.com/dlang/DIPs/blob/master/DIPs/DIP1035.md).

But as long as we want code to do anything interesting, there is always going to be some trusted code, and the risks that come with it.

I would support @trusted blocks, as long as we can have @system variables (DIP1035) and variables declared inside a @trusted block were implicitly @system.

-Steve
June 16, 2021
On Wednesday, 16 June 2021 at 13:17:41 UTC, Steven Schveighoffer wrote:
> [snip]
>
> You mean like it does now?
> [snip]

See the code example I have above. My point isn't about @trusted per se, it's about best practices for using a @trusted code block.

In my opinion, your @trusted lambda example is a bad use of @trusted because you're not filling in the void initialized variable within @trusted code area. The person who is trying to manually verify that what is in the @trusted block is actually safe has to search for that outside the block.
June 16, 2021
On 6/16/21 9:09 AM, Sönke Ludwig wrote:
> Am 16.06.2021 um 13:38 schrieb RazvanN:
>> Currently, @trusted applies only to functions. This is most of the times a pain when you want trusted code blocks inside functions. Why not simplify it a bit by using trusted scope blocks?
> 
> Yes, please! There are 800 of these in vibe.d alone. There has also been an issue where the delegate workaround was erroneously flagged as a heap delegate, causing considerable GC memory load.
> 
> `@trusted` *should* probably not even be available for functions (of course it is not a desirable breaking change to disallow that now, though).

If I were to design it today:

- a @safe function could not call @trusted functions that gained implicit access to local data (i.e. inner functions, or non-static member functions from the same type).
- a @trusted function would be mechanically checked just like @safe, but could have @system blocks in them (where you could call @system functions, or do @system-like behaviors).

This at least puts the emphasis on where manual verification is required, but still has the compiler checking things I want it to check. Most times, I never want to write a fully marked @trusted function, because it's so easy to trust things you didn't intend to (like destructors).

-Steve
June 16, 2021
On 6/16/21 9:22 AM, jmh530 wrote:
> On Wednesday, 16 June 2021 at 13:17:41 UTC, Steven Schveighoffer wrote:
>> [snip]
>>
>> You mean like it does now?
>> [snip]
> 
> See the code example I have above. My point isn't about @trusted per se, it's about best practices for using a @trusted code block.
> 
> In my opinion, your @trusted lambda example is a bad use of @trusted because you're not filling in the void initialized variable within @trusted code area. The person who is trying to manually verify that what is in the @trusted block is actually safe has to search for that outside the block.

Of course it's bad. But this is how code is written today, because @trusted is too blunt an instrument (I might want just void initialization of that one variable, but still want other @safety checks throughout the rest of the function).

My point (in a slightly snarky reply, apologies) is that we don't need a new @trusted block feature to have the documentation identify such pitfalls.

-Steve
« First   ‹ Prev
1 2 3 4 5 6 7 8 9 10 11