]> git.lizzy.rs Git - rust.git/commitdiff
Revert "Auto merge of #84797 - richkadel:cover-unreachable-statements, r=tmandry"
authorRich Kadel <richkadel@google.com>
Tue, 11 May 2021 19:47:08 +0000 (12:47 -0700)
committerRich Kadel <richkadel@google.com>
Tue, 11 May 2021 19:47:08 +0000 (12:47 -0700)
This reverts commit e5f83d24aee866a14753a7cedbb4e301dfe5bef5, reversing
changes made to ac888e8675182c703c2cd097957878faf88dad94.

21 files changed:
compiler/rustc_mir/src/transform/const_goto.rs
compiler/rustc_mir/src/transform/deduplicate_blocks.rs
compiler/rustc_mir/src/transform/early_otherwise_branch.rs
compiler/rustc_mir/src/transform/generator.rs
compiler/rustc_mir/src/transform/inline.rs
compiler/rustc_mir/src/transform/match_branches.rs
compiler/rustc_mir/src/transform/multiple_return_terminators.rs
compiler/rustc_mir/src/transform/remove_unneeded_drops.rs
compiler/rustc_mir/src/transform/simplify.rs
compiler/rustc_mir/src/transform/simplify_try.rs
compiler/rustc_mir/src/transform/unreachable_prop.rs
src/test/run-make-fulldeps/coverage-reports/expected_show_coverage.async2.txt
src/test/run-make-fulldeps/coverage-reports/expected_show_coverage.conditions.txt
src/test/run-make-fulldeps/coverage-reports/expected_show_coverage.doctest.txt
src/test/run-make-fulldeps/coverage-reports/expected_show_coverage.drop_trait.txt
src/test/run-make-fulldeps/coverage-reports/expected_show_coverage.generics.txt
src/test/run-make-fulldeps/coverage-reports/expected_show_coverage.loops_branches.txt
src/test/run-make-fulldeps/coverage-reports/expected_show_coverage.tight_inf_loop.txt
src/test/run-make-fulldeps/coverage/conditions.rs
src/test/run-make-fulldeps/coverage/generics.rs
src/test/run-make-fulldeps/coverage/loops_branches.rs

index ba10b54c5ae2e37fa579dc694bcfb35a79d50e1f..b5c8b4bebc360496349001822c0dc24c1bece0f2 100644 (file)
@@ -47,7 +47,7 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
         // if we applied optimizations, we potentially have some cfg to cleanup to
         // make it easier for further passes
         if should_simplify {
-            simplify_cfg(tcx, body);
+            simplify_cfg(body);
             simplify_locals(body, tcx);
         }
     }
index 912505c65983edb8f06df2ef6fe0016035ef90db..c41e71e09a4efc7d64186850460538b2b254d0cb 100644 (file)
@@ -26,7 +26,7 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
         if has_opts_to_apply {
             let mut opt_applier = OptApplier { tcx, duplicates };
             opt_applier.visit_body(body);
-            simplify_cfg(tcx, body);
+            simplify_cfg(body);
         }
     }
 }
index ac392066233089914105843a2e224df382ad3761..f7ea9faec47283cf2903062fedd031d0f9c4a440 100644 (file)
@@ -164,7 +164,7 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
         // Since this optimization adds new basic blocks and invalidates others,
         // clean up the cfg to make it nicer for other passes
         if should_cleanup {
-            simplify_cfg(tcx, body);
+            simplify_cfg(body);
         }
     }
 }
index 3560b4b1e8645e77ed17488ac8ba954b9ea4c28c..003003a8abbeae90f8f736d89e8a3a504e271de8 100644 (file)
@@ -964,7 +964,7 @@ fn create_generator_drop_shim<'tcx>(
 
     // Make sure we remove dead blocks to remove
     // unrelated code from the resume part of the function
-    simplify::remove_dead_blocks(tcx, &mut body);
+    simplify::remove_dead_blocks(&mut body);
 
     dump_mir(tcx, None, "generator_drop", &0, &body, |_, _| Ok(()));
 
@@ -1137,7 +1137,7 @@ fn create_generator_resume_function<'tcx>(
 
     // Make sure we remove dead blocks to remove
     // unrelated code from the drop part of the function
-    simplify::remove_dead_blocks(tcx, body);
+    simplify::remove_dead_blocks(body);
 
     dump_mir(tcx, None, "generator_resume", &0, body, |_, _| Ok(()));
 }
index f1c95a84ade85a04800a9af36440ba28429fbe6d..b6f80763bc8c4c2b94b445b999a0d1bb85c2f3bb 100644 (file)
@@ -57,7 +57,7 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
         if inline(tcx, body) {
             debug!("running simplify cfg on {:?}", body.source);
             CfgSimplifier::new(body).simplify();
-            remove_dead_blocks(tcx, body);
+            remove_dead_blocks(body);
         }
     }
 }
