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