about summary refs log tree commit diff
path: root/tests/ui/sanitize-attr/valid-sanitize.rs
blob: ebe76fcba0442ee70cc2cea9ec270eddd9cc9435 (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
//! Tests where the `#[sanitize(..)]` attribute can and cannot be used.

#![feature(sanitize)]
#![feature(extern_types)]
#![feature(impl_trait_in_assoc_type)]
#![warn(unused_attributes)]
#![sanitize(address = "off", thread = "on")]

#[sanitize(address = "off", thread = "on")]
mod submod {}

#[sanitize(address = "off")]
static FOO: u32 = 0;

#[sanitize(thread = "off")] //~ ERROR sanitize attribute not allowed here
static BAR: u32 = 0;

#[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
type MyTypeAlias = ();

#[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
trait MyTrait {
    #[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
    const TRAIT_ASSOC_CONST: u32;

    #[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
    type TraitAssocType;

    #[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
    fn trait_method(&self);

    #[sanitize(address = "off", thread = "on")]
    fn trait_method_with_default(&self) {}

    #[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
    fn trait_assoc_fn();
}

#[sanitize(address = "off")]
impl MyTrait for () {
    const TRAIT_ASSOC_CONST: u32 = 0;

    #[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
    type TraitAssocType = Self;

    #[sanitize(address = "off", thread = "on")]
    fn trait_method(&self) {}
    #[sanitize(address = "off", thread = "on")]
    fn trait_method_with_default(&self) {}
    #[sanitize(address = "off", thread = "on")]
    fn trait_assoc_fn() {}
}

trait HasAssocType {
    type T;
    fn constrain_assoc_type() -> Self::T;
}

impl HasAssocType for () {
    #[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
    type T = impl Copy;
    fn constrain_assoc_type() -> Self::T {}
}

#[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
struct MyStruct {
    #[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
    field: u32,
}

#[sanitize(address = "off", thread = "on")]
impl MyStruct {
    #[sanitize(address = "off", thread = "on")]
    fn method(&self) {}
    #[sanitize(address = "off", thread = "on")]
    fn assoc_fn() {}
}

extern "C" {
    #[sanitize(address = "off", thread = "on")] //~ ERROR sanitize attribute not allowed here
    static X: u32;

    #[sanitize(address = "off", thread = "on")] //~ ERROR sanitize attribute not allowed here
    type T;

    #[sanitize(address = "off", thread = "on")] //~ ERROR sanitize attribute not allowed here
    fn foreign_fn();
}

#[sanitize(address = "off", thread = "on")]
fn main() {
    #[sanitize(address = "off", thread = "on")] //~ ERROR sanitize attribute not allowed here
    let _ = ();

    // Currently not allowed on let statements, even if they bind to a closure.
    // It might be nice to support this as a special case someday, but trying
    // to define the precise boundaries of that special case might be tricky.
    #[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
    let _let_closure = || ();

    // In situations where attributes can already be applied to expressions,
    // the sanitize attribute is allowed on closure expressions.
    let _closure_tail_expr = {
        #[sanitize(address = "off", thread = "on")]
        || ()
    };

    match () {
        #[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
        () => (),
    }

    #[sanitize(address = "off")] //~ ERROR sanitize attribute not allowed here
    return ();
}