index 21b208a08c2dca13cad206c4c28bf986187f3e43..f7a9835353e5cff77c14c762641f2bf99103e088 100644 (file)
@@ -167,7 +167,7 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
         }
 
         if should_cleanup {
-            simplify_cfg(tcx, body);
+            simplify_cfg(body);
         }
     }
 }
index cd2db180552868c681f5bf862da1b988b43f9375..4aaa0baa9f46a26fb640f59b73e5f322eb84c6db 100644 (file)
@@ -38,6 +38,6 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
             }
         }
 
-        simplify::remove_dead_blocks(tcx, body)
+        simplify::remove_dead_blocks(body)
     }
 }
index 02e45021a0aaf343fe3f73173934dbbead3e9f0d..5144d48750de70d7febebd7a0c6dcc013c157781 100644 (file)
@@ -36,7 +36,7 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
         // if we applied optimizations, we potentially have some cfg to cleanup to
         // make it easier for further passes
         if should_simplify {
-            simplify_cfg(tcx, body);
+            simplify_cfg(body);
         }
     }
 }
index 63373b0cffbb033c6f2f11ed4aceddd1194d6299..65e2d096b209462dd496320f589244ea13d12241 100644 (file)
@@ -29,7 +29,6 @@
 
 use crate::transform::MirPass;
 use rustc_index::vec::{Idx, IndexVec};
-use rustc_middle::mir::coverage::*;
 use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
 use rustc_middle::mir::*;
 use rustc_middle::ty::TyCtxt;
@@ -47,9 +46,9 @@ pub fn new(label: &str) -> Self {
     }
 }
 
-pub fn simplify_cfg(tcx: TyCtxt<'tcx>, body: &mut Body<'_>) {
+pub fn simplify_cfg(body: &mut Body<'_>) {
     CfgSimplifier::new(body).simplify();
-    remove_dead_blocks(tcx, body);
+    remove_dead_blocks(body);
 
     // FIXME: Should probably be moved into some kind of pass manager
     body.basic_blocks_mut().raw.shrink_to_fit();
@@ -60,9 +59,9 @@ fn name(&self) -> Cow<'_, str> {
         Cow::Borrowed(&self.label)
     }
 
-    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
+    fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
         debug!("SimplifyCfg({:?}) - simplifying {:?}", self.label, body.source);
-        simplify_cfg(tcx, body);
+        simplify_cfg(body);
     }
 }
 
@@ -287,7 +286,7 @@ fn strip_nops(&mut self) {
     }
 }
 
-pub fn remove_dead_blocks(tcx: TyCtxt<'tcx>, body: &mut Body<'_>) {
+pub fn remove_dead_blocks(body: &mut Body<'_>) {
     let reachable = traversal::reachable_as_bitset(body);
     let num_blocks = body.basic_blocks().len();
     if num_blocks == reachable.count() {
@@ -307,11 +306,6 @@ pub fn remove_dead_blocks(tcx: TyCtxt<'tcx>, body: &mut Body<'_>) {
         }
         used_blocks += 1;
     }
-
-    if tcx.sess.instrument_coverage() {
-        save_unreachable_coverage(basic_blocks, used_blocks);
-    }
-
     basic_blocks.raw.truncate(used_blocks);
 
     for block in basic_blocks {
@@ -321,32 +315,6 @@ pub fn remove_dead_blocks(tcx: TyCtxt<'tcx>, body: &mut Body<'_>) {
     }
 }
 
-fn save_unreachable_coverage(
-    basic_blocks: &mut IndexVec<BasicBlock, BasicBlockData<'_>>,
-    first_dead_block: usize,
-) {
-    // retain coverage info for dead blocks, so coverage reports will still
-    // report `0` executions for the uncovered code regions.
-    let mut dropped_coverage = Vec::new();
-    for dead_block in first_dead_block..basic_blocks.len() {
-        for statement in basic_blocks[BasicBlock::new(dead_block)].statements.iter() {
-            if let StatementKind::Coverage(coverage) = &statement.kind {
-                if let Some(code_region) = &coverage.code_region {
-                    dropped_coverage.push((statement.source_info, code_region.clone()));
-                }
-            }
-        }
-    }
-    for (source_info, code_region) in dropped_coverage {
-        basic_blocks[START_BLOCK].statements.push(Statement {
-            source_info,
-            kind: StatementKind::Coverage(box Coverage {
-                kind: CoverageKind::Unreachable,
-                code_region: Some(code_region),
-            }),
-        })
-    }
-}
 pub struct SimplifyLocals;
 
 impl<'tcx> MirPass<'tcx> for SimplifyLocals {
index c9c4492062720199bf7d5ed0c63b73d892e7b2ef..b42543c04eb3dd4d0dec4d2f1af86b94f14e5132 100644 (file)
@@ -558,7 +558,7 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
 
         if did_remove_blocks {
             // We have dead blocks now, so remove those.
-            simplify::remove_dead_blocks(tcx, body);
+            simplify::remove_dead_blocks(body);
         }
     }
 }
index e7fb6b4f6b4ade858cf5210480c5a4aece8647d9..658c6b6e9db20086855c89e94b73799352195b87 100644 (file)
@@ -60,7 +60,7 @@ fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
         }
 
         if replaced {
-            simplify::remove_dead_blocks(tcx, body);
+            simplify::remove_dead_blocks(body);
         }
     }
 }
