June 09, 2022
On 09.06.22 03:10, Ali Çehreli wrote:
> On 6/8/22 18:04, Timon Gehr wrote:
>  > On 09.06.22 02:54, Timon Gehr wrote:
>  >> On 09.06.22 02:44, Ali Çehreli wrote:
>  >>> The society trusts C libraries, so do we.
>  >>
>  >> free(cast(void*)0xDEADBEEF)
>  >>
>  >> Seems legit.
>  >
>  > I guess this does not actually make the point very well. Second try:
>  >
>  > ```d
>  > free(new int);
>  > ```
>  >
>  > Seems legit. The C library can do no wrong!
> 
> I still don't get it. :(
> ...

`@trusted` has a specific meaning, it does not mean we believe the implementer of `free` is a nice guy. It means the specification of `free` says it's safe to call with any valid pointer and we believe that it is true. This is not the case, hence it cannot be `@trusted`.

> ...
> 
> I mean, who wins by @system-by-default? Nobody. The code is not safer.
> ...

That's on Walter.
June 09, 2022
On Thursday, 9 June 2022 at 00:44:58 UTC, Ali Çehreli wrote:
> I was convinced (after having an email exchange with Walter) that unless we assumed extern(C) functions @safe, then nobody would bother marking their declarations as @trusted one-by-one. And whoever marked them as such, they would do it without actually auditing any source code.

It is actually even worse than this: no matter how diligent you are, it is impossible to audit the source code corresponding to an extern(C) function declaration, even in principle.

The reason for this is that when your code calls an extern(C) function, it is not calling a specific implementation. Rather, it is calling whatever implementation happens to get linked into the final binary when the code is compiled. If someone builds your code 10 years from now on a new system, it may end up calling a C implementation that was not even written yet when you wrote your extern(C) function declaration!

Strictly speaking, this means that you can *never* be absolutely, 100% mathematically sure that an extern(C) function is memory-safe. So if you are aiming for an absolute, 100% mathematically-ironclad guarantee of memory safety, your program cannot call any extern(C) functions, ever, period.

Unfortunately, this also means your program cannot make any system calls, so you will probably not get very far attempting to program this way.

The solution is to give up on aiming for an absolute, 100% mathematical guarantee of memory safety under all possible circumstances. Instead, what we aim for in practice is a *conditional* guarantee: "my program is memory safe, so long as assumptions A, B, and C about the external world hold true."

Some common assumptions we make are:

* The OS's system call interface behaves as documented.
* The C standard library API behaves as specified in the relevant C standard.
* Other C libraries I depend on behave as documented.

It is important to understand that relying on C libraries to behave according to their documentation is *not* the same thing as marking their functions as @trusted. Many C library functions say explicitly, in their documentation, that they *will* corrupt memory if called in certain ways. These functions must still be marked as @system, even if you are willing to "trust" the libraries to behave as-documented.
June 09, 2022
On 09.06.22 03:07, Ali Çehreli wrote:
> 
> If your point was about my proposal

It does not matter. There's nothing in your proposal that justifies `@trusted` being the default for extern(C) functions. In fact, the opposite is the case.
June 09, 2022

On Thursday, 9 June 2022 at 01:18:30 UTC, Steven Schveighoffer wrote:

>
  1. The programmer is not expecting this. They did not write scope, they wrote in, which according to the spec is "equivalent to const" (see https://dlang.org/spec/function.html#in-params). I'm convinced that we absolutely cannot turn on preview in by default until this is addressed. I can't even recommend using the preview switch, as this is too dangerous for memory safety.

According to the spec, in means const scope when you are using -preview=in.

June 08, 2022

On 6/8/22 8:44 PM, Ali Çehreli wrote:

> >

-- except on
extern(C) interfaces to C code, which by definition is un-@safe

I see it differently: extern(C) interfaces are @trusted but they can't be checked. (More below.)

I was convinced (after having an email exchange with Walter) that unless we assumed extern(C) functions @safe, then nobody would bother marking their declarations as @trusted one-by-one. And whoever marked them as such, they would do it without actually auditing any source code.

What have we gained by disapproving @safe-by-default? Nothing: C API would either not be called and be marked blindly as @trusted. I think this is more embarrassing than @safe-by-default C libraries.

So, D's presumed embarrassment of "C functions are assumed @safe" was against both practicality and the truth: The truth is, we indeed "trust" C functions because we use C libraries all the time without reading their source code. This is the definition of trust. And that's why I say we chose wrong names around this topic.

You are missing the point.

extern(C) void *malloc(size_t);

extern(C) void free(void *); // @safe?

void main() // @safe!
{
   auto ptr = malloc(int.sizeof);
   int *v = ((p) @trusted => cast(int*)p)(ptr);
   free(v);
   *v = 5; // @safe??!
}

See, you can trust malloc and free to have valid implementations (read, they are memory safe as long as you obey their rules). But they don't obey @safe rules. They can't, they aren't written in D, and they aren't checked by D. There is no way to express the invariants that they require, and there certainly isn't a way to infer those invariants based on the types of the parameters.

Marking them @safe by default is a disaster. It is the complete and utter destruction of memory safety in D. I can't stress this enough.

I wish Walter had not brought this up, because I don't think it's fruitful to have this discussion again.

-Steve

June 09, 2022

On Thursday, 9 June 2022 at 01:10:04 UTC, forkit wrote:

>

All these attributes you mention, wanna make my head explode ;-)

They are very funny .
Obviously simple, they say there is a cognitive burden.
And trouble,They said it wasn't that hard.

June 09, 2022

On Thursday, 9 June 2022 at 01:35:43 UTC, zjh wrote:

>

And trouble,They said it wasn't that hard.

We hope to have a complete, comprehensive and accurate introduction to this 'dip1000' , otherwise no one can use it rightly!

June 08, 2022

On 6/8/22 9:27 PM, Mathias LANG wrote:

>

On Thursday, 9 June 2022 at 01:18:30 UTC, Steven Schveighoffer wrote:

>
  1. The programmer is not expecting this. They did not write scope, they wrote in, which according to the spec is "equivalent to const" (see https://dlang.org/spec/function.html#in-params). I'm convinced that we absolutely cannot turn on preview in by default until this is addressed. I can't even recommend using the preview switch, as this is too dangerous for memory safety.

According to the spec, in means const scope when you are using -preview=in.

Existing code likely does not use -preview=in.

-Steve

June 08, 2022
On 6/8/22 18:34, Steven Schveighoffer wrote:

> You are missing the point.

Clearly. :) I actually want checked-by-default but don't know how to get it for D code around C library calls. But I think I see it better now.

Ali

June 08, 2022
On 6/8/2022 5:38 PM, Timon Gehr wrote:
> This does not have anything to do with `@safe` by default, it's just an inconsistency in the compiler implementation.

I could make a case for every one of the safety checks being checked in @system code, too.

The existing checks you note were there long before @safe/@trusted/@system was added. Them remaining is an artifact of evolution and a wish to support legacy behavior.