Skip to content

Commit 4af3e84

Browse files
committed
rustc_lint: avoid using TypeckTables::empty for LateContext.
1 parent a610b7f commit 4af3e84

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

src/librustc_lint/context.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

678675
impl<'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 {

src/librustc_lint/late.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
378378
tcx,
379379
enclosing_body: None,
380380
cached_typeck_tables: Cell::new(None),
381-
empty_typeck_tables: &ty::TypeckTables::empty(None),
382381
param_env: ty::ParamEnv::empty(),
383382
access_levels,
384383
lint_store: unerased_lint_store(tcx),
@@ -427,7 +426,6 @@ fn late_lint_pass_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tc
427426
tcx,
428427
enclosing_body: None,
429428
cached_typeck_tables: Cell::new(None),
430-
empty_typeck_tables: &ty::TypeckTables::empty(None),
431429
param_env: ty::ParamEnv::empty(),
432430
access_levels,
433431
lint_store: unerased_lint_store(tcx),

src/librustc_lint/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#![feature(never_type)]
3535
#![feature(nll)]
3636
#![feature(or_patterns)]
37+
#![feature(track_caller)]
3738
#![recursion_limit = "256"]
3839

3940
#[macro_use]

src/tools/clippy/clippy_lints/src/map_unit_fn.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ fn reduce_unit_expression<'a>(cx: &LateContext<'_, '_>, expr: &'a hir::Expr<'_>)
160160
}
161161
}
162162

163-
fn unit_closure<'a, 'tcx>(
164-
cx: &LateContext<'a, 'tcx>,
165-
expr: &'a hir::Expr<'a>,
166-
) -> Option<(&'tcx hir::Param<'tcx>, &'a hir::Expr<'a>)> {
163+
fn unit_closure<'tcx>(
164+
cx: &LateContext<'_, 'tcx>,
165+
expr: &hir::Expr<'_>,
166+
) -> Option<(&'tcx hir::Param<'tcx>, &'tcx hir::Expr<'tcx>)> {
167167
if let hir::ExprKind::Closure(_, ref decl, inner_expr_id, _, _) = expr.kind {
168168
let body = cx.tcx.hir().body(inner_expr_id);
169169
let body_expr = &body.value;

0 commit comments

Comments
 (0)