index dc06a485a8fc124bd26a7779b81c9bd0ebd1dbef..322f5681b3fd9278243359047748a891ca46fe21 100644 (file)
@@ -12,7 +12,6 @@
    12|      1|    if b {
    13|      1|        println!("non_async_func println in block");
    14|      1|    }
-                   ^0
    15|      1|}
    16|       |
    17|       |
index 2d8a98a5d0c92f6a19ebc1c7b3fa63a63515f57c..656a26597759d337bac841ad5a4bfa956cac3a9b 100644 (file)
@@ -5,7 +5,6 @@
     5|      1|    if true {
     6|      1|        countdown = 10;
     7|      1|    }
-                   ^0
     8|       |
     9|       |    const B: u32 = 100;
    10|      1|    let x = if countdown > 7 {
@@ -25,7 +24,6 @@
    24|      1|    if true {
    25|      1|        countdown = 10;
    26|      1|    }
-                   ^0
    27|       |
    28|      1|    if countdown > 7 {
    29|      1|        countdown -= 4;
@@ -44,7 +42,6 @@
    41|      1|        if true {
    42|      1|            countdown = 10;
    43|      1|        }
-                       ^0
    44|       |
    45|      1|        if countdown > 7 {
    46|      1|            countdown -= 4;
    53|       |        } else {
    54|      0|            return;
    55|       |        }
-   56|      0|    }
-   57|       |
+   56|       |    } // Note: closing brace shows uncovered (vs. `0` for implicit else) because condition literal
+   57|       |      // `true` was const-evaluated. The compiler knows the `if` block will be executed.
    58|       |
    59|      1|    let mut countdown = 0;
    60|      1|    if true {
    61|      1|        countdown = 1;
    62|      1|    }
-                   ^0
    63|       |
    64|      1|    let z = if countdown > 7 {
                       ^0
index 7ae0e978808e7e7c717422a44ed141ae1e43b37d..1b6bb9ff8891de6c5ad657868580e8d31afe28ff 100644 (file)
@@ -9,7 +9,7 @@
     8|      1|//!     assert_eq!(1, 1);
     9|       |//! } else {
    10|       |//!     // this is not!
-   11|      0|//!     assert_eq!(1, 2);
+   11|       |//!     assert_eq!(1, 2);
    12|       |//! }
    13|      1|//! ```
    14|       |//!
@@ -84,7 +84,7 @@
    74|      1|    if true {
    75|      1|        assert_eq!(1, 1);
    76|       |    } else {
-   77|      0|        assert_eq!(1, 2);
+   77|       |        assert_eq!(1, 2);
    78|       |    }
    79|      1|}
    80|       |
index fe6a9e93cbf710ab516bdc1e2b283ed7ffae99eb..fab5be41901c9b544ba12e3379b30cb96d331177 100644 (file)
    19|      1|    if true {
    20|      1|        println!("Exiting with error...");
    21|      1|        return Err(1);
-   22|      0|    }
-   23|      0|
-   24|      0|    let _ = Firework { strength: 1000 };
-   25|      0|
-   26|      0|    Ok(())
+   22|       |    }
+   23|       |
+   24|       |    let _ = Firework { strength: 1000 };
+   25|       |
+   26|       |    Ok(())
    27|      1|}
    28|       |
    29|       |// Expected program output:
index 8e8bc0fd1894338f3f7b0579ee35ca6c02baf744..7b38ffb87cba89b257125b6585d09100014763df 100644 (file)
    30|      1|    if true {
    31|      1|        println!("Exiting with error...");
    32|      1|        return Err(1);
-   33|      0|    }
-   34|      0|
-   35|      0|
-   36|      0|
-   37|      0|
-   38|      0|
-   39|      0|    let _ = Firework { strength: 1000 };
-   40|      0|
-   41|      0|    Ok(())
+   33|       |    } // The remaining lines below have no coverage because `if true` (with the constant literal
+   34|       |      // `true`) is guaranteed to execute the `then` block, which is also guaranteed to `return`.
+   35|       |      // Thankfully, in the normal case, conditions are not guaranteed ahead of time, and as shown
+   36|       |      // in other tests, the lines below would have coverage (which would show they had `0`
+   37|       |      // executions, assuming the condition still evaluated to `true`).
+   38|       |
+   39|       |    let _ = Firework { strength: 1000 };
+   40|       |
+   41|       |    Ok(())
    42|      1|}
    43|       |
    44|       |// Expected program output:
