|
| 1 | +//! Tests where the `#[coverage(..)]` attribute can and cannot be used. |
| 2 | +
|
| 3 | +//@ reference: attributes.coverage.allowed-positions |
| 4 | + |
| 5 | +#![feature(coverage_attribute)] |
| 6 | +#![feature(extern_types)] |
| 7 | +#![feature(impl_trait_in_assoc_type)] |
| 8 | +#![warn(unused_attributes)] |
| 9 | +#![coverage(off)] |
| 10 | + |
| 11 | +#[coverage(off)] |
| 12 | +mod submod {} |
| 13 | + |
| 14 | +#[coverage(off)] //~ ERROR coverage attribute not allowed here [E0788] |
| 15 | +type MyTypeAlias = (); |
| 16 | + |
| 17 | +#[coverage(off)] //~ ERROR [E0788] |
| 18 | +trait MyTrait { |
| 19 | + #[coverage(off)] //~ ERROR [E0788] |
| 20 | + const TRAIT_ASSOC_CONST: u32; |
| 21 | + |
| 22 | + #[coverage(off)] //~ ERROR [E0788] |
| 23 | + type TraitAssocType; |
| 24 | + |
| 25 | + #[coverage(off)] //~ ERROR [E0788] |
| 26 | + fn trait_method(&self); |
| 27 | + |
| 28 | + #[coverage(off)] |
| 29 | + fn trait_method_with_default(&self) {} |
| 30 | + |
| 31 | + #[coverage(off)] //~ ERROR [E0788] |
| 32 | + fn trait_assoc_fn(); |
| 33 | +} |
| 34 | + |
| 35 | +#[coverage(off)] |
| 36 | +impl MyTrait for () { |
| 37 | + const TRAIT_ASSOC_CONST: u32 = 0; |
| 38 | + |
| 39 | + #[coverage(off)] //~ ERROR [E0788] |
| 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 | +} |
| 54 | + |
| 55 | +impl HasAssocType for () { |
| 56 | + #[coverage(off)] //~ ERROR [E0788] |
| 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 | +} |
| 66 | + |
| 67 | +#[coverage(off)] |
| 68 | +impl MyStruct { |
| 69 | + #[coverage(off)] |
| 70 | + fn method(&self) {} |
| 71 | + #[coverage(off)] |
| 72 | + fn assoc_fn() {} |
| 73 | +} |
| 74 | + |
| 75 | +extern "C" { |
| 76 | + #[coverage(off)] //~ ERROR [E0788] |
| 77 | + static X: u32; |
| 78 | + |
| 79 | + #[coverage(off)] //~ ERROR [E0788] |
| 80 | + type T; |
| 81 | + |
| 82 | + #[coverage(off)] //~ ERROR [E0788] |
| 83 | + fn foreign_fn(); |
| 84 | +} |
| 85 | + |
| 86 | +#[coverage(off)] |
| 87 | +fn main() { |
| 88 | + #[coverage(off)] //~ ERROR [E0788] |
| 89 | + let _ = (); |
| 90 | + |
| 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 | + |
| 109 | + match () { |
| 110 | + #[coverage(off)] //~ ERROR [E0788] |
| 111 | + () => (), |
| 112 | + } |
| 113 | + |
| 114 | + #[coverage(off)] //~ ERROR [E0788] |
| 115 | + return (); |
| 116 | +} |
0 commit comments