fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1$DIR/auxiliary/used_crate.rs: 2 LL| |#![allow(unused_assignments, unused_variables)] 3 LL| |// Verify that coverage works with optimizations: 4 LL| |// compile-flags: -C opt-level=3 5 LL| | 6 LL| |use std::fmt::Debug; 7 LL| | 8 LL| 1|pub fn used_function() { 9 LL| 1| // Initialize test constants in a way that cannot be determined at compile time, to ensure 10 LL| 1| // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from 11 LL| 1| // dependent conditions. 12 LL| 1| let is_true = std::env::args().len() == 1; 13 LL| 1| let mut countdown = 0; 14 LL| 1| if is_true { 15 LL| 1| countdown = 10; 16 LL| 1| } 17 ^0 18 LL| 1| use_this_lib_crate(); 19 LL| 1|} 20 LL| | 21 LL| 2|pub fn used_only_from_bin_crate_generic_function<T: Debug>(arg: T) { 22 LL| 2| println!("used_only_from_bin_crate_generic_function with {:?}", arg); 23 LL| 2|} 24 ------------------ 25 | Unexecuted instantiation: used_crate::used_only_from_bin_crate_generic_function::<_> 26 ------------------ 27 | used_crate::used_only_from_bin_crate_generic_function::<&alloc::vec::Vec<i32>>: 28 | LL| 1|pub fn used_only_from_bin_crate_generic_function<T: Debug>(arg: T) { 29 | LL| 1| println!("used_only_from_bin_crate_generic_function with {:?}", arg); 30 | LL| 1|} 31 ------------------ 32 | used_crate::used_only_from_bin_crate_generic_function::<&str>: 33 | LL| 1|pub fn used_only_from_bin_crate_generic_function<T: Debug>(arg: T) { 34 | LL| 1| println!("used_only_from_bin_crate_generic_function with {:?}", arg); 35 | LL| 1|} 36 ------------------ 37 LL| |// Expect for above function: `Unexecuted instantiation` (see below) 38 LL| 2|pub fn used_only_from_this_lib_crate_generic_function<T: Debug>(arg: T) { 39 LL| 2| println!("used_only_from_this_lib_crate_generic_function with {:?}", arg); 40 LL| 2|} 41 ------------------ 42 | used_crate::used_only_from_this_lib_crate_generic_function::<&str>: 43 | LL| 1|pub fn used_only_from_this_lib_crate_generic_function<T: Debug>(arg: T) { 44 | LL| 1| println!("used_only_from_this_lib_crate_generic_function with {:?}", arg); 45 | LL| 1|} 46 ------------------ 47 | used_crate::used_only_from_this_lib_crate_generic_function::<alloc::vec::Vec<i32>>: 48 | LL| 1|pub fn used_only_from_this_lib_crate_generic_function<T: Debug>(arg: T) { 49 | LL| 1| println!("used_only_from_this_lib_crate_generic_function with {:?}", arg); 50 | LL| 1|} 51 ------------------ 52 LL| | 53 LL| 2|pub fn used_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) { 54 LL| 2| println!("used_from_bin_crate_and_lib_crate_generic_function with {:?}", arg); 55 LL| 2|} 56 ------------------ 57 | used_crate::used_from_bin_crate_and_lib_crate_generic_function::<&str>: 58 | LL| 1|pub fn used_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) { 59 | LL| 1| println!("used_from_bin_crate_and_lib_crate_generic_function with {:?}", arg); 60 | LL| 1|} 61 ------------------ 62 | used_crate::used_from_bin_crate_and_lib_crate_generic_function::<alloc::vec::Vec<i32>>: 63 | LL| 1|pub fn used_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) { 64 | LL| 1| println!("used_from_bin_crate_and_lib_crate_generic_function with {:?}", arg); 65 | LL| 1|} 66 ------------------ 67 LL| | 68 LL| 2|pub fn used_with_same_type_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) { 69 LL| 2| println!("used_with_same_type_from_bin_crate_and_lib_crate_generic_function with {:?}", arg); 70 LL| 2|} 71 ------------------ 72 | used_crate::used_with_same_type_from_bin_crate_and_lib_crate_generic_function::<&str>: 73 | LL| 1|pub fn used_with_same_type_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) { 74 | LL| 1| println!("used_with_same_type_from_bin_crate_and_lib_crate_generic_function with {:?}", arg); 75 | LL| 1|} 76 ------------------ 77 | used_crate::used_with_same_type_from_bin_crate_and_lib_crate_generic_function::<&str>: 78 | LL| 1|pub fn used_with_same_type_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) { 79 | LL| 1| println!("used_with_same_type_from_bin_crate_and_lib_crate_generic_function with {:?}", arg); 80 | LL| 1|} 81 ------------------ 82 LL| | 83 LL| 0|pub fn unused_generic_function<T: Debug>(arg: T) { 84 LL| 0| println!("unused_generic_function with {:?}", arg); 85 LL| 0|} 86 LL| | 87 LL| 0|pub fn unused_function() { 88 LL| 0| let is_true = std::env::args().len() == 1; 89 LL| 0| let mut countdown = 2; 90 LL| 0| if !is_true { 91 LL| 0| countdown = 20; 92 LL| 0| } 93 LL| 0|} 94 LL| | 95 LL| |#[allow(dead_code)] 96 LL| 0|fn unused_private_function() { 97 LL| 0| let is_true = std::env::args().len() == 1; 98 LL| 0| let mut countdown = 2; 99 LL| 0| if !is_true { 100 LL| 0| countdown = 20; 101 LL| 0| } 102 LL| 0|} 103 LL| | 104 LL| 1|fn use_this_lib_crate() { 105 LL| 1| used_from_bin_crate_and_lib_crate_generic_function("used from library used_crate.rs"); 106 LL| 1| used_with_same_type_from_bin_crate_and_lib_crate_generic_function( 107 LL| 1| "used from library used_crate.rs", 108 LL| 1| ); 109 LL| 1| let some_vec = vec![5, 6, 7, 8]; 110 LL| 1| used_only_from_this_lib_crate_generic_function(some_vec); 111 LL| 1| used_only_from_this_lib_crate_generic_function("used ONLY from library used_crate.rs"); 112 LL| 1|} 113 LL| | 114 LL| |// FIXME(#79651): "Unexecuted instantiation" errors appear in coverage results, 115 LL| |// for example: 116 LL| |// 117 LL| |// | Unexecuted instantiation: used_crate::used_only_from_bin_crate_generic_function::<_> 118 LL| |// 119 LL| |// These notices appear when `llvm-cov` shows instantiations. This may be a 120 LL| |// default option, but it can be suppressed with: 121 LL| |// 122 LL| |// ```shell 123 LL| |// $ `llvm-cov show --show-instantiations=0 ...` 124 LL| |// ``` 125 LL| |// 126 LL| |// The notice is triggered because the function is unused by the library itself, 127 LL| |// and when the library is compiled, a synthetic function is generated, so 128 LL| |// unused function coverage can be reported. Coverage can be skipped for unused 129 LL| |// generic functions with: 130 LL| |// 131 LL| |// ```shell 132 LL| |// $ `rustc -Zunstable-options -C instrument-coverage=except-unused-generics ...` 133 LL| |// ``` 134 LL| |// 135 LL| |// Even though this function is used by `uses_crate.rs` (and 136 LL| |// counted), with substitutions for `T`, those instantiations are only generated 137 LL| |// when the generic function is actually used (from the binary, not from this 138 LL| |// library crate). So the test result shows coverage for all instantiated 139 LL| |// versions and their generic type substitutions, plus the `Unexecuted 140 LL| |// instantiation` message for the non-substituted version. This is valid, but 141 LL| |// unfortunately a little confusing. 142 LL| |// 143 LL| |// The library crate has its own coverage map, and the only way to show unused 144 LL| |// coverage of a generic function is to include the generic function in the 145 LL| |// coverage map, marked as an "unused function". If the library were used by 146 LL| |// another binary that never used this generic function, then it would be valid 147 LL| |// to show the unused generic, with unknown substitution (`_`). 148 LL| |// 149 LL| |// The alternative is to exclude all generics from being included in the "unused 150 LL| |// functions" list, which would then omit coverage results for 151 LL| |// `unused_generic_function<T>()`, below. 152 153$DIR/uses_crate.rs: 154 LL| |// This test was failing on Linux for a while due to #110393 somehow making 155 LL| |// the unused functions not instrumented, but it seems to be fine now. 156 LL| | 157 LL| |// Validates coverage now works with optimizations 158 LL| |// compile-flags: -C opt-level=3 159 LL| | 160 LL| |#![allow(unused_assignments, unused_variables)] 161 LL| | 162 LL| |// aux-build:used_crate.rs 163 LL| |extern crate used_crate; 164 LL| | 165 LL| 1|fn main() { 166 LL| 1| used_crate::used_function(); 167 LL| 1| let some_vec = vec![1, 2, 3, 4]; 168 LL| 1| used_crate::used_only_from_bin_crate_generic_function(&some_vec); 169 LL| 1| used_crate::used_only_from_bin_crate_generic_function("used from bin uses_crate.rs"); 170 LL| 1| used_crate::used_from_bin_crate_and_lib_crate_generic_function(some_vec); 171 LL| 1| used_crate::used_with_same_type_from_bin_crate_and_lib_crate_generic_function("interesting?"); 172 LL| 1|} 173