On Sunday, 16 January 2022 at 22:56:44 UTC, Max Samukha wrote:
On Saturday, 15 January 2022 at 18:35:02 UTC, Paul Backus wrote:
Technically, a variable is a name associated with a block of memory, which may or may not contain a value. In the case of
noreturn, the block of memory has size 0, and there are no possible values it can contain.
Memory of size 0 contains a single value, which is the value of the unit type, 0-tuple, or whatever. Zero type is different. Timon, I am totally confused, could you clarify?
Unit type = size 0, 1 possible value.
Bottom type = size 0, 0 possible values.
The reason there are 0 possible values is because it is the bottom type, not because its size is 0.
So, technically, a
noreturn variable is impossible to initialize. But since the compiler knows that the behavior of the program can't possibly depend on the variable's value, it is free to just skip the initialization altogether and leave the variable uninitialized instead.
Sounds like "alias x = noreturn.init;"
alias an expression, so the above is ill-formed.
I guess you could make an argument that you should have to write
noreturn x = void; every time you declare a
noreturn variable. But even then, it would not be correct for
noreturn x; to result in an
assert(0) at runtime--it would have to be a compile-time error.
I think it depends on the definition of local declaration. For statics, it is obviously a compile-time error. For locals, my feeling is it should be a runtime error, but who knows. Timon, help.
It would be a compile-time error for essentially the same reason that default-initializing a type with a @disabled default constructor is a compile-time error: you have asked the compiler to do something that it knows, at compile time, is impossible to do.
I think maybe the misconception that is leading you astray here is that you assume that "default-initializing a variable of type
T" requires "evaluating the expression
T.init". It does not. What default-initializing a variable of type
T requires is
- Determining the in-memory representation of
T's default value.
- Arranging for that representation to be placed in the variable's memory at the start of its lifetime.
In the case of
noreturn, the in-memory representation is "nothing", because
noreturn has no default value (nor any other values), and arranging for that representation to be placed in the appropriate memory is either impossible or a no-op (depending on how you view things), because there is nothing to place and no memory to place it in.