From 28ef141e19ee1acf69b2e4dbc51d5be71b78da50 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Sun, 8 Sep 2019 21:19:47 +0200 Subject: [PATCH] Always validate HIR ID for TypeckTables Performance shouldn't be impacted (see [1] for a perf run) and this should allow us to catch more bugs, e.g. [2] and [3]. [1]: https://github.com/rust-lang/rust/pull/64262 [2]: https://github.com/rust-lang/rust/pull/64250 [3]: https://github.com/rust-lang/rust/issues/57298 --- src/librustc/ty/context.rs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 17c9e520bcea2..32c4377e0eb27 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -207,26 +207,24 @@ pub struct LocalTableInContext<'a, V> { fn validate_hir_id_for_typeck_tables(local_id_root: Option, hir_id: hir::HirId, mut_access: bool) { - if cfg!(debug_assertions) { - if let Some(local_id_root) = local_id_root { - if hir_id.owner != local_id_root.index { - ty::tls::with(|tcx| { - bug!("node {} with HirId::owner {:?} cannot be placed in \ - TypeckTables with local_id_root {:?}", - tcx.hir().node_to_string(hir_id), - DefId::local(hir_id.owner), - local_id_root) - }); - } - } else { - // We use "Null Object" TypeckTables in some of the analysis passes. - // These are just expected to be empty and their `local_id_root` is - // `None`. Therefore we cannot verify whether a given `HirId` would - // be a valid key for the given table. Instead we make sure that - // nobody tries to write to such a Null Object table. - if mut_access { - bug!("access to invalid TypeckTables") - } + if let Some(local_id_root) = local_id_root { + if hir_id.owner != local_id_root.index { + ty::tls::with(|tcx| { + bug!("node {} with HirId::owner {:?} cannot be placed in \ + TypeckTables with local_id_root {:?}", + tcx.hir().node_to_string(hir_id), + DefId::local(hir_id.owner), + local_id_root) + }); + } + } else { + // We use "Null Object" TypeckTables in some of the analysis passes. + // These are just expected to be empty and their `local_id_root` is + // `None`. Therefore we cannot verify whether a given `HirId` would + // be a valid key for the given table. Instead we make sure that + // nobody tries to write to such a Null Object table. + if mut_access { + bug!("access to invalid TypeckTables") } } }