-
Notifications
You must be signed in to change notification settings - Fork 533
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
Conversation
…constants as patterns
I'm trying to figure out, which reference rule rejects the following? static mut S: i32 = 1;
const C: &mut i32 = unsafe { &mut S }; // ERROR: constructing invalid value What I'm getting at, is that it seems like despite removing
Is there maybe still some "final value must not have mutable references" restriction? Is there some rule in the reference that would explain the following? const C: &AtomicU32 = &AtomicU32::new(1); // ERROR: constants cannot refer to interior mutable data It seems like a pretty strong blanket statement that "constants cannot refer to interior mutable data", but the AtomicU32 example from rust-lang/rust#140942 seems like you can refer to interior mutable data. What exactly is the difference between a reference to a static versus a reference to a value created in the initializer? Similarly, I'm trying to understand E0764. Is the description of E0764 out of date? const C: &mut i32 = &mut 1; // ERROR: mutable references are not allowed in the final value of constants Questions:
|
So, I can't always tell you where any of that is in the reference, I had a hard time even find the things I am editing here.^^ But I can tell you what the compiler does: static mut S: i32 = 1;
const C: &mut i32 = unsafe { &mut S }; // ERROR: constructing invalid value We do a type-directed pass over the final value of the constant, mostly to check e.g. that every
const C: &AtomicU32 = &AtomicU32::new(1); // ERROR: constants cannot refer to interior mutable data That falls out of the following two things which are in the reference:
Oh yeah, that is very outdated. And I don't understand that "remember" thing either. (I worry this might be true for many of our error descriptions -- at least I don't ever seem them so I wouldn't even notice them being outdated. Multiple of our Exxxx tests don't even emit the right error code any more...) const C: &mut i32 = &mut 1; // ERROR: mutable references are not allowed in the final value of constants This is the exact same as "constants cannot refer to interior mutable data", but for a mutable reference rather than an interior mutable shared ref. I don't know why the error messages are worded so differently... |
@RalfJung we had some discussion in the spec about this wording. I'm having a hard time (quite honestly) knowing with precision what you are saying and what the spec is saying. I want to present my mental model and check if it works for you and then ask you some questions in terms of that model. CONST VALUEA const "value" is defined by some kind of grammar that looks kind of like a valtree (but with some extra things):
where a referent is either a static value (in which case we know which static) or another valtree (indicating a constant). And then when you say, "we do a type-directed pass over the final value of the constant", I take it that you have a pair like (Type, Value) and then we can kind of "visit" the components in a relatively straightforward way. And then when you say, "we just throw an error if we find any mutable reference in a const", you mean that if at any point in this traversal, Does that all make sense to you? Is my model accurate? If it would be helpful I can try to write it in a kind of pseudo-code-y Rust. |
src/items/constant-items.md
Outdated
r[items.const.final-value-immutable] | ||
The final value of a `const` item cannot contain references to anything mutable. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We were wondering if it would make sense to change the wording here instead of removing it. Perhaps something like:
"The final value of a const
item cannot contain a mutable reference."
Most of us seemed to think this is correct. Perhaps there is some other rule forbidding this already (implicitly or explicitly), but it seems like it wouldn't hurt to be clear about it here?
No, it's not a valtree. Speaking about the internal compiler representation, a But given an arbitrary place and a type, we can just walk that place according to the type. We don't construct a tree, we just do a type-guided traversal of this memory, recursing into structs and tuples and enums and so on. That's what const validation does. Mostly that traversal just checks regular (recursive) validity, but for mutability we are super extra paranoid to make it less likely for (I will note that we are now digging into pre-existing lacks of documentation here, and have ventured far outside of what rust-lang/rust#140942 is about. I'd rather not have to rebase that PR again while we figure out how to document things that have been undocumented for years...) |
c9dcbbe
to
5cdb502
Compare
b4adf8b
to
caca8df
Compare
3f73a6e
to
1438d40
Compare
In the text as revised, it wasn't clear what rule prevented a mutable reference from appearing in the final value of a constant item, so let's add a rule specifically for that. Similarly, it may not be immediately clear what rule prevents a reference to a lifetime-extended interior mutable temporary created in the initializer, so let's add an admonition section describing how other rules apply to prevent this.
1360541
to
649b729
Compare
649b729
to
5910551
Compare
Update books ## rust-lang/book 3 commits in ef1ce8f87a8b18feb1b6a9cf9a4939a79bde6795..b2d1a0821e12a676b496d61891b8e3d374a8e832 2025-07-08 17:24:41 UTC to 2025-07-02 21:30:57 UTC - Chapter 16 from tech review (rust-lang/book#4438) - WIP ch 17 edits after tech review (rust-lang/book#4319) - Chapter 15 from tech review (rust-lang/book#4433) ## rust-embedded/book 1 commits in 41f688a598a5022b749e23d37f3c524f6a0b28e1..fe88fbb68391a465680dd91109f0a151a1676f3e 2025-07-08 18:54:25 UTC to 2025-07-08 18:54:25 UTC - Clarify usage of #[interrupt] attribute and recommend device crate re… (rust-embedded/book#386) ## rust-lang/nomicon 3 commits in 8b61acfaea822e9ac926190bc8f15791c33336e8..3ff384320598bbe8d8cfe5cb8f18f78a3a3e6b15 2025-07-05 07:34:22 UTC to 2025-07-05 07:13:51 UTC - Add build script part to FFI chapter for more clear and smooth learn … (rust-lang/nomicon#440) - Cleanups for tree example of splitting borrows (rust-lang/nomicon#443) - Handle drop zst (rust-lang/nomicon#425) ## rust-lang/reference 17 commits in e9fc99f107840813916f62e16b3f6d9556e1f2d8..1f45bd41fa6c17b7c048ed6bfe5f168c4311206a 2025-07-11 23:15:51 UTC to 2025-07-01 16:49:33 UTC - mention an important use for the naked attribute (rust-lang/reference#1929) - Array expression repeat operands can be const blocks. (rust-lang/reference#1928) - Document (tuple) struct pattern namespace behavior (rust-lang/reference#1925) - Replace set of en dashes with set of em dashes (rust-lang/reference#1926) - Update `should_panic` to use the attribute template (rust-lang/reference#1882) - const-eval.const-expr.borrows: mention indirect places (rust-lang/reference#1865) - associated-items.md: remove redundant word (rust-lang/reference#1874) - introduction.md: replace hard-to-read example (rust-lang/reference#1873) - typo (rust-lang/reference#1924) - Update `ignore` to use the attribute template (rust-lang/reference#1881) - Update `test` to use the attribute template (rust-lang/reference#1880) - Update `cfg_attr` to use the attribute template (rust-lang/reference#1879) - Update `cfg` to use the attribute template (rust-lang/reference#1878) - allow constants to refer to mutable/external memory, but reject such constants as patterns (rust-lang/reference#1859) - Remove outdated comment about non-copy unions (rust-lang/reference#1872) - Add a template for documenting attributes (rust-lang/reference#1877) - Switch enum grammar to use "variant" (rust-lang/reference#1876) ## rust-lang/rust-by-example 1 commits in 288b4e4948add43f387cad35adc7b1c54ca6fe12..e386be5f44af711854207c11fdd61bb576270b04 2025-07-04 23:17:15 UTC to 2025-07-04 23:17:15 UTC - Update Chinese translations (rust-lang/rust-by-example#1943)
Update books ## rust-lang/book 3 commits in ef1ce8f87a8b18feb1b6a9cf9a4939a79bde6795..b2d1a0821e12a676b496d61891b8e3d374a8e832 2025-07-08 17:24:41 UTC to 2025-07-02 21:30:57 UTC - Chapter 16 from tech review (rust-lang/book#4438) - WIP ch 17 edits after tech review (rust-lang/book#4319) - Chapter 15 from tech review (rust-lang/book#4433) ## rust-embedded/book 1 commits in 41f688a598a5022b749e23d37f3c524f6a0b28e1..fe88fbb68391a465680dd91109f0a151a1676f3e 2025-07-08 18:54:25 UTC to 2025-07-08 18:54:25 UTC - Clarify usage of #[interrupt] attribute and recommend device crate re… (rust-embedded/book#386) ## rust-lang/nomicon 3 commits in 8b61acfaea822e9ac926190bc8f15791c33336e8..3ff384320598bbe8d8cfe5cb8f18f78a3a3e6b15 2025-07-05 07:34:22 UTC to 2025-07-05 07:13:51 UTC - Add build script part to FFI chapter for more clear and smooth learn … (rust-lang/nomicon#440) - Cleanups for tree example of splitting borrows (rust-lang/nomicon#443) - Handle drop zst (rust-lang/nomicon#425) ## rust-lang/reference 17 commits in e9fc99f107840813916f62e16b3f6d9556e1f2d8..1f45bd41fa6c17b7c048ed6bfe5f168c4311206a 2025-07-11 23:15:51 UTC to 2025-07-01 16:49:33 UTC - mention an important use for the naked attribute (rust-lang/reference#1929) - Array expression repeat operands can be const blocks. (rust-lang/reference#1928) - Document (tuple) struct pattern namespace behavior (rust-lang/reference#1925) - Replace set of en dashes with set of em dashes (rust-lang/reference#1926) - Update `should_panic` to use the attribute template (rust-lang/reference#1882) - const-eval.const-expr.borrows: mention indirect places (rust-lang/reference#1865) - associated-items.md: remove redundant word (rust-lang/reference#1874) - introduction.md: replace hard-to-read example (rust-lang/reference#1873) - typo (rust-lang/reference#1924) - Update `ignore` to use the attribute template (rust-lang/reference#1881) - Update `test` to use the attribute template (rust-lang/reference#1880) - Update `cfg_attr` to use the attribute template (rust-lang/reference#1879) - Update `cfg` to use the attribute template (rust-lang/reference#1878) - allow constants to refer to mutable/external memory, but reject such constants as patterns (rust-lang/reference#1859) - Remove outdated comment about non-copy unions (rust-lang/reference#1872) - Add a template for documenting attributes (rust-lang/reference#1877) - Switch enum grammar to use "variant" (rust-lang/reference#1876) ## rust-lang/rust-by-example 1 commits in 288b4e4948add43f387cad35adc7b1c54ca6fe12..e386be5f44af711854207c11fdd61bb576270b04 2025-07-04 23:17:15 UTC to 2025-07-04 23:17:15 UTC - Update Chinese translations (rust-lang/rust-by-example#1943)
Update books ## rust-lang/book 3 commits in ef1ce8f87a8b18feb1b6a9cf9a4939a79bde6795..b2d1a0821e12a676b496d61891b8e3d374a8e832 2025-07-08 17:24:41 UTC to 2025-07-02 21:30:57 UTC - Chapter 16 from tech review (rust-lang/book#4438) - WIP ch 17 edits after tech review (rust-lang/book#4319) - Chapter 15 from tech review (rust-lang/book#4433) ## rust-embedded/book 1 commits in 41f688a598a5022b749e23d37f3c524f6a0b28e1..fe88fbb68391a465680dd91109f0a151a1676f3e 2025-07-08 18:54:25 UTC to 2025-07-08 18:54:25 UTC - Clarify usage of #[interrupt] attribute and recommend device crate re… (rust-embedded/book#386) ## rust-lang/nomicon 3 commits in 8b61acfaea822e9ac926190bc8f15791c33336e8..3ff384320598bbe8d8cfe5cb8f18f78a3a3e6b15 2025-07-05 07:34:22 UTC to 2025-07-05 07:13:51 UTC - Add build script part to FFI chapter for more clear and smooth learn … (rust-lang/nomicon#440) - Cleanups for tree example of splitting borrows (rust-lang/nomicon#443) - Handle drop zst (rust-lang/nomicon#425) ## rust-lang/reference 17 commits in e9fc99f107840813916f62e16b3f6d9556e1f2d8..1f45bd41fa6c17b7c048ed6bfe5f168c4311206a 2025-07-11 23:15:51 UTC to 2025-07-01 16:49:33 UTC - mention an important use for the naked attribute (rust-lang/reference#1929) - Array expression repeat operands can be const blocks. (rust-lang/reference#1928) - Document (tuple) struct pattern namespace behavior (rust-lang/reference#1925) - Replace set of en dashes with set of em dashes (rust-lang/reference#1926) - Update `should_panic` to use the attribute template (rust-lang/reference#1882) - const-eval.const-expr.borrows: mention indirect places (rust-lang/reference#1865) - associated-items.md: remove redundant word (rust-lang/reference#1874) - introduction.md: replace hard-to-read example (rust-lang/reference#1873) - typo (rust-lang/reference#1924) - Update `ignore` to use the attribute template (rust-lang/reference#1881) - Update `test` to use the attribute template (rust-lang/reference#1880) - Update `cfg_attr` to use the attribute template (rust-lang/reference#1879) - Update `cfg` to use the attribute template (rust-lang/reference#1878) - allow constants to refer to mutable/external memory, but reject such constants as patterns (rust-lang/reference#1859) - Remove outdated comment about non-copy unions (rust-lang/reference#1872) - Add a template for documenting attributes (rust-lang/reference#1877) - Switch enum grammar to use "variant" (rust-lang/reference#1876) ## rust-lang/rust-by-example 1 commits in 288b4e4948add43f387cad35adc7b1c54ca6fe12..e386be5f44af711854207c11fdd61bb576270b04 2025-07-04 23:17:15 UTC to 2025-07-04 23:17:15 UTC - Update Chinese translations (rust-lang/rust-by-example#1943)
Update books ## rust-lang/book 3 commits in ef1ce8f87a8b18feb1b6a9cf9a4939a79bde6795..b2d1a0821e12a676b496d61891b8e3d374a8e832 2025-07-08 17:24:41 UTC to 2025-07-02 21:30:57 UTC - Chapter 16 from tech review (rust-lang/book#4438) - WIP ch 17 edits after tech review (rust-lang/book#4319) - Chapter 15 from tech review (rust-lang/book#4433) ## rust-embedded/book 1 commits in 41f688a598a5022b749e23d37f3c524f6a0b28e1..fe88fbb68391a465680dd91109f0a151a1676f3e 2025-07-08 18:54:25 UTC to 2025-07-08 18:54:25 UTC - Clarify usage of #[interrupt] attribute and recommend device crate re… (rust-embedded/book#386) ## rust-lang/nomicon 3 commits in 8b61acfaea822e9ac926190bc8f15791c33336e8..3ff384320598bbe8d8cfe5cb8f18f78a3a3e6b15 2025-07-05 07:34:22 UTC to 2025-07-05 07:13:51 UTC - Add build script part to FFI chapter for more clear and smooth learn … (rust-lang/nomicon#440) - Cleanups for tree example of splitting borrows (rust-lang/nomicon#443) - Handle drop zst (rust-lang/nomicon#425) ## rust-lang/reference 17 commits in e9fc99f107840813916f62e16b3f6d9556e1f2d8..1f45bd41fa6c17b7c048ed6bfe5f168c4311206a 2025-07-11 23:15:51 UTC to 2025-07-01 16:49:33 UTC - mention an important use for the naked attribute (rust-lang/reference#1929) - Array expression repeat operands can be const blocks. (rust-lang/reference#1928) - Document (tuple) struct pattern namespace behavior (rust-lang/reference#1925) - Replace set of en dashes with set of em dashes (rust-lang/reference#1926) - Update `should_panic` to use the attribute template (rust-lang/reference#1882) - const-eval.const-expr.borrows: mention indirect places (rust-lang/reference#1865) - associated-items.md: remove redundant word (rust-lang/reference#1874) - introduction.md: replace hard-to-read example (rust-lang/reference#1873) - typo (rust-lang/reference#1924) - Update `ignore` to use the attribute template (rust-lang/reference#1881) - Update `test` to use the attribute template (rust-lang/reference#1880) - Update `cfg_attr` to use the attribute template (rust-lang/reference#1879) - Update `cfg` to use the attribute template (rust-lang/reference#1878) - allow constants to refer to mutable/external memory, but reject such constants as patterns (rust-lang/reference#1859) - Remove outdated comment about non-copy unions (rust-lang/reference#1872) - Add a template for documenting attributes (rust-lang/reference#1877) - Switch enum grammar to use "variant" (rust-lang/reference#1876) ## rust-lang/rust-by-example 1 commits in 288b4e4948add43f387cad35adc7b1c54ca6fe12..e386be5f44af711854207c11fdd61bb576270b04 2025-07-04 23:17:15 UTC to 2025-07-04 23:17:15 UTC - Update Chinese translations (rust-lang/rust-by-example#1943)
Rollup merge of #143938 - rustbot:docs-update, r=ehuss Update books ## rust-lang/book 3 commits in ef1ce8f87a8b18feb1b6a9cf9a4939a79bde6795..b2d1a0821e12a676b496d61891b8e3d374a8e832 2025-07-08 17:24:41 UTC to 2025-07-02 21:30:57 UTC - Chapter 16 from tech review (rust-lang/book#4438) - WIP ch 17 edits after tech review (rust-lang/book#4319) - Chapter 15 from tech review (rust-lang/book#4433) ## rust-embedded/book 1 commits in 41f688a598a5022b749e23d37f3c524f6a0b28e1..fe88fbb68391a465680dd91109f0a151a1676f3e 2025-07-08 18:54:25 UTC to 2025-07-08 18:54:25 UTC - Clarify usage of #[interrupt] attribute and recommend device crate re… (rust-embedded/book#386) ## rust-lang/nomicon 3 commits in 8b61acfaea822e9ac926190bc8f15791c33336e8..3ff384320598bbe8d8cfe5cb8f18f78a3a3e6b15 2025-07-05 07:34:22 UTC to 2025-07-05 07:13:51 UTC - Add build script part to FFI chapter for more clear and smooth learn … (rust-lang/nomicon#440) - Cleanups for tree example of splitting borrows (rust-lang/nomicon#443) - Handle drop zst (rust-lang/nomicon#425) ## rust-lang/reference 17 commits in e9fc99f107840813916f62e16b3f6d9556e1f2d8..1f45bd41fa6c17b7c048ed6bfe5f168c4311206a 2025-07-11 23:15:51 UTC to 2025-07-01 16:49:33 UTC - mention an important use for the naked attribute (rust-lang/reference#1929) - Array expression repeat operands can be const blocks. (rust-lang/reference#1928) - Document (tuple) struct pattern namespace behavior (rust-lang/reference#1925) - Replace set of en dashes with set of em dashes (rust-lang/reference#1926) - Update `should_panic` to use the attribute template (rust-lang/reference#1882) - const-eval.const-expr.borrows: mention indirect places (rust-lang/reference#1865) - associated-items.md: remove redundant word (rust-lang/reference#1874) - introduction.md: replace hard-to-read example (rust-lang/reference#1873) - typo (rust-lang/reference#1924) - Update `ignore` to use the attribute template (rust-lang/reference#1881) - Update `test` to use the attribute template (rust-lang/reference#1880) - Update `cfg_attr` to use the attribute template (rust-lang/reference#1879) - Update `cfg` to use the attribute template (rust-lang/reference#1878) - allow constants to refer to mutable/external memory, but reject such constants as patterns (rust-lang/reference#1859) - Remove outdated comment about non-copy unions (rust-lang/reference#1872) - Add a template for documenting attributes (rust-lang/reference#1877) - Switch enum grammar to use "variant" (rust-lang/reference#1876) ## rust-lang/rust-by-example 1 commits in 288b4e4948add43f387cad35adc7b1c54ca6fe12..e386be5f44af711854207c11fdd61bb576270b04 2025-07-04 23:17:15 UTC to 2025-07-04 23:17:15 UTC - Update Chinese translations (rust-lang/rust-by-example#1943)
Reference update for rust-lang/rust#140942.