Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9124662

Browse files
committed
Expand the main test for where the coverage attribute is allowed
Some of these cases are also implicitly checked by other tests, but it's helpful to also explicitly list them in the main test.
1 parent 5e98118 commit 9124662

File tree

2 files changed

+148
-48
lines changed

2 files changed

+148
-48
lines changed

tests/ui/coverage-attr/allowed-positions.rs

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,68 @@
88
#![warn(unused_attributes)]
99
#![coverage(off)]
1010

11+
#[coverage(off)]
12+
mod submod {}
13+
1114
#[coverage(off)] //~ ERROR [E0788]
12-
trait Trait {
13-
#[coverage(off)] //~ ERROR [E0788]
14-
const X: u32;
15+
type MyTypeAlias = ();
1516

17+
#[coverage(off)] //~ ERROR [E0788]
18+
trait MyTrait {
1619
#[coverage(off)] //~ ERROR [E0788]
17-
type T;
20+
const TRAIT_ASSOC_CONST: u32;
1821

19-
type U;
22+
#[coverage(off)] //~ ERROR [E0788]
23+
type TraitAssocType;
2024

2125
#[coverage(off)] //~ ERROR [E0788]
22-
fn f(&self);
26+
fn trait_method(&self);
27+
28+
#[coverage(off)]
29+
fn trait_method_with_default(&self) {}
2330

2431
#[coverage(off)] //~ ERROR [E0788]
25-
fn g();
32+
fn trait_assoc_fn();
2633
}
2734

2835
#[coverage(off)]
29-
impl Trait for () {
30-
const X: u32 = 0;
36+
impl MyTrait for () {
37+
const TRAIT_ASSOC_CONST: u32 = 0;
3138

3239
#[coverage(off)] //~ ERROR [E0788]
33-
type T = Self;
40+
type TraitAssocType = Self;
41+
42+
#[coverage(off)]
43+
fn trait_method(&self) {}
44+
#[coverage(off)]
45+
fn trait_method_with_default(&self) {}
46+
#[coverage(off)]
47+
fn trait_assoc_fn() {}
48+
}
49+
50+
trait HasAssocType {
51+
type T;
52+
fn constrain_assoc_type() -> Self::T;
53+
}
3454

55+
impl HasAssocType for () {
3556
#[coverage(off)] //~ ERROR [E0788]
36-
type U = impl Trait; //~ ERROR unconstrained opaque type
57+
type T = impl Copy;
58+
fn constrain_assoc_type() -> Self::T {}
59+
}
60+
61+
#[coverage(off)] //~ ERROR [E0788]
62+
struct MyStruct {
63+
#[coverage(off)] //~ ERROR [E0788]
64+
field: u32,
65+
}
3766

38-
fn f(&self) {}
39-
fn g() {}
67+
#[coverage(off)]
68+
impl MyStruct {
69+
#[coverage(off)]
70+
fn method(&self) {}
71+
#[coverage(off)]
72+
fn assoc_fn() {}
4073
}
4174

4275
extern "C" {
@@ -45,13 +78,34 @@ extern "C" {
4578

4679
#[coverage(off)] //~ ERROR [E0788]
4780
type T;
81+
82+
#[coverage(off)] //~ ERROR [E0788]
83+
fn foreign_fn();
4884
}
4985

5086
#[coverage(off)]
5187
fn main() {
5288
#[coverage(off)] //~ ERROR [E0788]
5389
let _ = ();
5490

91+
// Currently not allowed on let statements, even if they bind to a closure.
92+
// It might be nice to support this as a special case someday, but trying
93+
// to define the precise boundaries of that special case might be tricky.
94+
#[coverage(off)] //~ ERROR [E0788]
95+
let _let_closure = || ();
96+
97+
// In situations where attributes can already be applied to expressions,
98+
// the coverage attribute is allowed on closure expressions.
99+
let _closure_tail_expr = {
100+
#[coverage(off)]
101+
|| ()
102+
};
103+
104+
// Applying attributes to arbitrary expressions requires an unstable
105+
// feature, but if that feature were enabled then this would be allowed.
106+
let _closure_expr = #[coverage(off)] || ();
107+
//~^ ERROR attributes on expressions are experimental [E0658]
108+
55109
match () {
56110
#[coverage(off)] //~ ERROR [E0788]
57111
() => (),
Lines changed: 81 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,158 @@
1+
error[E0658]: attributes on expressions are experimental
2+
--> $DIR/allowed-positions.rs:106:25
3+
|
4+
LL | let _closure_expr = #[coverage(off)] || ();
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
8+
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
111
error[E0788]: attribute should be applied to a function definition or closure
2-
--> $DIR/allowed-positions.rs:11:1
12+
--> $DIR/allowed-positions.rs:14:1
13+
|
14+
LL | #[coverage(off)]
15+
| ^^^^^^^^^^^^^^^^
16+
LL | type MyTypeAlias = ();
17+
| ---------------------- not a function or closure
18+
19+
error[E0788]: attribute should be applied to a function definition or closure
20+
--> $DIR/allowed-positions.rs:17:1
321
|
422
LL | #[coverage(off)]
523
| ^^^^^^^^^^^^^^^^
6-
LL | / trait Trait {
24+
LL | / trait MyTrait {
725
LL | | #[coverage(off)]
8-
LL | | const X: u32;
26+
LL | | const TRAIT_ASSOC_CONST: u32;
927
... |
10-
LL | | fn g();
28+
LL | | fn trait_assoc_fn();
29+
LL | | }
30+
| |_- not a function or closure
31+
32+
error[E0788]: attribute should be applied to a function definition or closure
33+
--> $DIR/allowed-positions.rs:61:1
34+
|
35+
LL | #[coverage(off)]
36+
| ^^^^^^^^^^^^^^^^
37+
LL | / struct MyStruct {
38+
LL | | #[coverage(off)]
39+
LL | | field: u32,
1140
LL | | }
1241
| |_- not a function or closure
1342

1443
error[E0788]: attribute should be applied to a function definition or closure
15-
--> $DIR/allowed-positions.rs:52:5
44+
--> $DIR/allowed-positions.rs:63:5
45+
|
46+
LL | #[coverage(off)]
47+
| ^^^^^^^^^^^^^^^^
48+
LL | field: u32,
49+
| ---------- not a function or closure
50+
51+
error[E0788]: attribute should be applied to a function definition or closure
52+
--> $DIR/allowed-positions.rs:88:5
1653
|
1754
LL | #[coverage(off)]
1855
| ^^^^^^^^^^^^^^^^
1956
LL | let _ = ();
2057
| ----------- not a function or closure
2158

2259
error[E0788]: attribute should be applied to a function definition or closure
23-
--> $DIR/allowed-positions.rs:56:9
60+
--> $DIR/allowed-positions.rs:94:5
61+
|
62+
LL | #[coverage(off)]
63+
| ^^^^^^^^^^^^^^^^
64+
LL | let _let_closure = || ();
65+
| ------------------------- not a function or closure
66+
67+
error[E0788]: attribute should be applied to a function definition or closure
68+
--> $DIR/allowed-positions.rs:110:9
2469
|
2570
LL | #[coverage(off)]
2671
| ^^^^^^^^^^^^^^^^
2772
LL | () => (),
2873
| -------- not a function or closure
2974

3075
error[E0788]: attribute should be applied to a function definition or closure
31-
--> $DIR/allowed-positions.rs:60:5
76+
--> $DIR/allowed-positions.rs:114:5
3277
|
3378
LL | #[coverage(off)]
3479
| ^^^^^^^^^^^^^^^^
3580
LL | return ();
3681
| --------- not a function or closure
3782

3883
error[E0788]: attribute should be applied to a function definition or closure
39-
--> $DIR/allowed-positions.rs:13:5
84+
--> $DIR/allowed-positions.rs:19:5
4085
|
4186
LL | #[coverage(off)]
4287
| ^^^^^^^^^^^^^^^^
43-
LL | const X: u32;
44-
| ------------- not a function or closure
88+
LL | const TRAIT_ASSOC_CONST: u32;
89+
| ----------------------------- not a function or closure
4590

4691
error[E0788]: attribute should be applied to a function definition or closure
47-
--> $DIR/allowed-positions.rs:16:5
92+
--> $DIR/allowed-positions.rs:22:5
4893
|
4994
LL | #[coverage(off)]
5095
| ^^^^^^^^^^^^^^^^
51-
LL | type T;
52-
| ------- not a function or closure
96+
LL | type TraitAssocType;
97+
| -------------------- not a function or closure
5398

5499
error[E0788]: attribute should be applied to a function definition or closure
55-
--> $DIR/allowed-positions.rs:21:5
100+
--> $DIR/allowed-positions.rs:25:5
56101
|
57102
LL | #[coverage(off)]
58103
| ^^^^^^^^^^^^^^^^
59-
LL | fn f(&self);
60-
| ------------ not a function or closure
104+
LL | fn trait_method(&self);
105+
| ----------------------- not a function or closure
61106

62107
error[E0788]: attribute should be applied to a function definition or closure
63-
--> $DIR/allowed-positions.rs:24:5
108+
--> $DIR/allowed-positions.rs:31:5
64109
|
65110
LL | #[coverage(off)]
66111
| ^^^^^^^^^^^^^^^^
67-
LL | fn g();
68-
| ------- not a function or closure
112+
LL | fn trait_assoc_fn();
113+
| -------------------- not a function or closure
69114

70115
error[E0788]: attribute should be applied to a function definition or closure
71-
--> $DIR/allowed-positions.rs:32:5
116+
--> $DIR/allowed-positions.rs:39:5
72117
|
73118
LL | #[coverage(off)]
74119
| ^^^^^^^^^^^^^^^^
75-
LL | type T = Self;
76-
| -------------- not a function or closure
120+
LL | type TraitAssocType = Self;
121+
| --------------------------- not a function or closure
77122

78123
error[E0788]: attribute should be applied to a function definition or closure
79-
--> $DIR/allowed-positions.rs:35:5
124+
--> $DIR/allowed-positions.rs:56:5
80125
|
81126
LL | #[coverage(off)]
82127
| ^^^^^^^^^^^^^^^^
83-
LL | type U = impl Trait;
84-
| -------------------- not a function or closure
128+
LL | type T = impl Copy;
129+
| ------------------- not a function or closure
85130

86131
error[E0788]: attribute should be applied to a function definition or closure
87-
--> $DIR/allowed-positions.rs:43:5
132+
--> $DIR/allowed-positions.rs:76:5
88133
|
89134
LL | #[coverage(off)]
90135
| ^^^^^^^^^^^^^^^^
91136
LL | static X: u32;
92137
| -------------- not a function or closure
93138

94139
error[E0788]: attribute should be applied to a function definition or closure
95-
--> $DIR/allowed-positions.rs:46:5
140+
--> $DIR/allowed-positions.rs:79:5
96141
|
97142
LL | #[coverage(off)]
98143
| ^^^^^^^^^^^^^^^^
99144
LL | type T;
100145
| ------- not a function or closure
101146

102-
error: unconstrained opaque type
103-
--> $DIR/allowed-positions.rs:36:14
104-
|
105-
LL | type U = impl Trait;
106-
| ^^^^^^^^^^
147+
error[E0788]: attribute should be applied to a function definition or closure
148+
--> $DIR/allowed-positions.rs:82:5
107149
|
108-
= note: `U` must be used in combination with a concrete type within the same impl
150+
LL | #[coverage(off)]
151+
| ^^^^^^^^^^^^^^^^
152+
LL | fn foreign_fn();
153+
| ---------------- not a function or closure
109154

110-
error: aborting due to 13 previous errors
155+
error: aborting due to 18 previous errors
111156

112-
For more information about this error, try `rustc --explain E0788`.
157+
Some errors have detailed explanations: E0658, E0788.
158+
For more information about an error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)