Skip to content

Commit 35f29c3

Browse files
Merge #248
248: Expand #[pre_init] safety docs, discourage its use r=adamgreig a=jonas-schievink Co-authored-by: Jonas Schievink <[email protected]>
2 parents 1e2b541 + 670dfe9 commit 35f29c3

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

cortex-m-rt/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,18 @@ pub use macros::exception;
658658
///
659659
/// The function must have the signature of `unsafe fn()`.
660660
///
661-
/// The function passed will be called before static variables are initialized. Any access of static
661+
/// # Safety
662+
///
663+
/// The function will be called before static variables are initialized. Any access of static
662664
/// variables will result in undefined behavior.
663665
///
666+
/// **Warning**: Due to [rvalue static promotion][rfc1414] static variables may be accessed whenever
667+
/// taking a reference to a constant. This means that even trivial expressions such as `&1` in the
668+
/// `#[pre_init]` function *or any code called by it* will cause **immediate undefined behavior**.
669+
///
670+
/// Users are advised to only use the `#[pre_init]` feature when absolutely necessary as these
671+
/// constraints make safe usage difficult.
672+
///
664673
/// # Examples
665674
///
666675
/// ```
@@ -672,6 +681,8 @@ pub use macros::exception;
672681
///
673682
/// # fn main() {}
674683
/// ```
684+
///
685+
/// [rfc1414]: https://github.com/rust-lang/rfcs/blob/master/text/1414-rvalue_static_promotion.md
675686
pub use macros::pre_init;
676687

677688
#[export_name = "error: cortex-m-rt appears more than once in the dependency graph"]

0 commit comments

Comments
 (0)