From 8f8cf46f7a19a14308137c747cdee2631fd12d6f Mon Sep 17 00:00:00 2001 From: Ding Xiang Fei Date: Wed, 2 Oct 2024 05:27:52 +0800 Subject: [PATCH] add section on treatment on extern statics --- src/const_eval.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/const_eval.md b/src/const_eval.md index 4e106f286..ffcf5dbcd 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -91,6 +91,17 @@ const fn not_allowed() -> &'static u32 { const WILL_FAIL: &'static u32 = not_allowed(); ``` +One other instance that the constant evaluation will reject is any attempt to read values behind a `static` item declared in an [`extern`] block, even though the item is not marked as `mut`. +Computing the address to the `static` item is still permitted. However, as soon as a use or dereferencing of the whole or a part of the `static` item is performed, it constitutes a read access which will fail the constant evaluation. + +```rust,edition2021,compile_fail,E0080 +extern { + static S: u32; +} + +const C: u32 = unsafe { S }; +``` + ## Const Functions A _const fn_ is a function that one is permitted to call from a const context. Declaring a function @@ -126,6 +137,7 @@ of whether you are building on a `64` bit or a `32` bit system. [enum discriminants]: items/enumerations.md#discriminants [expression statements]: statements.md#expression-statements [expressions]: expressions.md +[`extern`]: items/external-blocks.md#statics [field]: expressions/field-expr.md [functions]: items/functions.md [grouped]: expressions/grouped-expr.md