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