5 days ago
On 02/05/2025 7:44 PM, Manu wrote:
> On a slight tangent; why do I need to attribute the function at all for it to check `scope` args don't escape?
> I do want to add `scope` to pointers and ref arguments, but I don't see any reason that I should have to attribute `live`... why isn't `scope` enough to enable escape analysis?

DIP1000 was the escape analysis meant to be enabled by scope.

And it can't be turned on.

5 days ago

On Tuesday, 29 April 2025 at 17:12:41 UTC, Walter Bright wrote:

>

I was quite intrigued with the borrow checker, and set about learning about it. While D cannot be retrofitted with a borrow checker, it can be enhanced with it. A borrow checker has nothing tying it to the Rust syntax, so it should work.

That's right.

>

So I implemented a borrow checker for D, and it is enabled by adding the @live annotation for a function, which turns on the borrow checker for that function. There are no syntax or semantic changes to the language, other than laying on a borrow checker.

There's a difference.

In Rust, as I understand it, if you have a function like

fn free(ptr: MyCustomPointer)
{   // ...
}

it is 100% safe to use. The compiler will not let you double-free or use after free, unless you use the unsafe block to do so.

But you can't have

@trusted void free(MyCustomPointer ptr)
{   // ...
}

in D because it would be unsafe to use from a non-@live function.

If we had a way to say that "this function can be called from @safe, if and only if it's usage is guarded with @live" then it would be equal to the Rust borrow checker.

5 days ago

On Friday, 2 May 2025 at 10:14:34 UTC, Dukc wrote:

>

There's a difference.

In Rust, as I understand it, if you have a function like

fn free(ptr: MyCustomPointer)
{   // ...
}

it is 100% safe to use. The compiler will not let you double-free or use after free, unless you use the unsafe block to do so.

But you can't have

@trusted void free(MyCustomPointer ptr)
{   // ...
}

in D because it would be unsafe to use from a non-@live function.

If we had a way to say that "this function can be called from @safe, if and only if it's usage is guarded with @live" then it would be equal to the Rust borrow checker.

I was trying to make this point before, but think you do it better here.

5 days ago

On Friday, 2 May 2025 at 07:44:52 UTC, Manu wrote:

>

On a slight tangent; why do I need to attribute the function at all for it check scope args don't escape?

You don't need @live for that, only @safe and -preview=dip1000. @live / -preview=dip1021 checks for mutable aliasing, which is something added on top of scope escape analysis.

5 days ago

On Friday, 2 May 2025 at 10:14:34 UTC, Dukc wrote:

>

There's a difference.

In Rust, as I understand it, if you have a function like

fn free(ptr: MyCustomPointer)
{   // ...
}

it is 100% safe to use. The compiler will not let you double-free or use after free, unless you use the unsafe block to do so.

But you can't have

@trusted void free(MyCustomPointer ptr)
{   // ...
}

in D because it would be unsafe to use from a non-@live function.

+100

Rust programmers don't put up with restrictions of the borrow checker just for fun. They do it because it allows the compiler to verify the safety of their code.

A borrow checker that can't be used for safety verification is like an oven mitt that can't be used to hold a hot pan. All it does is make things awkward and uncomfortable, and if you actually try to rely on it, you'll end up getting hurt.

5 days ago
On Tuesday, 29 April 2025 at 17:12:41 UTC, Walter Bright wrote:
>
> It reminds me of OOP. OOP was sold as the answer to every programming problem. Many OOP languages appeared. But, eventually, things died down and OOP became just another tool in the toolbox. D and C++ support OOP, too.

My issue with all the talk about how some features could our savior is that they are nowhere to be found in our direct competitors.

There is no surge of competitors boosted or enabled by memory-safety, borrow-checking, lack of data races or even advanced types. There is very little Rust competitors, to the point I'd argue people self-select out of the market by spending time on expensive features like those that bring no bread on the table. For example we have zero Haskell, Clojure, or Elm competitors, or even Ocaml in the audio space (that one could work there probably).

But there is a very real threat of competitors being faster with lower iteration times.


5 days ago
On 5/2/25 16:53, Guillaume Piolat wrote:
> On Tuesday, 29 April 2025 at 17:12:41 UTC, Walter Bright wrote:
>>
>> It reminds me of OOP. OOP was sold as the answer to every programming problem. Many OOP languages appeared. But, eventually, things died down and OOP became just another tool in the toolbox. D and C++ support OOP, too.
> 
> My issue with all the talk about how some features could our savior is that they are nowhere to be found in our direct competitors.
> 
> There is no surge of competitors boosted or enabled by memory-safety, 

