|
| 1 | +# `ErrorGuaranteed` |
| 2 | + |
| 3 | +The previous sections have been about the error message that a user of the |
| 4 | +compiler sees. But emitting an error can also have a second important side |
| 5 | +effect within the compiler source code: it generates an |
| 6 | +[`ErrorGuaranteed`][errorguar]. |
| 7 | + |
| 8 | +`ErrorGuaranteed` is a zero-sized type that is unconstructable outside of the |
| 9 | +[`rustc_errors`][rerrors] crate. It is generated whenever an error is reported |
| 10 | +to the user, so that if your compiler code ever encounters a value of type |
| 11 | +`ErrorGuaranteed`, the compilation is _statically guaranteed to fail_. This is |
| 12 | +useful for avoiding unsoundness bugs because you can statically check that an |
| 13 | +error code path leads to a failure. |
| 14 | + |
| 15 | +There are some important considerations about the usage of `ErrorGuaranteed`: |
| 16 | + |
| 17 | +* It does _not_ convey information about the _kind_ of error. For example, the |
| 18 | + error may be due (indirectly) to a `delay_span_bug` or other compiler error. |
| 19 | + Thus, you should not rely on |
| 20 | + `ErrorGuaranteed` when deciding whether to emit an error, or what kind of error |
| 21 | + to emit. |
| 22 | + |
| 23 | +* `ErrorGuaranteed` should not be used to indicate that a compilation _will |
| 24 | + emit_ an error in the future. It should be used to indicate that an error |
| 25 | + _has already been_ emitted -- that is, the [`emit()`][emit] function has |
| 26 | + already been called. For example, if we detect that a future part of the |
| 27 | + compiler will error, we _cannot_ use `ErrorGuaranteed` unless we first emit |
| 28 | + an error ourselves. |
| 29 | + |
| 30 | +Thankfully, in most cases, it should be statically impossible to abuse |
| 31 | +`ErrorGuaranteed`. |
| 32 | + |
| 33 | + |
| 34 | +[errorguar]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.ErrorGuaranteed.html |
| 35 | +[rerrors]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/index.html |
| 36 | +[dsp]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Handler.html#method.delay_span_bug |
| 37 | +[emit]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/diagnostic_builder/struct.DiagnosticBuilder.html#method.emit |
0 commit comments