]> git.lizzy.rs Git - rust.git/commitdiff
2229: Consider varaiables mentioned in closure as used
authorAman Arora <me@aman-arora.com>
Mon, 23 Aug 2021 22:47:38 +0000 (18:47 -0400)
committerAman Arora <me@aman-arora.com>
Mon, 23 Aug 2021 22:47:38 +0000 (18:47 -0400)
compiler/rustc_passes/src/liveness.rs
src/test/ui/closures/2229_closure_analysis/issue-87987.rs
src/test/ui/closures/2229_closure_analysis/issue-87987.stderr
src/test/ui/closures/2229_closure_analysis/run_pass/destructure_patterns.rs
src/test/ui/closures/2229_closure_analysis/run_pass/destructure_patterns.stderr

index 2cd780e1b9bbe9d92071e6b64b88c2078ec9d254..87c14d9c395f117d3f440bb6ac4ef0f27ef46385 100644 (file)
@@ -337,8 +337,8 @@ fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
             return;
         }
 
-        if let Some(captures) = maps.tcx.typeck(local_def_id).closure_min_captures.get(&def_id) {
-            for &var_hir_id in captures.keys() {
+        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));
             }
@@ -405,21 +405,14 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
                 // breaks or continues)
                 self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span));
 
-                // Make a live_node for each captured variable, with the span
+                // Make a live_node for each mentioned variable, with the span
                 // being the location that the variable is used.  This results
                 // in better error messages than just pointing at the closure
                 // construction site.
                 let mut call_caps = Vec::new();
                 let closure_def_id = self.tcx.hir().local_def_id(expr.hir_id);
