What should happen when the assert message expression throws?

Dennis dkorpel at gmail.com
Fri Nov 18 13:18:39 UTC 2022


My thoughts:
- A wrong format string passed to `format` should be a runtime 
error (if not caught at compile time), not an exception
- The current behavior is 'correct' if you consider `assert` a 
function call, where the argument list is evaluated before 
calling the function. I'm not a fan of adding more magic to 
`assert`, especially considering:
- This issue is not limited to exceptions, but any bottom value 
generated in the assert message. 'Fixing' it for exceptions does 
not 'fix' it for other cases:

```D
assert(x, (){while(1){} return "";}()); // endless loop before 
assert
assert(x, new char[2UL ^^ 60]); // Out of Memory before assert
assert(x, assert(0)); // assert before assert
```

On Friday, 18 November 2022 at 12:36:14 UTC, RazvanN wrote:
> This rewrite would be done only if it is determined that the 
> assert message may throw. However, the current dmd-fe 
> implementation does not allow for such a construction and I 
> think that it might be overkill to implement the necessary 
> machinery just to support this case.

You could add a minimal implementation of 
`std.exception.collectException` to druntime and use that.

> An alternative solution would be to deprecate having an assert 
> message that may throw.

That could be annoying to users since Phobos' string functions 
often may throw.



More information about the Digitalmars-d mailing list