(`@live` does not give you memory safety guarantees.)

> borrow-checking, lack of data races or even advanced types. There is very little Rust competitors, to the point I'd argue people self-select out of the market by spending time on expensive features like those that bring no bread on the table. For example we have zero Haskell, Clojure, or Elm competitors, or even Ocaml in the audio space (that one could work there probably).
> 
> But there is a very real threat of competitors being faster with lower iteration times.
> 
> 
Well, beyond the competitive edge that D affords you, to some extent users get what they pay/wait for and I think/hope users will be increasingly conscious of the possibility that someone might take over their computer by e.g. sending them an mp3 file that is invalid in a peculiar way.

The point of memory safety features is not really to help you write faster unsafe code with lower iteration times (though they sometimes do).

The goal is to help you write faster safe code with lower iteration times. In some domains, safety is, or will be, a requirement on the functionality of the software.

Some businesses don't care about cybersecurity until they or their users get hacked (regularly). Of course, memory corruption is only one way attackers can succeed, so advanced type system features can help. (More advanced than what you cite though, so we'll see how quickly those can be transferred to industry. It seems that usually the type system tech in popular languages is on the order of 40 years old.)

`@live` however does not give you additional hard guarantees, so I also think a priori it is not a feature on whose implementation I would spend time personally. Perhaps the work put into it can be reused for something more useful in the future.
5 days ago

On Friday, 2 May 2025 at 19:25:53 UTC, Timon Gehr wrote:

>

On 5/2/25 16:53, Guillaume Piolat wrote:

>

On Tuesday, 29 April 2025 at 17:12:41 UTC, Walter Bright wrote:
For example we have zero Haskell, Clojure, or Elm competitors

Of course, memory corruption is only one way attackers can succeed, so advanced type system features can help. (More advanced than what you cite though, so we'll see how quickly those can be transferred to industry.

More advanced than Haskell? Wat?

IMO it's absolutely mind-blowing how advanced Haskell is when you enable the more recent features such as GADTs. I feel it's already so powerful that my comprehension runs out well before the expressive power of the language, and I don't think I'm bad at comprehending programming language features.

5 days ago
On 5/2/25 21:48, Dukc wrote:
> On Friday, 2 May 2025 at 19:25:53 UTC, Timon Gehr wrote:
>> On 5/2/25 16:53, Guillaume Piolat wrote:
>>> On Tuesday, 29 April 2025 at 17:12:41 UTC, Walter Bright wrote:
>>> For example we have zero Haskell, Clojure, or Elm competitors
>>>
>> Of course, memory corruption is only one way attackers can succeed, so advanced type system features can help. (More advanced than what you cite though, so we'll see how quickly those can be transferred to industry.
> 
> More advanced than _Haskell_? Wat?
> 
> IMO it's absolutely mind-blowing how advanced Haskell is when you enable the more recent features such as GADTs. I feel it's already so powerful that my comprehension runs out well before the expressive power of the language, and I don't think I'm bad at comprehending programming language features.

See e.g., https://ghc.serokell.io/dh

In any case, comprehensibility is at most loosely related to expressiveness. I think Haskell with all its extensions is too complex for what it gives you in terms of expressiveness.
4 days ago
On 5/2/2025 12:44 AM, Manu wrote:
> On a slight tangent; why do I need to attribute the function at all for it to check `scope` args don't escape?

Because otherwise the compiler has to examine the function implementation to verify this. Doing such is called attribute inference, and D does quite a bit of that. Problems are:

1. Function declarations don't have a body, so there's no way to inspect the body.

2. Inspecting all the bodies means compilation gets slowed down quite a bit.

3. Chicken-and-egg problems when inferring attributes when there are loops in the function call flow graph.

> I do want to add `scope` to pointers and ref arguments, but I don't see any reason that I should have to attribute `live`... why isn't `scope` enough to enable escape analysis?

The DFA (Data Flow Analysis) required to determine whether a pointer is the "owner" or not at each point in the function is considerably more complex than just checking for scope-ness. DFA equations are generated and then solved iteratively.

The code required to do it is:

https://github.com/dlang/dmd/blob/master/compiler/src/dmd/ob.d

which is very similar to the DFA performed by the optimizer:

https://github.com/dlang/dmd/blob/master/compiler/src/dmd/backend/gflow.d