@safe, @trusted, @system #26
Replies: 4 comments 11 replies
-
Jup. This is exactly what I say since long. We need only two states for functions + trusted/unsafe/whatever blocks within a safe function. But I don't think we need any new keywords like @unsafe. We can stay with @System. @safe can go, as this is the default, and @trusted can only be used on blocks. Very small change. |
Beta Was this translation helpful? Give feedback.
-
The problem that the 3 safe/trusted/system attributes solve is to separate the following:
If you only had
@safe f() {
int* p = (() @trusted => cast(int*)1000)();
// programming error even if `p` is never used
...
} The unsafe value of The problem with that is that then other unsafe stuff may happen anywhere in // OK, safety verified by a human, safe interface
@trusted f() {
int* p = @system(cast(int*)1000)); // OK
int* q = cast(int*)1000; // Error, need to mark as unsafe
...
} |
Beta Was this translation helpful? Give feedback.
-
Isn't this exactly the same as // OK, safety verified by a human, safe interface
@safe f() {
@unsafe
{
int* p = cast(int*)1000); // OK
}
int* q = cast(int*)1000; // Error, need to mark as unsafe
...
} |
Beta Was this translation helpful? Give feedback.
-
It's funny how this editor changes @System always to have an uppercase S (which the compiler doesn't like) :-) |
Beta Was this translation helpful? Give feedback.
-
This needs a discussion in a new thread I think. This is regarding the future of the state @trusted and also unsafe blocks. In my opinion we only need safe and unsafe designations and those two states can cover everything together with unsafe blocks. @safe can be declared on functions. @unsafe can be declared on blocks @safe and removing @trusted also simplifies the compiler and design work what is supposed to go in there.
There was a lot of debate about if extern(C) was supposed to be @safe or @System. For the sake of the discussion extern(C) is regarded as unsafe unless declared as @safe in these examples.
Example:
The point of the @trusted state was that it would serve as "verified trampoline and interface code". Nothing wrong with that but that extra @trusted state is unnecessary.
The code in the trampoline is usually short enough that it doesn't matter if you use @System or @trusted in terms of safety.
Sometimes you just want to call a few C functions and you don't really care about making trampoline functions or write @safe at extern(C) (let's say it is a library and you don't want to change that code) as they are just used once. It is up to the programmer how they want it. Often when you call FFI functions, there is an unsafe preparations before the call. unsafe blocks enables you to do this inline if you wish to do so.
Functions with @System attribute can be created just as before and can only called from unsafe code.
As you noticed by now, we can do everything without the @trusted attribute and it is completely unnecessary.
If we want to keep @trusted for compatibility reasons with old D code we can use lowering.
becomes using lowering
Beta Was this translation helpful? Give feedback.
All reactions