Summary

Consider some attributes ‘unsafe’, so that they must only be used like this:

#[unsafe(no_mangle)]

Motivation

Some of our attributes, such as no_mangle, can be used to cause Undefined Behavior without any unsafe block. If this was regular code we would require them to be placed in an unsafe {} block, but since they are attributes that makes less sense. Hence we need a concept of ‘unsafe attributes’ and accompanying syntax to declare that one is aware of the UB risks here (and it might be good to add a SAFETY comment explaining why this use of the attribute is fine).

Guide-level explanation

Example explanation for no_mangle; the other attributes need something similar.

When declaring a function like this

#[no_mangle]
pub fn write(...) { ... }

this will cause Rust to generate a globally visible function with the linker/export name write. As consequence of that, other code that wants to call the POSIX write function might end up calling this other write instead. This can easily lead to Undefined Behavior:

To avoid this, when declaring a function no_mangle, it is important that the name of the function does not clash with other globally named functions. Similar to how unsafe { ... } blocks are used to acknowledge that this code is dangerous and needs manual checking, unsafe(no_mangle) acknowledges that no_mangle is dangerous and needs to be manually checked for correctness:

// SAFETY: there is no other global function of this name
#[unsafe(no_mangle)]
pub fn my_own_write(...) { ... }

Note that when writing a library crate, it is in general not possible to make claims like “there is no other global function of this name”. This is a fundamental limitation of the global linking namespace, and not something Rust currently is able to overcome. Libraries that make such assumptions should ideally document somewhere publicly that they consider some namespace, i.e. every function starting with _mycrate__, to be reserved for their exclusive use.

Reference-level explanation

Some attributes (e.g. no_mangle, export_name, link_section – see here for a more complete list) are considered “unsafe” attributes. An unsafe attribute must only be used inside unsafe(...) in the attribute declaration, like

#[unsafe(no_mangle)]

For backwards compatibility reasons, using these attributes outside of unsafe(...) is just a lint, not a hard error. The lint is called unsafe_attr_outside_unsafe. Initially, this lint will be allow-by-default. Unsafe attributes that are added in the future can hard-require unsafe from the start since the backwards compatibility concern does not apply to them. The 2024 edition is also expected to increase the severity of this lint, possibly even making it a hard error.

Syntactically, for each unsafe attribute attr, we now also accept unsafe(attr) anywhere that attr can be used (in particular, inside cfg_attr). unsafe cannot be nested, cannot contain cfg_attr, and cannot contain any other (non-unsafe) attributes. Only a single attribute can be used inside unsafe, i.e., unsafe(foo, bar) is invalid.

The deny(unsafe_code) lint denies the use of unsafe attributes both inside and outside of unsafe(...) blocks. (That lint currently has special handling to deny these attributes. Once there is a general notion of ‘unsafe attributes’ as proposed by this RFC, that special handling should no longer be needed.)

The unsafe(...) attribute block is required even for functions declared inside an unsafe block. That is, the following is an error:

fn outer() {
  unsafe {
    #[no_mangle]
    fn write() {}
  }
}

This matches the fact that expression-level unsafety is not inherited for items declared inside other items.

Drawbacks

I think if we had thought of this around Rust 1.0, then this would be rather uncontroversial. As things stand now, this proposal will cause a lot of churn since all existing uses of these unsafe attributes need to be adjusted. The warning for using unsafe attributes outside unsafe(...) should probably have an auto-fix available to help ease the transition here.

Rationale and alternatives

Prior art

We have unsafe blocks; this is basically the same thing for the “attributes DSL”.

In the attribute DSL, we already have a “nesting” construct: cfg_attr. That allows terms like #[cfg_attr(debug_assertions, deny(unsafe_code), allow(unused))], so there is precedent for having a list of attributes inside a single attribute.

I don’t know of other languages that would distinguish safe and unsafe attributes.

Unresolved questions

Future possibilities