|  | 
|  | 1 | +#![allow(unused_assignments, unused_variables)] | 
|  | 2 | +// Verify that coverage works with optimizations: | 
|  | 3 | +// compile-flags: -C opt-level=3 | 
|  | 4 | + | 
|  | 5 | +use std::fmt::Debug; | 
|  | 6 | + | 
|  | 7 | +pub fn used_function() { | 
|  | 8 | +    // Initialize test constants in a way that cannot be determined at compile time, to ensure | 
|  | 9 | +    // rustc and LLVM cannot optimize out statements (or coverage counters) downstream from | 
|  | 10 | +    // dependent conditions. | 
|  | 11 | +    let is_true = std::env::args().len() == 1; | 
|  | 12 | +    let mut countdown = 0; | 
|  | 13 | +    if is_true { | 
|  | 14 | +        countdown = 10; | 
|  | 15 | +    } | 
|  | 16 | +    use_this_lib_crate(); | 
|  | 17 | +} | 
|  | 18 | + | 
|  | 19 | +pub fn used_only_from_bin_crate_generic_function<T: Debug>(arg: T) { | 
|  | 20 | +    println!("used_only_from_bin_crate_generic_function with {:?}", arg); | 
|  | 21 | +} | 
|  | 22 | +// Expect for above function: `Unexecuted instantiation` (see below) | 
|  | 23 | +pub fn used_only_from_this_lib_crate_generic_function<T: Debug>(arg: T) { | 
|  | 24 | +    println!("used_only_from_this_lib_crate_generic_function with {:?}", arg); | 
|  | 25 | +} | 
|  | 26 | + | 
|  | 27 | +pub fn used_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) { | 
|  | 28 | +    println!("used_from_bin_crate_and_lib_crate_generic_function with {:?}", arg); | 
|  | 29 | +} | 
|  | 30 | + | 
|  | 31 | +pub fn used_with_same_type_from_bin_crate_and_lib_crate_generic_function<T: Debug>(arg: T) { | 
|  | 32 | +    println!("used_with_same_type_from_bin_crate_and_lib_crate_generic_function with {:?}", arg); | 
|  | 33 | +} | 
|  | 34 | + | 
|  | 35 | +pub fn unused_generic_function<T: Debug>(arg: T) { | 
|  | 36 | +    println!("unused_generic_function with {:?}", arg); | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +pub fn unused_function() { | 
|  | 40 | +    let is_true = std::env::args().len() == 1; | 
|  | 41 | +    let mut countdown = 2; | 
|  | 42 | +    if !is_true { | 
|  | 43 | +        countdown = 20; | 
|  | 44 | +    } | 
|  | 45 | +} | 
|  | 46 | + | 
|  | 47 | +#[allow(dead_code)] | 
|  | 48 | +fn unused_private_function() { | 
|  | 49 | +    let is_true = std::env::args().len() == 1; | 
|  | 50 | +    let mut countdown = 2; | 
|  | 51 | +    if !is_true { | 
|  | 52 | +        countdown = 20; | 
|  | 53 | +    } | 
|  | 54 | +} | 
|  | 55 | + | 
|  | 56 | +fn use_this_lib_crate() { | 
|  | 57 | +    used_from_bin_crate_and_lib_crate_generic_function("used from library used_crate.rs"); | 
|  | 58 | +    used_with_same_type_from_bin_crate_and_lib_crate_generic_function( | 
|  | 59 | +        "used from library used_crate.rs", | 
|  | 60 | +    ); | 
|  | 61 | +    let some_vec = vec![5, 6, 7, 8]; | 
|  | 62 | +    used_only_from_this_lib_crate_generic_function(some_vec); | 
|  | 63 | +    used_only_from_this_lib_crate_generic_function("used ONLY from library used_crate.rs"); | 
|  | 64 | +} | 
|  | 65 | + | 
|  | 66 | +// FIXME(#79651): "Unexecuted instantiation" errors appear in coverage results, | 
|  | 67 | +// for example: | 
|  | 68 | +// | 
|  | 69 | +// | Unexecuted instantiation: used_crate::used_only_from_bin_crate_generic_function::<_> | 
|  | 70 | +// | 
|  | 71 | +// These notices appear when `llvm-cov` shows instantiations. This may be a | 
|  | 72 | +// default option, but it can be suppressed with: | 
|  | 73 | +// | 
|  | 74 | +// ```shell | 
|  | 75 | +// $ `llvm-cov show --show-instantiations=0 ...` | 
|  | 76 | +// ``` | 
|  | 77 | +// | 
|  | 78 | +// The notice is triggered because the function is unused by the library itself, | 
|  | 79 | +// and when the library is compiled, a synthetic function is generated, so | 
|  | 80 | +// unused function coverage can be reported. Coverage can be skipped for unused | 
|  | 81 | +// generic functions with: | 
|  | 82 | +// | 
|  | 83 | +// ```shell | 
|  | 84 | +// $ `rustc -Zunstable-options -C instrument-coverage=except-unused-generics ...` | 
|  | 85 | +// ``` | 
|  | 86 | +// | 
|  | 87 | +// Even though this function is used by `uses_crate.rs` (and | 
|  | 88 | +// counted), with substitutions for `T`, those instantiations are only generated | 
|  | 89 | +// when the generic function is actually used (from the binary, not from this | 
|  | 90 | +// library crate). So the test result shows coverage for all instantiated | 
|  | 91 | +// versions and their generic type substitutions, plus the `Unexecuted | 
|  | 92 | +// instantiation` message for the non-substituted version. This is valid, but | 
|  | 93 | +// unfortunately a little confusing. | 
|  | 94 | +// | 
|  | 95 | +// The library crate has its own coverage map, and the only way to show unused | 
|  | 96 | +// coverage of a generic function is to include the generic function in the | 
|  | 97 | +// coverage map, marked as an "unused function". If the library were used by | 
|  | 98 | +// another binary that never used this generic function, then it would be valid | 
|  | 99 | +// to show the unused generic, with unknown substitution (`_`). | 
|  | 100 | +// | 
|  | 101 | +// The alternative is to exclude all generics from being included in the "unused | 
|  | 102 | +// functions" list, which would then omit coverage results for | 
|  | 103 | +// `unused_generic_function<T>()`, below. | 
0 commit comments