Jump to page: 1 2 3
Thread overview
My late christmas present for you: context-aware assertion error messages
Jan 12, 2019
Seb
Jan 12, 2019
Andre Pany
Jan 12, 2019
H. S. Teoh
Jan 12, 2019
Seb
Jan 12, 2019
H. S. Teoh
Jan 12, 2019
Seb
Jan 14, 2019
WebFreak001
Jan 12, 2019
viniarck
Jan 12, 2019
viniarck
Jan 12, 2019
Dennis
Jan 12, 2019
Meta
Jan 13, 2019
Per Nordlöw
Jan 13, 2019
Seb
Jan 13, 2019
Per Nordlöw
Jan 13, 2019
Seb
Jan 13, 2019
Per Nordlöw
Jan 13, 2019
Vladimir Panteleev
Jan 14, 2019
Atila Neves
Jan 14, 2019
Seb
Jan 15, 2019
Kagamin
Jan 15, 2019
Seb
Jan 15, 2019
Kagamin
Jan 15, 2019
rjframe
Jan 15, 2019
Jacob Carlborg
Jan 15, 2019
Kagamin
Jan 15, 2019
Seb
January 12, 2019
tl;dr: I was annoyed for years that D's assert are so non informative AssertError, so here's my late Christmas present for the D community. With 2.085 DMD will gain an experimental flag for more informative assertion errors. Feedback on this experimental feature is welcome, s.t. we eventually can enable it by default.

Example:

---
void main()
{
    int a = 1, b = 2;
    assert(a == b); // ERROR: 1 != 2
}
---

```
> dmd -run onlineapp.d
core.exception.AssertError@onlineapp.d(4): 1 != 2
----------------
??:? _d_assert_msg [0xb150e350]
??:? _Dmain [0xb150d627]
```

Play online: https://run.dlang.io/is/GMfe9S
Full changelog: https://dlang.org/changelog/pending.html#assert


Where to start hacking:
- https://github.com/dlang/dmd/blob/00299e3b6ca9dcd5a5dc8bd50280b63d0bdc51f9/src/dmd/expressionsem.d#L5559
- https://github.com/dlang/druntime/blob/master/src/core/internal/dassert.d
- https://github.com/dlang/dmd/pull/8517

Q: Why not just introduce an `assertEqual` user function?
A:
- there's a lot of already written D code out there that uses `assert` [1]
- `assertEqual` wouldn't cut, it because then we would need to have an `assertLessThan` etc. too

Q: Why put this in the compiler and not in fluent-assert or similar library?
A:
- there's a lot of already written D code out there that uses `assert` [1]
- not everyone wants to use a library just to have somewhat decent assert messages
- libraries can only do sth. like `testedValue.should.equal(42)`, but the compiler can deal with `testedValue == 42`

[1] https://github.com/search?l=D&q=assert&type=Code

Error context
-------------

Also note that with 2.085 DMD will get -verrors=context


---
void foo()
{
    a = 1;
}
---


```
> dmd -verrors=context onlineapp.d
onlineapp.d(3): Error: undefined identifier a
    a = 1;
    ^
```

Full changelog: https://dlang.org/changelog/pending.html#error-context
Play online: https://run.dlang.io/is/8gsye1

Thanks to all the people (Jacob Carlborg, Petar Kirov, Nicolas Wilson, Rainer Schuetze, etc.) who helped me to get this feature into DMD!

Merry Christmas!
January 12, 2019
On Saturday, 12 January 2019 at 15:34:02 UTC, Seb wrote:
...
> Full changelog: https://dlang.org/changelog/pending.html#error-context
> Play online: https://run.dlang.io/is/8gsye1
>
> Thanks to all the people (Jacob Carlborg, Petar Kirov, Nicolas Wilson, Rainer Schuetze, etc.) who helped me to get this feature into DMD!
>
> Merry Christmas!

