Use par_body_owners for liveness

This commit is contained in:
Seo Sanghyeon 2022-08-23 20:53:02 +09:00
parent 8818b00b63
commit f280138c7c
8 changed files with 137 additions and 146 deletions

View File

@ -911,13 +911,13 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
});
},
{
sess.time("liveness_and_intrinsic_checking", || {
tcx.hir().par_for_each_module(|module| {
sess.time("liveness_checking", || {
tcx.hir().par_body_owners(|def_id| {
// this must run before MIR dump, because
// "not all control paths return a value" is reported here.
//
// maybe move the check to a MIR pass?
tcx.ensure().check_mod_liveness(module);
tcx.ensure().check_liveness(def_id.to_def_id());
});
});
}

View File

@ -818,8 +818,8 @@ rustc_queries! {
desc { |tcx| "checking privacy in {}", describe_as_module(key, tcx) }
}
query check_mod_liveness(key: LocalDefId) -> () {
desc { |tcx| "checking liveness of variables in {}", describe_as_module(key, tcx) }
query check_liveness(key: DefId) {
desc { |tcx| "checking liveness of variables in {}", tcx.def_path_str(key) }
}
/// Return the live symbols in the crate for dead code check.

View File

@ -89,11 +89,10 @@ use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def::*;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{Expr, HirId, HirIdMap, HirIdSet};
use rustc_index::vec::IndexVec;
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, DefIdTree, RootVariableMinCaptureList, Ty, TyCtxt};
use rustc_session::lint;
@ -139,12 +138,54 @@ fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_>) -> String {
}
}
fn check_mod_liveness(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
tcx.hir().visit_item_likes_in_module(module_def_id, &mut IrMaps::new(tcx));
fn check_liveness(tcx: TyCtxt<'_>, def_id: DefId) {
let local_def_id = match def_id.as_local() {
None => return,
Some(def_id) => def_id,
};
// Don't run unused pass for #[derive()]
let parent = tcx.local_parent(local_def_id);
if let DefKind::Impl = tcx.def_kind(parent)
&& tcx.has_attr(parent.to_def_id(), sym::automatically_derived)
{
return;
}
// Don't run unused pass for #[naked]
if tcx.has_attr(def_id, sym::naked) {
return;
}
let mut maps = IrMaps::new(tcx);
let body_id = tcx.hir().body_owned_by(local_def_id);
let hir_id = tcx.hir().body_owner(body_id);
let body = tcx.hir().body(body_id);
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
for &var_hir_id in upvars.keys() {
let var_name = tcx.hir().name(var_hir_id);
maps.add_variable(Upvar(var_hir_id, var_name));
}
}
// gather up the various local variables, significant expressions,
// and so forth:
maps.visit_body(body);
// compute liveness
let mut lsets = Liveness::new(&mut maps, local_def_id);
let entry_ln = lsets.compute(&body, hir_id);
lsets.log_liveness(entry_ln, body_id.hir_id);
// check for various error conditions
lsets.visit_body(body);
lsets.warn_about_unused_upvars(entry_ln);
lsets.warn_about_unused_args(body, entry_ln);
}
pub fn provide(providers: &mut Providers) {
*providers = Providers { check_mod_liveness, ..*providers };
*providers = Providers { check_liveness, ..*providers };
}
// ______________________________________________________________________
@ -316,56 +357,6 @@ impl<'tcx> IrMaps<'tcx> {
}
impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
type NestedFilter = nested_filter::OnlyBodies;
fn nested_visit_map(&mut self) -> Self::Map {
self.tcx.hir()
}
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
debug!("visit_body {:?}", body.id());
// swap in a new set of IR maps for this body
let mut maps = IrMaps::new(self.tcx);
let hir_id = maps.tcx.hir().body_owner(body.id());
let local_def_id = maps.tcx.hir().local_def_id(hir_id);
let def_id = local_def_id.to_def_id();
// Don't run unused pass for #[derive()]
let parent = self.tcx.local_parent(local_def_id);
if let DefKind::Impl = self.tcx.def_kind(parent)
&& self.tcx.has_attr(parent.to_def_id(), sym::automatically_derived)
{
return;
}
// Don't run unused pass for #[naked]
if self.tcx.has_attr(def_id, sym::naked) {
return;
}
if let Some(upvars) = maps.tcx.upvars_mentioned(def_id) {
for &var_hir_id in upvars.keys() {
let var_name = maps.tcx.hir().name(var_hir_id);
maps.add_variable(Upvar(var_hir_id, var_name));
}
}
// gather up the various local variables, significant expressions,
// and so forth:
intravisit::walk_body(&mut maps, body);
// compute liveness
let mut lsets = Liveness::new(&mut maps, local_def_id);
let entry_ln = lsets.compute(&body, hir_id);
lsets.log_liveness(entry_ln, body.id().hir_id);
// check for various error conditions
lsets.visit_body(body);
lsets.warn_about_unused_upvars(entry_ln);
lsets.warn_about_unused_args(body, entry_ln);
}
fn visit_local(&mut self, local: &'tcx hir::Local<'tcx>) {
self.add_from_pat(&local.pat);
if local.els.is_some() {

View File

@ -1,8 +1,8 @@
warning: unused variable: `t2`
--> $DIR/destructure-pattern-closure-within-closure.rs:13:21
warning: unused variable: `g2`
--> $DIR/destructure-pattern-closure-within-closure.rs:10:17
|
LL | let (_, t2) = t;
| ^^ help: if this is intentional, prefix it with an underscore: `_t2`
LL | let (_, g2) = g;
| ^^ help: if this is intentional, prefix it with an underscore: `_g2`
|
note: the lint level is defined here
--> $DIR/destructure-pattern-closure-within-closure.rs:3:9
@ -11,11 +11,11 @@ LL | #![warn(unused)]
| ^^^^^^
= note: `#[warn(unused_variables)]` implied by `#[warn(unused)]`
warning: unused variable: `g2`
--> $DIR/destructure-pattern-closure-within-closure.rs:10:17
warning: unused variable: `t2`
--> $DIR/destructure-pattern-closure-within-closure.rs:13:21
|
LL | let (_, g2) = g;
| ^^ help: if this is intentional, prefix it with an underscore: `_g2`
LL | let (_, t2) = t;
| ^^ help: if this is intentional, prefix it with an underscore: `_t2`
warning: 2 warnings emitted

View File

@ -11,12 +11,6 @@ LL | #![deny(unused)]
| ^^^^^^
= note: `#[deny(unused_variables)]` implied by `#[deny(unused)]`
error: unused variable: `x`
--> $DIR/issue-54180-unused-ref-field.rs:29:45
|
LL | let _: i32 = points.iter().map(|Point { x, y }| y).sum();
| ^ help: try ignoring the field: `x: _`
error: unused variable: `f1`
--> $DIR/issue-54180-unused-ref-field.rs:26:13
|
@ -29,5 +23,11 @@ error: unused variable: `x`
LL | Point { y, ref mut x } => y,
| ^^^^^^^^^ help: try ignoring the field: `x: _`
error: unused variable: `x`
--> $DIR/issue-54180-unused-ref-field.rs:29:45
|
LL | let _: i32 = points.iter().map(|Point { x, y }| y).sum();
| ^ help: try ignoring the field: `x: _`
error: aborting due to 4 previous errors

View File

@ -16,30 +16,6 @@ error: unused variable: `b`
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `a`
--> $DIR/lint-unused-variables.rs:68:9
|
LL | a: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_a`
error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:74:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:42:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:47:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `a`
--> $DIR/lint-unused-variables.rs:22:9
|
@ -58,6 +34,18 @@ error: unused variable: `b`
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:42:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:47:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:55:9
|
@ -70,5 +58,17 @@ error: unused variable: `b`
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `a`
--> $DIR/lint-unused-variables.rs:68:9
|
LL | a: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_a`
error: unused variable: `b`
--> $DIR/lint-unused-variables.rs:74:9
|
LL | b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: aborting due to 11 previous errors

View File

@ -39,12 +39,6 @@ warning: unused variable: `z`
LL | pub fn f(x: [u8; { let s = 17; 100 }]) -> [u8; { let z = 18; 100 }] {
| ^ help: if this is intentional, prefix it with an underscore: `_z`
warning: unused variable: `z`
--> $DIR/liveness-consts.rs:60:13
|
LL | let z = 42;
| ^ help: if this is intentional, prefix it with an underscore: `_z`
warning: value assigned to `t` is never read
--> $DIR/liveness-consts.rs:42:9
|
@ -59,5 +53,11 @@ warning: unused variable: `w`
LL | let w = 10;
| ^ help: if this is intentional, prefix it with an underscore: `_w`
warning: unused variable: `z`
--> $DIR/liveness-consts.rs:60:13
|
LL | let z = 42;
| ^ help: if this is intentional, prefix it with an underscore: `_z`
warning: 8 warnings emitted

View File

@ -22,48 +22,6 @@ error: unused variable: `c`
LL | #[cfg_attr(nothing, cfg(nothing))] c: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_c`
error: unused variable: `a`
--> $DIR/param-attrs-cfg.rs:107:27
|
LL | #[cfg(something)] a: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_a`
error: unused variable: `b`
--> $DIR/param-attrs-cfg.rs:113:27
|
LL | #[cfg(something)] b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `c`
--> $DIR/param-attrs-cfg.rs:115:44
|
LL | #[cfg_attr(nothing, cfg(nothing))] c: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_c`
error: unused variable: `b`
--> $DIR/param-attrs-cfg.rs:67:27
|
LL | #[cfg(something)] b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `c`
--> $DIR/param-attrs-cfg.rs:69:44
|
LL | #[cfg_attr(nothing, cfg(nothing))] c: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_c`
error: unused variable: `b`
--> $DIR/param-attrs-cfg.rs:75:27
|
LL | #[cfg(something)] b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `c`
--> $DIR/param-attrs-cfg.rs:77:44
|
LL | #[cfg_attr(nothing, cfg(nothing))] c: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_c`
error: unused variable: `a`
--> $DIR/param-attrs-cfg.rs:41:27
|
@ -94,6 +52,30 @@ error: unused variable: `c`
LL | #[cfg_attr(nothing, cfg(nothing))] c: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_c`
error: unused variable: `b`
--> $DIR/param-attrs-cfg.rs:67:27
|
LL | #[cfg(something)] b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `c`
--> $DIR/param-attrs-cfg.rs:69:44
|
LL | #[cfg_attr(nothing, cfg(nothing))] c: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_c`
error: unused variable: `b`
--> $DIR/param-attrs-cfg.rs:75:27
|
LL | #[cfg(something)] b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `c`
--> $DIR/param-attrs-cfg.rs:77:44
|
LL | #[cfg_attr(nothing, cfg(nothing))] c: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_c`
error: unused variable: `b`
--> $DIR/param-attrs-cfg.rs:86:27
|
@ -118,5 +100,23 @@ error: unused variable: `c`
LL | #[cfg_attr(nothing, cfg(nothing))] c: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_c`
error: unused variable: `a`
--> $DIR/param-attrs-cfg.rs:107:27
|
LL | #[cfg(something)] a: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_a`
error: unused variable: `b`
--> $DIR/param-attrs-cfg.rs:113:27
|
LL | #[cfg(something)] b: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_b`
error: unused variable: `c`
--> $DIR/param-attrs-cfg.rs:115:44
|
LL | #[cfg_attr(nothing, cfg(nothing))] c: i32,
| ^ help: if this is intentional, prefix it with an underscore: `_c`
error: aborting due to 19 previous errors