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

$DIR/uses_crate.rs:
   LL|       |// This test was failing on Linux for a while due to #110393 somehow making
   LL|       |// the unused functions not instrumented, but it seems to be fine now.
   LL|       |
   LL|       |// Validates coverage now works with optimizations
   LL|       |// compile-flags: -C opt-level=3
   LL|       |
   LL|       |#![allow(unused_assignments, unused_variables)]
   LL|       |
   LL|       |// aux-build:used_crate.rs
   LL|       |extern crate used_crate;
   LL|       |
   LL|      1|fn main() {
   LL|      1|    used_crate::used_function();
   LL|      1|    let some_vec = vec![1, 2, 3, 4];
   LL|      1|    used_crate::used_only_from_bin_crate_generic_function(&some_vec);
   LL|      1|    used_crate::used_only_from_bin_crate_generic_function("used from bin uses_crate.rs");
   LL|      1|    used_crate::used_from_bin_crate_and_lib_crate_generic_function(some_vec);
   LL|      1|    used_crate::used_with_same_type_from_bin_crate_and_lib_crate_generic_function("interesting?");
   LL|      1|}