bottom type as parameter or local variable, does that make sense?
Paul Backus
snarwin at gmail.com
Fri Jan 14 16:32:22 UTC 2022
On Friday, 14 January 2022 at 15:07:13 UTC, H. S. Teoh wrote:
> On Fri, Jan 14, 2022 at 02:05:25PM +0000, vit via Digitalmars-d
> wrote:
>> On Friday, 14 January 2022 at 13:58:38 UTC, H. S. Teoh wrote:
>> > On Fri, Jan 14, 2022 at 11:52:04AM +0000, WebFreak001 via
>> > Digitalmars-d wrote:
>> > [...]
>> > > ```d
>> > > void bar()
>> > > {
>> > > noreturn b;
>> > > writeln("calling");
>> > > foo(b);
>> > > }
>> > > ```
>> > [...]
>> >
>> > Wait, doesn't the DIP say that while declaring a variable of
>> > type `noreturn` is technically allowed, it should abort at
>> > runtime as soon as the variable is initialized? Why is
>> > program actually running past that line??!
> [...]
>> Whith explicit init it works like that:
>>
>> ```d
>> import std.stdio;
>>
>>
>> void main(){
>> noreturn n = noreturn.init;
>> writeln("bar");
>> }
>>
>> ```
>> Print:
>> ```
>> //Illegal instruction (core dumped)
>> ```
>
> IMO, that's a bug. Implicit initialization of noreturn should
> behave exactly the same way as invoking noreturn.init
> explicitly.
>
> Somebody should file a bug, if one hasn't been filed already.
That's not what the bug is here. `noreturn` is the type of
expressions whose evaluation does not halt; i.e., it is a type
with no values. Therefore, declaring a variable of type
`noreturn` is a no-op: all it does is add a symbol to the current
scope. No initialization is performed, because there is nothing
to initialize.
On the other hand, this also means that any attempt to *evaluate*
a `noreturn` *expression* (such as `noreturn.init`) can never
succeed. So, a function call expression with a `noreturn`
argument can never result in the function actually being called:
it must either enter an infinite loop, throw an exception,
terminate the program, etc.
Some `noreturn` expressions, like `throw new Exception` and `() {
while(1) {} }()`, already had specific runtime behaviors defined
by the language spec. Others, like `noreturn.init`, did not. For
the latter kind of `noreturn` expression, the DIP specifies that
their behavior at runtime is equivalent to `assert(0)`.
More information about the Digitalmars-d
mailing list