Bug 23587 – cast(void) doesn't work for noreturn

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-12-28T13:15:15Z
Last change time
2022-12-31T14:27:43Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
artha
See also
https://issues.dlang.org/show_bug.cgi?id=23591

Comments

Comment #0 by artha — 2022-12-28T13:15:15Z
``` noreturn stuff() { assert(false); } void doStuff(alias fun)() { // This is a template, so I don't want to have to care about noreturn cast(void) fun(); writeln("never written"); // Should output "void" pragma(msg, typeof(cast(void) fun())); } void main() { doStuff!stuff(); } ``` Starting with DMD 2.101, the pragma in the above example outputs "noreturn" rather than the expected "void" and issues "Warning: statement is not reachable".
Comment #1 by ibuclaw — 2022-12-30T00:01:53Z
Comment #2 by dlang-bot — 2022-12-30T00:34:15Z
@ibuclaw updated dlang/dmd pull request #14758 "Revert "Fix Issues 23331, 23379 - fix casts involving noreturn"" fixing this issue: - fix Issue 23587 - cast(void) doesn't work for noreturn https://github.com/dlang/dmd/pull/14758
Comment #3 by razvan.nitu1305 — 2022-12-30T09:15:52Z
Why does it matter? The call to fun will never return so casting to void (or any other value) it actually a no-op.
Comment #4 by ibuclaw — 2022-12-30T13:05:06Z
(In reply to RazvanN from comment #3) > Why does it matter? The call to fun will never return so casting to void (or > any other value) it actually a no-op. The devils in the detail of the change, not the example code in this bug report. By blowing a hole in the type system has opened up the flood gates to even worse abominations being accepted.
Comment #5 by artha — 2022-12-30T16:43:08Z
(In reply to RazvanN from comment #3) > Why does it matter? The call to fun will never return so casting to void (or > any other value) it actually a no-op. In this case it makes the compiler issue an unwanted warning. The cast is supposed to tell the compiler I'm purposefully discarding the return value. As a workaround, I could've done this: ``` fun(); static if (!is(typeof(stuff()) == void)) { writeln("never written"); } ``` But it's a mess, and I don't want to do that every time I call a template delegate.
Comment #6 by ibuclaw — 2022-12-30T23:37:31Z
(In reply to artha from comment #5) > (In reply to RazvanN from comment #3) > > Why does it matter? The call to fun will never return so casting to void (or > > any other value) it actually a no-op. > > In this case it makes the compiler issue an unwanted warning. The cast is > supposed to tell the compiler I'm purposefully discarding the return value. > You're not wrong. cast(void) bottom is completely valid and the result should be void.
Comment #7 by dlang-bot — 2022-12-31T14:27:43Z
dlang/dmd pull request #14758 "Revert "Fix Issues 23331, 23379 - fix casts involving noreturn"" was merged into master: - 8bf1c8dab73adb9b990be334cdde1a068cb0a71e by Iain Buclaw: fix Issue 23587 - cast(void) doesn't work for noreturn https://github.com/dlang/dmd/pull/14758