Thanks for this great features. 2019 will be again a very great year for D!

Kind regards
Andre
January 12, 2019
On Sat, Jan 12, 2019 at 03:34:02PM +0000, Seb via Digitalmars-d wrote: [...]
> ---
> void main()
> {
>     int a = 1, b = 2;
>     assert(a == b); // ERROR: 1 != 2
> }
> ---
> 
> ```
> > dmd -run onlineapp.d
> core.exception.AssertError@onlineapp.d(4): 1 != 2

Awesome!


[...]
> Also note that with 2.085 DMD will get -verrors=context
[...]
> ---
> void foo()
> {
>     a = 1;
> }
> ---
> 
> 
> ```
> > dmd -verrors=context onlineapp.d
> onlineapp.d(3): Error: undefined identifier a
>     a = 1;
>     ^
> ```

Awesome!

It's about time dmd error messages got a facelift.  Will -verrors=context eventually become the default?


T

-- 
The irony is that Bill Gates claims to be making a stable operating system and Linus Torvalds claims to be trying to take over the world. -- Anonymous
January 12, 2019
On Saturday, 12 January 2019 at 16:17:07 UTC, H. S. Teoh wrote:
> Awesome!
>
> It's about time dmd error messages got a facelift.  Will -verrors=context eventually become the default?


Yes, I hope so!
Once it has gotten a fair bit of real-world feedback and testing (which is the one of the reasons why I opened this thread) we can enable it by default if a TTY is detected.

TTY detection is probably necessary as there are still a lot of programs (especially editor plugins) which parse the DMD output.


January 12, 2019
On Saturday, 12 January 2019 at 15:34:02 UTC, Seb wrote:
> tl;dr: I was annoyed for years that D's assert are so non informative AssertError, so here's my late Christmas present for the D community. With 2.085 DMD will gain an experimental flag for more informative assertion errors. Feedback on this experimental feature is welcome, s.t. we eventually can enable it by default.
>
> [...]

Epic! That's massively useful. I Such a great start for D in 2019 :)

Thanks a lot, Seb! I'll use this new version soon.
January 12, 2019
On Saturday, 12 January 2019 at 16:28:57 UTC, viniarck wrote:
> On Saturday, 12 January 2019 at 15:34:02 UTC, Seb wrote:
>> tl;dr: I was annoyed for years that D's assert are so non informative AssertError, so here's my late Christmas present for the D community. With 2.085 DMD will gain an experimental flag for more informative assertion errors. Feedback on this experimental feature is welcome, s.t. we eventually can enable it by default.
>>
>> [...]
>
> Epic! That's massively useful. I Such a great start for D in 2019 :)
>
> Thanks a lot, Seb! I'll use this new version soon.

My bad for the typo. I meant to type "It's such a great start for D in 2019".
January 12, 2019
On Sat, Jan 12, 2019 at 04:23:53PM +0000, Seb via Digitalmars-d wrote:
> On Saturday, 12 January 2019 at 16:17:07 UTC, H. S. Teoh wrote:
> > Awesome!
> > 
> > It's about time dmd error messages got a facelift.  Will -verrors=context eventually become the default?
> 
> 
> Yes, I hope so!
> Once it has gotten a fair bit of real-world feedback and testing
> (which is the one of the reasons why I opened this thread) we can
> enable it by default if a TTY is detected.
> 
> TTY detection is probably necessary as there are still a lot of programs (especially editor plugins) which parse the DMD output.
[...]

Seriously, for editor/IDE plugins, there should be a --porcelain option that outputs error messages in a machine-friendly format, like with a fixed format that's easy for scripts / code / whatever to parse.


T

-- 
Life is too short to run proprietary software. -- Bdale Garbee
January 12, 2019
On Saturday, 12 January 2019 at 16:32:43 UTC, H. S. Teoh wrote:
>
> Seriously, for editor/IDE plugins, there should be a --porcelain option that outputs error messages in a machine-friendly format, like with a fixed format that's easy for scripts / code / whatever to parse.
>
>
> T

It probably would be -verrors=json (or sth. like this), but the problem is that tools which parse DMD output are already out there, so we need to be a bit careful not to break them.

Anyhow, I think using TTY detection to enable -verrors=context by default should work fine.
DMD did/does the same when color-coded messages (`-color` ) where introduced.
January 12, 2019
On Saturday, 12 January 2019 at 15:34:02 UTC, Seb wrote:
> Merry Christmas!

Amazing. I saw the PR a while back and was afraid it would be pushed aside in favor of library solutions, but whenever I quickly write unittests for my modules I never feel like dragging in a fluent assert library and end up manually putting writeln's everywhere, so I'm super glad it's actually coming to dmd.
January 12, 2019
On Saturday, 12 January 2019 at 15:34:02 UTC, Seb wrote:
> tl;dr: I was annoyed for years that D's assert are so non informative AssertError, so here's my late Christmas present for the D community. With 2.085 DMD will gain an experimental flag for more informative assertion errors. Feedback on this experimental feature is welcome, s.t. we eventually can enable it by default.
>
> Example:
>
> ---
> void main()
> {
>     int a = 1, b = 2;
>     assert(a == b); // ERROR: 1 != 2
> }
> ---
>
> ```
>> dmd -run onlineapp.d
> core.exception.AssertError@onlineapp.d(4): 1 != 2
> ----------------
> ??:? _d_assert_msg [0xb150e350]
> ??:? _Dmain [0xb150d627]
> ```
>
> Play online: https://run.dlang.io/is/GMfe9S
> Full changelog: https://dlang.org/changelog/pending.html#assert
>
>
> Where to start hacking:
> - https://github.com/dlang/dmd/blob/00299e3b6ca9dcd5a5dc8bd50280b63d0bdc51f9/src/dmd/expressionsem.d#L5559
> - https://github.com/dlang/druntime/blob/master/src/core/internal/dassert.d
> - https://github.com/dlang/dmd/pull/8517
>
> Q: Why not just introduce an `assertEqual` user function?
> A:
> - there's a lot of already written D code out there that uses `assert` [1]
> - `assertEqual` wouldn't cut, it because then we would need to have an `assertLessThan` etc. too
>
> Q: Why put this in the compiler and not in fluent-assert or similar library?
> A:
> - there's a lot of already written D code out there that uses `assert` [1]
> - not everyone wants to use a library just to have somewhat decent assert messages
> - libraries can only do sth. like `testedValue.should.equal(42)`, but the compiler can deal with `testedValue == 42`
>
> [1] https://github.com/search?l=D&q=assert&type=Code
>
> Error context
> -------------
>
> Also note that with 2.085 DMD will get -verrors=context
>
>
> ---
> void foo()
> {
>     a = 1;
> }
> ---
>
>
> ```
>> dmd -verrors=context onlineapp.d
> onlineapp.d(3): Error: undefined identifier a
>     a = 1;
>     ^
> ```
>
> Full changelog: https://dlang.org/changelog/pending.html#error-context
> Play online: https://run.dlang.io/is/8gsye1
>
> Thanks to all the people (Jacob Carlborg, Petar Kirov, Nicolas Wilson, Rainer Schuetze, etc.) who helped me to get this feature into DMD!
>
> Merry Christmas!

Thank you so much for working on this. There's nothing more infuriating than my program crashing due to a failed assert, then having to edit and recompile it just to put in some debug printing. It's not a big problem when I take the time to do things properly and put the relevant information into the assert message, but when I'm quickly hacking on some code it's annoying to do it for every single assert (I generally use asserts and DBC religiously to ensure preconditions and assumptions actually hold).

This is one of those little things that is so small but is also extremely annoying, so thank you again for taking the time to fix it.
« First   ‹ Prev
1 2 3