@@ -427,15 +427,12 @@ pub struct LateContext<'a, 'tcx> {
427
427
/// Current body, or `None` if outside a body.
428
428
pub enclosing_body : Option < hir:: BodyId > ,
429
429
430
- /// Type-checking side-tables for the current body. Access using the
431
- /// `tables` method , which handles querying the tables on demand.
430
+ /// Type-checking side-tables for the current body. Access using the `tables`
431
+ /// and `maybe_tables` methods , which handle querying the tables on demand.
432
432
// FIXME(eddyb) move all the code accessing internal fields like this,
433
433
// to this module, to avoid exposing it to lint logic.
434
434
pub ( super ) cached_typeck_tables : Cell < Option < & ' tcx ty:: TypeckTables < ' tcx > > > ,
435
435
436
- // HACK(eddyb) replace this with having `Option` around `&TypeckTables`.
437
- pub ( super ) empty_typeck_tables : & ' a ty:: TypeckTables < ' tcx > ,
438
-
439
436
/// Parameter environment for the item we are in.
440
437
pub param_env : ty:: ParamEnv < ' tcx > ,
441
438
@@ -677,19 +674,23 @@ impl LintContext for EarlyContext<'_> {
677
674
678
675
impl < ' a , ' tcx > LateContext < ' a , ' tcx > {
679
676
/// Gets the type-checking side-tables for the current body,
680
- /// or empty `TypeckTables` if outside a body.
681
- // FIXME(eddyb) return `Option<&'tcx ty::TypeckTables<'tcx>>`,
682
- // where `None` indicates we're outside a body.
683
- pub fn tables ( & self ) -> & ' a ty:: TypeckTables < ' tcx > {
684
- if let Some ( body) = self . enclosing_body {
685
- self . cached_typeck_tables . get ( ) . unwrap_or_else ( || {
677
+ /// or `None` if outside a body.
678
+ pub fn maybe_tables ( & self ) -> Option < & ' tcx ty:: TypeckTables < ' tcx > > {
679
+ self . cached_typeck_tables . get ( ) . or_else ( || {
680
+ self . enclosing_body . map ( |body| {
686
681
let tables = self . tcx . body_tables ( body) ;
687
682
self . cached_typeck_tables . set ( Some ( tables) ) ;
688
683
tables
689
684
} )
690
- } else {
691
- self . empty_typeck_tables
692
- }
685
+ } )
686
+ }
687
+
688
+ /// Gets the type-checking side-tables for the current body.
689
+ /// As this will ICE if called outside bodies, only call when working with
690
+ /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
691
+ #[ track_caller]
692
+ pub fn tables ( & self ) -> & ' tcx ty:: TypeckTables < ' tcx > {
693
+ self . maybe_tables ( ) . expect ( "`LateContext::tables` called outside of body" )
693
694
}
694
695
695
696
pub fn current_lint_root ( & self ) -> hir:: HirId {
0 commit comments