index 5d572db7cc60da3b6b90c2a9dd799c7eda9143ed..81d5c7d90346d08a397e577d120352dbe465f124 100644 (file)
@@ -9,23 +9,23 @@
     9|      1|    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    10|      1|        if true {
    11|      1|            if false {
-   12|      0|                while true {
-   13|      0|                }
+   12|       |                while true {
+   13|       |                }
    14|      1|            }
-   15|      1|            write!(f, "cool")?;
-                                           ^0
-   16|      0|        } else {
-   17|      0|        }
+   15|      1|            write!(f, "error")?;
+                                            ^0
+   16|       |        } else {
+   17|       |        }
    18|       |
    19|     10|        for i in 0..10 {
    20|     10|            if true {
    21|     10|                if false {
-   22|      0|                    while true {}
+   22|       |                    while true {}
    23|     10|                }
-   24|     10|                write!(f, "cool")?;
-                                               ^0
-   25|      0|            } else {
-   26|      0|            }
+   24|     10|                write!(f, "error")?;
+                                                ^0
+   25|       |            } else {
+   26|       |            }
    27|       |        }
    28|      1|        Ok(())
    29|      1|    }
    34|       |impl std::fmt::Display for DisplayTest {
    35|      1|    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    36|      1|        if false {
-   37|      0|        } else {
+   37|       |        } else {
    38|      1|            if false {
-   39|      0|                while true {}
+   39|       |                while true {}
    40|      1|            }
-   41|      1|            write!(f, "cool")?;
-                                           ^0
+   41|      1|            write!(f, "error")?;
+                                            ^0
    42|       |        }
    43|     10|        for i in 0..10 {
    44|     10|            if false {
-   45|      0|            } else {
+   45|       |            } else {
    46|     10|                if false {
-   47|      0|                    while true {}
+   47|       |                    while true {}
    48|     10|                }
-   49|     10|                write!(f, "cool")?;
-                                               ^0
+   49|     10|                write!(f, "error")?;
+                                                ^0
    50|       |            }
    51|       |        }
    52|      1|        Ok(())
index 2d4c57f451a2de3871bb0223d6cfbd72b71d2dac..5adeef7d0850b462b369da348651e695db38307b 100644 (file)
@@ -1,6 +1,6 @@
     1|      1|fn main() {
     2|      1|    if false {
-    3|      0|        loop {}
+    3|       |        loop {}
     4|      1|    }
     5|      1|}
 
index 057599d1b471ab2e12a63f6a4a0b5b2fc9a3f654..8a2a0b53e5862f3031364e1e340834aedfbefda8 100644 (file)
@@ -53,8 +53,8 @@ fn main() {
         } else {
             return;
         }
-    }
-
+    } // Note: closing brace shows uncovered (vs. `0` for implicit else) because condition literal
+      // `true` was const-evaluated. The compiler knows the `if` block will be executed.
 
     let mut countdown = 0;
     if true {
index 18b38868496d4bc10a699e6fe68035adaf2690e2..cbeda35d3b8cfe8a5b226f769282d568f9689012 100644 (file)
@@ -30,11 +30,11 @@ fn main() -> Result<(),u8> {
     if true {
         println!("Exiting with error...");
         return Err(1);
-    }
-
-
-
-
+    } // The remaining lines below have no coverage because `if true` (with the constant literal
+      // `true`) is guaranteed to execute the `then` block, which is also guaranteed to `return`.
+      // Thankfully, in the normal case, conditions are not guaranteed ahead of time, and as shown
+      // in other tests, the lines below would have coverage (which would show they had `0`
+      // executions, assuming the condition still evaluated to `true`).
 
     let _ = Firework { strength: 1000 };
 
index 7116ce47f4b9dcdf28bbd8fd161509f0e74faa6a..4d9bbad3367f6ef50c9d072327ab996ec5d62cb9 100644 (file)
@@ -12,7 +12,7 @@ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                 while true {
                 }
             }
-            write!(f, "cool")?;
+            write!(f, "error")?;
         } else {
         }
 
@@ -21,7 +21,7 @@ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                 if false {
                     while true {}
                 }
-                write!(f, "cool")?;
+                write!(f, "error")?;
             } else {
             }
         }
@@ -38,7 +38,7 @@ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
             if false {
                 while true {}
             }
-            write!(f, "cool")?;
+            write!(f, "error")?;
         }
         for i in 0..10 {
             if false {
@@ -46,7 +46,7 @@ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                 if false {
                     while true {}
                 }
-                write!(f, "cool")?;
+                write!(f, "error")?;
             }
         }
         Ok(())