From 86e6f2ebc7c095f0e1486a5e2c2f60be8b8d0d88 Mon Sep 17 00:00:00 2001 From: Ding Xiang Fei Date: Wed, 11 Sep 2024 21:02:18 +0800 Subject: [PATCH] const expression can borrow static items --- src/const_eval.md | 6 +++++- src/items/constant-items.md | 2 ++ src/items/static-items.md | 8 ++------ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/const_eval.md b/src/const_eval.md index c7a3bca6d..df93c21a2 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -42,7 +42,10 @@ r[const-eval.const-expr.path-item] Recursively defining constants is not allowed. r[const-eval.const-expr.path-static] -* Paths to [statics]. These are only allowed within the initializer of a static. +* Paths to [statics] with these restrictions and observations. + * In particular, reads and writes to any `static`, `static mut` or [`extern` statics] is not allowed. + * Immutable borrows and pointers into immutable part of a `static` are allowed and observes the same restriction + on all other forms of [borrow]s as mentioned below. r[const-eval.const-expr.tuple] * [Tuple expressions]. @@ -177,6 +180,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` statics]: items/external-blocks.md#statics [field]: expressions/field-expr.md [functions]: items/functions.md [grouped]: expressions/grouped-expr.md diff --git a/src/items/constant-items.md b/src/items/constant-items.md index f6ba8da73..417f404f0 100644 --- a/src/items/constant-items.md +++ b/src/items/constant-items.md @@ -39,6 +39,8 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings { }; ``` +The final value of a `const` item cannot contain references to anything mutable. + The constant expression may only be omitted in a [trait definition]. ## Constants with Destructors diff --git a/src/items/static-items.md b/src/items/static-items.md index f688a9024..e070da485 100644 --- a/src/items/static-items.md +++ b/src/items/static-items.md @@ -22,11 +22,7 @@ Static initializers may refer to other statics. Non-`mut` static items that contain a type that is not [interior mutable] may be placed in read-only memory. -All access to a static is safe, but there are a number of restrictions on -statics: - -* The type must have the `Sync` trait bound to allow thread-safe access. -* Constants cannot refer to statics. +The type of an immutable static must implement the [`Sync`](std::marker::Sync) trait. The initializer expression must be omitted in an [external block], and must be provided for free static items. @@ -131,7 +127,7 @@ It can be confusing whether or not you should use a constant item or a static item. Constants should, in general, be preferred over statics unless one of the following are true: -* Large amounts of data are being stored +* Large amounts of data are being stored. * The single-address property of statics is required. * Interior mutability is required.