www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - unexpected noreturn behavior

reply WebFreak001 <d.forum webfreak.org> writes:
What would you expect for this code?

```d
struct Something(Types...) {}

enum isSomethingExact(T) = is(T == Something!Types, Types...);
enum isSomething(T) = is(T : Something!Types, Types...);

pragma(msg, isSomethingExact!noreturn);
pragma(msg, isSomething!noreturn);
```

This currently outputs `false`, `true` which breaks my code 
because more concretely `isSumType!(typeof(someMethod()))` 
returns true. I can add a check for noreturn before that, but it 
seems a little weird that noreturn returns true for isSomething!T 
and functions like that in phobos.
Apr 21 2022
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
noreturn is the bottom type which can implicitly convert to any type, 
including void. A value of type noreturn will never be produced and the 
compiler can optimize such code accordingly.

https://dlang.org/spec/type.html#noreturn
Apr 21 2022
parent reply WebFreak001 <d.forum webfreak.org> writes:
On Thursday, 21 April 2022 at 12:28:37 UTC, rikki cattermole 
wrote:
 noreturn is the bottom type which can implicitly convert to any 
 type, including void. A value of type noreturn will never be 
 produced and the compiler can optimize such code accordingly.

 https://dlang.org/spec/type.html#noreturn
ok so I guess all the `isSomething(T)` functions must be written like this then: ```d enum isSomething(T) = !is(immutable T == immutable noreturn) && is(T : Something!Other, Other...); ``` which I think is a little bug-prone, but at least that would solve my issues.
Apr 21 2022
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
Could we add a check for this in DScanner?

Otherwise I'm not sure how else we are going to find all of these 
instances and fix them.
Apr 21 2022
prev sibling parent reply Dennis <dkorpel gmail.com> writes:
On Thursday, 21 April 2022 at 12:41:08 UTC, WebFreak001 wrote:
 which I think is a little bug-prone, but at least that would 
 solve my issues.
What issue do you have with it returning `true`? Note that this compiles: ```D safe: import std.sumtype; void main() { SumType!(int, string) s = assert(0); } ```
Apr 21 2022
parent Paul Backus <snarwin gmail.com> writes:
On Thursday, 21 April 2022 at 12:54:12 UTC, Dennis wrote:
 On Thursday, 21 April 2022 at 12:41:08 UTC, WebFreak001 wrote:
 which I think is a little bug-prone, but at least that would 
 solve my issues.
What issue do you have with it returning `true`?
Presumably the problem is that if you write something like `assert(0).match!();`, you get a spew of compiler errors, on account of the fact that `match` assumes anything that satisfies `isSumType` has properties like `Types` and `get!T`.
Apr 21 2022