News for hackers. Stuff that matters.
Re:D as a Better C by Ace17 on Wednesday August 23, 2017 @12:52PM (#15083067)
Why remove RAII?

It's not fundamentally incompatible with "Better C" semantics, especially if there are no exceptions.

Re:D as a Better C by WalterBright on Wednesday August 23, 2017 @01:10PM (#15083235)
RAII requires exceptions to be correct. I hesitate to say it it has RAII otherwise.

Exceptions have two issues:

1. D exceptions currently require the GC. There is a Pull Request to fix that, but it isn't incorporated yet.

2. More problematic is the Dwarf exception handling mechanism requires a language specific "personality" handler. This is supplied by the D runtime library. Trying to trick the C runtime library one into working with D isn't a solved problem at the moment.

Re:D as a Better C by vvanders on Wednesday August 23, 2017 @03:50PM (#15084489)
Rust uses RAII all over the place and doesn't have exceptions :).
Re:D as a Better C by millstone on Wednesday August 23, 2017 @02:07PM (#15083705)
Why does RAII require exceptions? RAII is routinely used in C++ with -fno-exceptions.
Re:D as a Better C by WalterBright on Wednesday August 23, 2017 @03:05PM (#15084203)
Because D code may sit in between code that throws and exception and code that handles it. Without EH support, the RAII destructor won't get called when the stack is unwound.
Re:D as a Better C by dbaupp on Wednesday August 23, 2017 @05:22PM (#15085297)
Isn't it generally undefined behaviour to have exceptions pass through code that isn't compiled with support for them? I.e. not running some destructors is the least of your worries if an exception hits C/-fno-exceptions/-betterC code.
Re:D as a Better C by WalterBright on Wednesday August 23, 2017 @06:58PM (#15085932)
Undefined behavior or not, implementations of languages in gcc and Windows tend to support it, even if the language itself does not. I consider it my job to make this work if at all practical, because users do not like ugly surprises.

I don't know how Rust's RAII deals with exceptions thrown by lower level code. If someone better acquainted with Rust could chime in here, it would be interesting.

Re:D as a Better C by steveklabnik on Wednesday August 23, 2017 @07:34PM (#15086195)
> I don't know how Rust's RAII deals with exceptions thrown by lower level code.

Unwinding across an FFI boundary is considered undefined behavior.

https://doc.rust-lang.org/reference/behavior-considered-unde... last bullet point.

Re:D as a Better C by WalterBright on Wednesday August 23, 2017 @08:56PM (#15086727)
That's exactly what I wanted to know. Thanks!

Full D is compatible with foreign exceptions and unwinding. I'll see about making D as Better C also compatible, but I suppose Rust's precedent makes it acceptable to not handle it.

Re:D as a Better C by steveklabnik on Wednesday August 23, 2017 @08:59PM (#15086746)
No problem!

This isn't my super strong area of expertise, but don't you have to specify how the unwinding works? I believe this is part of the reason why Rust punted; you'd have to specify exactly what kind is supported. (Rust uses libunwind for panics, incidentally.)

Re:D as a Better C by WalterBright on Wednesday August 23, 2017 @09:10PM (#15086818)
The how is handled by providing a language-specific "personality" function in the generated exception tables. The D runtime library has one for D. But with D as Better C, I'd have to find a way to make the C personality function work for D.
Re:D as a Better C by steveklabnik on Wednesday August 23, 2017 @10:51PM (#15087261)
Ah right, makes sense. Thanks :)
Re:D as a Better C by fasquoika on Wednesday August 23, 2017 @07:28PM (#15086143)
If you mean lower level Rust code, there are no exceptions. Errors are just regular values encoded in the type system. If you're familiar with OCaml optionals and results, it's the same thing
Re:D as a Better C by dbaupp on Wednesday August 23, 2017 @08:03PM (#15086398)
There's a subtlety here: panics are exceptions, but they can be disabled without also disabling RAII.
Re:D as a Better C by dbaupp on Wednesday August 23, 2017 @08:10PM (#15086459)
How does it work if an exception gets thrown through a C stack frame that has outstanding resources (for instance, pointers that are freed later in the frame)? Are you referring to manual SJLJ?

In any case, it seems like C manual destruction has exactly the same problems as with RAII, because fundamentally the issue is passing exceptions through something that doesn't understand it, rather than with RAII.

Re:D as a Better C by millstone on Wednesday August 23, 2017 @03:51PM (#15084494)
Gotcha. C++ has the same problem when its exceptions are thrown through C or -fno-exceptions C++ frames.

If that's the only hold-up, IMO the way to go is to abort at the boundary, when an exception would propagate into frames without unwind tables. RAII is too valuable to do without!

Re:D as a Better C by andrewflnr on Wednesday August 23, 2017 @02:00PM (#15083661)
> RAII requires exceptions to be correct.

I'm not exactly sure what you mean here, but all I really want is guaranteed destructors. Can we just have those?

Re:D as a Better C by WalterBright on Wednesday August 23, 2017 @03:12PM (#15084243)
> Can we just have those?

It's a good thought. D as Better C is new for us, and we'll be flexible in making it work better.

Re:D as a Better C by zombinedev on Wednesday August 23, 2017 @12:54PM (#15083089)
RAII in not removed by design. It is just work in progress. It will be added to -betterC when its implementation is more mature for this mode.