@@ -280,8 +280,9 @@ There is a `TyKind::Error` that is produced when the user makes a type error. Th
280
280
we would propagate this type and suppress other errors that come up due to it so as not to overwhelm
281
281
the user with cascading compiler error messages.
282
282
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
285
286
because (a) you just reported it right there or (b) you are propagating an existing Error type (in
286
287
which case the error should've been reported when that error type was produced).
287
288
@@ -297,6 +298,19 @@ compilation should succeed, then it will trigger a compiler bug report.
297
298
298
299
[ `delay_span_bug` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/struct.Session.html#method.delay_span_bug
299
300
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
+
300
314
## Question: Why not substitute “inside” the ` AdtDef ` ?
301
315
302
316
Recall that we represent a generic struct with ` (AdtDef, substs) ` . So why bother with this scheme?
0 commit comments