@@ -427,15 +427,12 @@ pub struct LateContext<'a, 'tcx> {
427427 /// Current body, or `None` if outside a body.
428428 pub enclosing_body : Option < hir:: BodyId > ,
429429
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.
432432 // FIXME(eddyb) move all the code accessing internal fields like this,
433433 // to this module, to avoid exposing it to lint logic.
434434 pub ( super ) cached_typeck_tables : Cell < Option < & ' tcx ty:: TypeckTables < ' tcx > > > ,
435435
436- // HACK(eddyb) replace this with having `Option` around `&TypeckTables`.
437- pub ( super ) empty_typeck_tables : & ' a ty:: TypeckTables < ' tcx > ,
438-
439436 /// Parameter environment for the item we are in.
440437 pub param_env : ty:: ParamEnv < ' tcx > ,
441438
@@ -677,19 +674,23 @@ impl LintContext for EarlyContext<'_> {
677674
678675impl < ' a , ' tcx > LateContext < ' a , ' tcx > {
679676 /// 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| {
686681 let tables = self . tcx . body_tables ( body) ;
687682 self . cached_typeck_tables . set ( Some ( tables) ) ;
688683 tables
689684 } )
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" )
693694 }
694695
695696 pub fn current_lint_root ( & self ) -> hir:: HirId {
0 commit comments