Skip to content

Commit 45e540c

Browse files
authored
new tykind::error convention (#715)
1 parent 6c580ad commit 45e540c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/ty.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,9 @@ There is a `TyKind::Error` that is produced when the user makes a type error. Th
280280
we would propagate this type and suppress other errors that come up due to it so as not to overwhelm
281281
the user with cascading compiler error messages.
282282

283-
There is an **important invariant** for `TyKind::Error`. You should **never** return the 'error
284-
type' unless you **know** that an error has already been reported to the user. This is usually
283+
There is an **important invariant** for `TyKind::Error`. The compiler should
284+
**never** produce `Error` unless we **know** that an error has already been
285+
reported to the user. This is usually
285286
because (a) you just reported it right there or (b) you are propagating an existing Error type (in
286287
which case the error should've been reported when that error type was produced).
287288

@@ -297,6 +298,19 @@ compilation should succeed, then it will trigger a compiler bug report.
297298

298299
[`delay_span_bug`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/struct.Session.html#method.delay_span_bug
299300

301+
For added safety, it's not actually possible to produce a `TyKind::Error` value
302+
outside of [`rustc_middle::ty`][ty]; there is a private member of
303+
`TyKind::Error` that prevents it from being constructable elsewhere. Instead,
304+
one should use the [`TyCtxt::ty_error`][terr] or
305+
[`TyCtxt::ty_error_with_message`][terrmsg] methods. These methods automatically
306+
call `delay_span_bug` before returning an interned `Ty` of kind `Error`. If you
307+
were already planning to use [`delay_span_bug`], then you can just pass the
308+
span and message to [`ty_error_with_message`][terrmsg] instead to avoid
309+
delaying a redundant span bug.
310+
311+
[terr]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.ty_error
312+
[terrmsg]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.ty_error_with_message
313+
300314
## Question: Why not substitute “inside” the `AdtDef`?
301315

302316
Recall that we represent a generic struct with `(AdtDef, substs)`. So why bother with this scheme?

0 commit comments

Comments
 (0)