# More structured errors

**URL:** <https://internals.rust-lang.org/t/more-structured-errors/3005>\
**Category:** compiler\
**Created:** [December 15, 2015, 4:06am UTC](https://internals.rust-lang.org/t/more-structured-errors/3005 "2015-12-15T04:06:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nrc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nrc/32/126_2.png) [@nrc](https://internals.rust-lang.org/u/nrc)\
**Post date:** [December 15, 2015, 4:06am UTC](https://internals.rust-lang.org/t/more-structured-errors/3005/1 "2015-12-15T04:06:34Z")

</div>

We would like to support emitting errors in different ways, for example as JSON or HTML. The former for easier integration with IDEs, the latter for better user understanding. The main impediment to these more structured forms is that our error messages are not particularly structured. In particular, when we issue notes, help messages, or suggestions with an error message, these are treated as independent messages with no relation. I would like to have the notes associated with the errors.

I see two ways to do this: either we support error transactions, so a multi-part error message looks like:

```rust
handler.start_transaction();
handler.span_err(...);
handler.span_note(...);
handler.span_note(...);
handler.end_transaction();

```

or we support structured exceptions:

```rust
let mut err = span_err(...);
err.note(...);
err.note(...);
handler.report(err);

```

Some of the constraints are that we should be able to handle notes which are not in the same function as the error and we should strive to make orphan notes a static error, rather than a compile-time one (if possible).

Does anyone have any better designs? Or an opinion on these two? I think I prefer the second, although the first would be less work to implement (since existing code would still compile (if we admit single errors without transactions), which also means it is potentially buggier).

---

<div class="post-metadata">

**Author:** ![huon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/huon/32/3698_2.png) [@huon](https://internals.rust-lang.org/u/huon)\
**Post date:** [December 15, 2015, 5:06am UTC](https://internals.rust-lang.org/t/more-structured-errors/3005/2 "2015-12-15T05:06:35Z")

</div>

> [@nrc](#):
>
> or we support structured exceptions:

I've been wanting to experiment with this sort of error emission for a while (even for just terminal users, this would allow us to emit errors in a much more focused way). That said, I'd thought about doing it something like:

```rust
let mut err: DiagnosticBuilder = sess.span_err(...);
err.note(...);
err.emit();

```

(i.e. have the `handler` stored by the builder internally.) Or even just having a `Drop` implementation that outputs the error (although, this is possibly unnecessarily magic).

> [@nrc](#):
>
> Some of the constraints are that we should be able to handle notes which are not in the same function as the error

Is this adaquately handled by passing the `DiagnosticBuilder` into the function emitting a note?

> [@nrc](#):
>
> and we should strive to make orphan notes a static error, rather than a compile-time one (if possible).

I think this is handled by (eventually) only providing the `note` function on `DiagnosticBuilder`, so the only way to actually emit a note is in the process of emitting a more important diagnostic.

> [@nrc](#):
>
> although the first would be less work to implement (since existing code would still compile (if we admit single errors without transactions), which also means it is potentially buggier).

It seems like the second can be implemented in a way that lets current code still compile, which can be migrated onto the newer strategy progressively. The old functions could even have a separate feature gate (i.e. not `rustc_internals`) so that people won't accidentally add more uses of them to crates that have been migrated (and hence had the `#[feature]` removed).

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [December 15, 2015, 8:35am UTC](https://internals.rust-lang.org/t/more-structured-errors/3005/3 "2015-12-15T08:35:12Z")

</div>

too early for bikeshedding?

`DiagnosticBuilder` could be made `must_use` and `span_{err, lint, warn}` would return it like in the second example. The only difference would be that `note` and `help` actually take the `DiagnosticBuilder` by value and return it again, and finally `emit` consumes it properly.

```rust
sess.span_err(...)
    .note(...)
    .emit();

```

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [December 15, 2015, 10:34am UTC](https://internals.rust-lang.org/t/more-structured-errors/3005/4 "2015-12-15T10:34:02Z")

</div>

> [@nrc](#):
>
> Does anyone have any better designs? Or an opinion on these two? I think I prefer the second, although the first would be less work to implement (since existing code would still compile (if we admit single errors without transactions), which also means it is potentially buggier).

I've been thinking about this a lot. I want to group errors into transactions, yes, but I also want to go further, and support a kind of markup. Basically I want you to be able to "tag" parts of the error string with spans, to support a multipart error. For example, I plan to convert borrow check errors so that they look something like what is shown in [this gist](https://gist.github.com/nikomatsakis/fd8b4e0262b9aa0fc5c9/) (compare "borrowck-error-new.txt" against "borrowck-error-old.txt").

Still, making things transactional is an obvious first step in this direction. In this respect, I prefer the second of your alternatives:

```rust
let mut err = span_err(...);
err.note(...);
err.note(...);
handler.report(err);

```

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [December 15, 2015, 10:34am UTC](https://internals.rust-lang.org/t/more-structured-errors/3005/5 "2015-12-15T10:34:37Z")

</div>

well, I prefer the builder approaches others already suggested even more 😄

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [December 15, 2015, 6:34pm UTC](https://internals.rust-lang.org/t/more-structured-errors/3005/6 "2015-12-15T18:34:38Z")

</div>

The debug builders on `Formatter` worked out decently well I think: [https://doc.rust-lang.org/stable/std/fmt/struct.Formatter.html#method.debug\_struct](https://doc.rust-lang.org/stable/std/fmt/struct.Formatter.html#method.debug_struct)

---

<div class="post-metadata">

**Author:** ![nrc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nrc/32/126_2.png) [@nrc](https://internals.rust-lang.org/u/nrc)\
**Post date:** [December 17, 2015, 1:52am UTC](https://internals.rust-lang.org/t/more-structured-errors/3005/7 "2015-12-17T01:52:50Z")

</div>

Thanks all, the builder approach looks like a nice approach. I’ll start implementing soon.

I think I would like to keep convenience functions for errors without notes, since this is the majority of them at the moment. Emit in the destructor does feel a bit too magic to me. I worry a little bit about ordering too.

@nikomatsakis I also want to structure the errors more internally as well as externally. And indeed, this is meant to be a first step towards that.

---

<div class="post-metadata">

**Author:** ![nrc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nrc/32/126_2.png) [@nrc](https://internals.rust-lang.org/u/nrc)\
**Post date:** [March 25, 2019, 8:25am UTC](https://internals.rust-lang.org/t/more-structured-errors/3005/8 "2019-03-25T08:25:35Z")

</div>

Closed on March 25, 2019, 8:25am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
