Skip to content

allow constants to refer to mutable/external memory, but reject such constants as patterns #1859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/items/constant-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,26 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
```

r[items.const.final-value-immutable]
The final value of a `const` item cannot contain references to anything mutable.
The final value of a `const` item cannot contain a mutable reference.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty much an entirely new clause, at least in terms of intent, so IMO it'd make sense to give it a new name, such as items.const.no-mutable-ref.


```rust
# use core::sync::atomic::AtomicU8;
static mut S_M: u8 = 0;
static S_IM: AtomicU8 = AtomicU8::new(0);
const C_M: &u8 = unsafe { &mut S_M }; // OK
const C_IM: &AtomicU8 = &S_IM; // OK
const C_MR: &mut u8 = unsafe { &mut S_M }; // ERROR not allowed
```

> [!NOTE]
> We also disallow, in the final value, shared references to mutable statics created in the initializer for a separate reason. Consider:
>
> ```rust
> # use core::sync::atomic::AtomicU8;
> const C: &AtomicU8 = &AtomicU8:new(0); // ERROR
> ```
>
> Here, the `AtomicU8` is a temporary that is lifetime extended to `'static` (see [destructors.scope.lifetime-extension.static]), and references to lifetime-extended temporaries with interior mutability are not allowed in the final value of a constant expression (see [const-eval.const-expr.borrows]).

r[items.const.expr-omission]
The constant expression may only be omitted in a [trait definition].
Expand Down
3 changes: 3 additions & 0 deletions src/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,9 @@ r[patterns.const.generic]
In particular, the value of `C` must be known at pattern-building time (which is pre-monomorphization).
This means that associated consts that involve generic parameters cannot be used as patterns.

r[patterns.const.immutable]
The value of `C` must not contain any references to mutable statics (`static mut` or interior mutable) or `extern` statics.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The value of `C` must not contain any references to mutable statics (`static mut` or interior mutable) or `extern` statics.
The value of `C` must not contain any references to mutable statics (`static mut` or interior mutable `static`) or `extern` statics.


r[patterns.const.translation]
After ensuring all conditions are met, the constant value is translated into a pattern, and now behaves exactly as-if that pattern had been written directly.
In particular, it fully participates in exhaustiveness checking.
Expand Down
Loading