Skip to content
Open
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
10 changes: 7 additions & 3 deletions src/items/static-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
A *static item* is similar to a [constant], except that it represents a precise
memory location in the program. All references to the static refer to the same
memory location. Static items have the `static` lifetime, which outlives all
other lifetimes in a Rust program. Non-`mut` static items that contain a type
other lifetimes in a Rust program.

## Non-mut statics

Non-`mut` static items that contain a type
that is not [interior mutable] may be placed in read-only memory. Static items
do not call [`drop`] at the end of the program.
Comment on lines 16 to 17
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like it could use some more re-arranging. The sentence about drop should probably be moved up since it applies to both mut and non-mut, correct?


All access to a static is safe, but there are a number of restrictions on
statics:
All access to a Non-`mut` static is safe, but there are a number of restrictions on
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
All access to a Non-`mut` static is safe, but there are a number of restrictions on
All access to a non-`mut` static is safe, but there are a number of restrictions on

Non-`mut` statics:
Comment on lines +19 to +20
Copy link
Contributor

Choose a reason for hiding this comment

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

Not all these rules seem to apply to non-mut static. In fact, maybe only the first one?

Also, this sentence (the original) seems to be a very strong assertion (all access?). I'm not up to speed on safety issues, so maybe @Centril could validate what should be safe, or if this wording makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this diff seems to remove rules about static mut like the thing about referring to constants.

cc @RalfJung and @eddyb on the access thing, but notably, a static in an extern block is not safe to access and requires an unsafe context (https://doc.rust-lang.org/nightly/reference/items/external-blocks.html#statics). However, some static take be part of some internal contract that makes it unsound to access under some circumstances, even if the language rules do not enforce it. Under that reading it seems to me that the current wording makes sense.

Copy link
Member

Choose a reason for hiding this comment

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

I mean you can access a non-mut static in safe code, so as a matter of fact this statement is correct, or at least some interpretation of it. ;) However, as a reflection of what the compiler is doing, extern statics should probably be excluded somewhere.

"all access" is slightly misleading though; writing to a static X: i32 is not safe and thus rejected by the compiler.

(This might also be a good place to call out that mutating a non-mut non-interior-mut extern static X: T is UB, even if that mutation happens by non-Rust code.)

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Non-`mut` statics:
non-`mut` statics:


* The type must have the `Sync` trait bound to allow thread-safe access.
* Statics allow using paths to statics in the [constant expression] used to
Expand Down