rust/tests/ui/sanitize-attr/valid-sanitize.rs
Bastian Kersting 3ef065bf87 Implement the #[sanitize(..)] attribute
This change implements the #[sanitize(..)] attribute, which opts to
replace the currently unstable #[no_sanitize]. Essentially the new
attribute works similar as #[no_sanitize], just with more flexible
options regarding where it is applied. E.g. it is possible to turn
a certain sanitizer either on or off:
`#[sanitize(address = "on|off")]`

This attribute now also applies to more places, e.g. it is possible
to turn off a sanitizer for an entire module or impl block:
```rust
\#[sanitize(address = "off")]
mod foo {
    fn unsanitized(..) {}

    #[sanitize(address = "on")]
    fn sanitized(..) {}
}

\#[sanitize(thread = "off")]
impl MyTrait for () {
    ...
}
```

This attribute is enabled behind the unstable `sanitize` feature.
2025-08-18 08:30:00 +00:00

116 lines
3.5 KiB
Rust

//! 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 ();
}