Skip to content

Commit f320bf7

Browse files
mark-i-mpierwillnikomatsakis
authored
Document ErrorGuaranteed (rust-lang#1316)
* document ErrorGuaranteed * Fix typos Co-authored-by: pierwill <[email protected]> * Clarify Niko comment Co-authored-by: Niko Matsakis <[email protected]> Co-authored-by: pierwill <[email protected]> Co-authored-by: Niko Matsakis <[email protected]>
1 parent 8c6f4f4 commit f320bf7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/doc/rustc-dev-guide/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
- [`LintStore`](./diagnostics/lintstore.md)
143143
- [Diagnostic Codes](./diagnostics/diagnostic-codes.md)
144144
- [Diagnostic Items](./diagnostics/diagnostic-items.md)
145+
- [`ErrorGuaranteed`](./diagnostics/error-guaranteed.md)
145146

146147
# MIR to Binaries
147148

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

Comments
 (0)