-                if let Some(captures) = self
-                    .tcx
-                    .typeck(closure_def_id)
-                    .closure_min_captures
-                    .get(&closure_def_id.to_def_id())
-                {
-                    // If closure_min_captures is Some, upvars_mentioned must also be Some
-                    let upvars = self.tcx.upvars_mentioned(closure_def_id).unwrap();
-                    call_caps.extend(captures.keys().map(|var_id| {
+                if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) {
+                    call_caps.extend(upvars.keys().map(|var_id| {
                         let upvar = upvars[var_id];
                         let upvar_ln = self.add_live_node(UpvarNode(upvar.span));
                         CaptureInfo { ln: upvar_ln, var_hid: *var_id }
@@ -494,7 +487,6 @@ struct Liveness<'a, 'tcx> {
     ir: &'a mut IrMaps<'tcx>,
     typeck_results: &'a ty::TypeckResults<'tcx>,
     param_env: ty::ParamEnv<'tcx>,
-    upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
     closure_min_captures: Option<&'tcx RootVariableMinCaptureList<'tcx>>,
     successors: IndexVec<LiveNode, Option<LiveNode>>,
     rwu_table: rwu_table::RWUTable,
@@ -518,7 +510,6 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
     fn new(ir: &'a mut IrMaps<'tcx>, body_owner: LocalDefId) -> Liveness<'a, 'tcx> {
         let typeck_results = ir.tcx.typeck(body_owner);
         let param_env = ir.tcx.param_env(body_owner);
-        let upvars = ir.tcx.upvars_mentioned(body_owner);
         let closure_min_captures = typeck_results.closure_min_captures.get(&body_owner.to_def_id());
         let closure_ln = ir.add_live_node(ClosureNode);
         let exit_ln = ir.add_live_node(ExitNode);
@@ -530,7 +521,6 @@ fn new(ir: &'a mut IrMaps<'tcx>, body_owner: LocalDefId) -> Liveness<'a, 'tcx> {
             ir,
             typeck_results,
             param_env,
-            upvars,
             closure_min_captures,
             successors: IndexVec::from_elem_n(None, num_live_nodes),
             rwu_table: rwu_table::RWUTable::new(num_live_nodes, num_vars),
@@ -1234,21 +1224,7 @@ fn access_path(
         acc: u32,
     ) -> LiveNode {
         match path.res {
-            Res::Local(hid) => {
-                let in_upvars = self.upvars.map_or(false, |u| u.contains_key(&hid));
-                let in_captures = self.closure_min_captures.map_or(false, |c| c.contains_key(&hid));
-
-                match (in_upvars, in_captures) {
-                    (false, _) | (true, true) => self.access_var(hir_id, hid, succ, acc, path.span),
-                    (true, false) => {
-                        // This case is possible when with RFC-2229, a wild pattern
-                        // is used within a closure.
-                        // eg: `let _ = x`. The closure doesn't capture x here,
-                        // even though it's mentioned in the closure.
-                        succ
-                    }
-                }
-            }
+            Res::Local(hid) => self.access_var(hir_id, hid, succ, acc, path.span),
             _ => succ,
         }
     }
index 5dc2cb7e71099e24ba380487b5f011f76a7b9a9e..d4f243ee3475f42c8f49829dd2acc741c7733094 100644 (file)
@@ -8,7 +8,7 @@ struct Props {
 
 fn main() {
     // Test 1
-    let props_2 = Props { //~ WARNING: unused variable: `props_2`
+    let props_2 = Props {
         field_1: 1,
         field_2: 1,
     };
index aa7012c3618c2e5038df8b45431cd902eb4c1c93..5828295fae33a77c9850839f04b2ff5c5a50d1c7 100644 (file)
@@ -1,11 +1,3 @@
-warning: unused variable: `props_2`
-  --> $DIR/issue-87987.rs:11:9
-   |
-LL |     let props_2 = Props {
-   |         ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_props_2`
-   |
-   = note: `#[warn(unused_variables)]` on by default
-
 warning: field is never read: `field_1`
   --> $DIR/issue-87987.rs:5:5
    |
@@ -20,5 +12,5 @@ warning: field is never read: `field_2`
 LL |     field_2: u32,
    |     ^^^^^^^^^^^^
 
-warning: 3 warnings emitted
+warning: 2 warnings emitted
 
index 07adbee03f968c71009f644ee0793148e72ff743..dacc2c616b8b6936dedeedabd2d30f5a0946cdd3 100644 (file)
@@ -43,7 +43,6 @@ fn test3() {
 
 fn test4() {
     let t = (String::from("Hello"), String::from("World"));
-    //~^ WARN unused variable: `t`
 
     let c = ||  {
         let (_, _) = t;
@@ -81,9 +80,7 @@ fn test7() {
 
 fn test8() {
     let x = 0;
-    //~^ WARN unused variable: `x`
     let tup = (1, 2);
-    //~^ WARN unused variable: `tup`
     let p = Point { x: 10, y: 20 };
 
     let c = || {
index 6523f2b34d537c076a53d36e20a5b633c06b6878..7706f68ba5b44ba22d74805093db8c0e68877c62 100644 (file)
@@ -29,29 +29,11 @@ warning: unused variable: `t2`
 LL |         let (_, t2) = t;
    |                 ^^ help: if this is intentional, prefix it with an underscore: `_t2`
 
-warning: unused variable: `t`
-  --> $DIR/destructure_patterns.rs:45:9
-   |
-LL |     let t = (String::from("Hello"), String::from("World"));
-   |         ^ help: if this is intentional, prefix it with an underscore: `_t`
-
 warning: unused variable: `x`
-  --> $DIR/destructure_patterns.rs:91:21
+  --> $DIR/destructure_patterns.rs:88:21
    |
 LL |         let Point { x, y } = p;
    |                     ^ help: try ignoring the field: `x: _`
 
-warning: unused variable: `x`
-  --> $DIR/destructure_patterns.rs:83:9
-   |
-LL |     let x = 0;
-   |         ^ help: if this is intentional, prefix it with an underscore: `_x`
-
-warning: unused variable: `tup`
-  --> $DIR/destructure_patterns.rs:85:9
-   |
-LL |     let tup = (1, 2);
-   |         ^^^ help: if this is intentional, prefix it with an underscore: `_tup`
-
-warning: 8 warnings emitted
+warning: 5 warnings emitted