Repository navigation
Tracking Issue for const_refs_to_static #119618
Description
This is a tracking issue for consts referencing statics, a @rust-lang/wg-const-eval experiment.
The feature gate for the issue is #![feature(const_refs_to_static)].
This feature allow constants to refer to statics. However, reading from a static that is mutable (static mut, or static with interior mutability) leads to an evaluator error. This is done to ensure that it doesn't matter when the constant is evaluated, it will always produce the same result.
Having a reference to a mutable static in the final value leads to a validation error. This is necessary to ensure that converting these references to a valtree (e.g. for pattern matching) will not error. (The conversion would error because valtree conversion must never read from anything mutable.)
The same goes for reading from or having a reference to extern static; those obviously can't be read as we can't know their value at compile time. Mutating any static is also not possible -- we can't have global mutable state shared between const-eval queries.
About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Steps
- Implementation
- Adjust documentation (see instructions on rustc-dev-guide)
- Formatting for new syntax has been added to the Style Guide (nightly-style-procedure)
- Stabilization proposal
- Stabilization PR
- Update the Reference
Unresolved Questions
- When a constant refers to or reads from mutable memory, this is currently rejected at type-check time. With this feature it is instead rejected at const-eval time, which can be during monomorphization. However, that's not fundamentally different from all the other ways in which const-eval can fail post-monomorphization.
- What about the interaction with reference-typed const generics?
Implementation history
This issue has been assigned to @dingxiangfei2009 via this comment.
Activity
RalfJung commentedon Feb 5, 2024
@ojeda I saw you added this to the Rust-for-Linux wishlist. Could you say a bit more about the intended usecase? Last time this came up, it turned out that you actually need #118447, not consts that point to statics. So I wonder if maybe this time there also is a possible alternative.
Darksonn commentedon Feb 8, 2024
My understanding is that many of the vtables in the kernel have the following shape:
We do not need to read or write to
THIS_MODULEat const evaluation time. In fact, since it's an extern static, it doesn't really make sense to even talk about doing that.RalfJung commentedon Feb 10, 2024
Your code literally reads the value of
THIS_MODULE(an extern static) to create the vtable. That's impossible to do in a const (or static) initializer, and I don't know any way to allow this.Or did you mean
addr_of_mut!(T::Mod::THIS_MODULE)?RalfJung commentedon Feb 10, 2024
Oh wait, you have multiple items with the same name in different modules. That's very confusing code. I'll have to de-obfuscate it first.^^
RalfJung commentedon Feb 11, 2024
I added your testcase in #120932, it then passes with
feature(const_mut_refs, const_refs_to_static). :)dtolnay commentedon Apr 2, 2024
Potential use case in the standard library:
rust/library/core/src/task/wake.rs
Lines 492 to 495 in 5dbaafd
By using
const_refs_to_staticin the standard library and changingWAKERto a static, one can usewaker.will_wake(Waker::noop())orWaker::noop().will_wake(&waker)to determine whether a particular waker is noop.will_wakeis based on pointer identity for the waker's data ptr and vtable ptr.RalfJung commentedon Apr 2, 2024
There's no mutability here that I can see, so yeah that would work.
Basically it's a trade-off between a lower risk of monomorphization-time errors and letting
constto more things. With const_refs_to_static you get a mono-time error if you ever read from or (transitively) have a reference to mutable statics (this includes interior mutability) or extern statics.rust: upgrade to Rust 1.78.0
nikomatsakis commentedon Jun 8, 2024
What is the current status here -- in particular, the Rust For Linux project makes use of this feature to support creation of vtables (example code) and I am hoping that in the next 6 months we can move their subset to stabilization. @RalfJung or @oli-obk -- can you enlighten me as to what the key blockers and/or questions are?
EDIT: Oh, that example appears earlier in the thread -- right, I forgot, we have already added a testcase to ensure that it continues to compile.
EDIT: Created a Zulip thread to discuss.
RalfJung commentedon Jun 8, 2024
Yeah, the feature was added specifically to support that example. ;)
Main concerns/blockers I can think of:
3 remaining items
oli-obk commentedon Jun 21, 2024
Yes, array lengths are constants, so the feature is required for that
Pzixel commentedon Jun 21, 2024
Note that it is currently possible to declare a
const fnwhich mutates static variable. Which leads to a function which is declared asconst fnbut actually cannot be called in const context. E.g.https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=de9d10f90ca407933fd2c10ae85556b6
If we now replace
with
It won't compile. Which means that
constmodifier on this function does nothing and shouldn't be allowed.RalfJung commentedon Jun 21, 2024
We could detect some simple cases but in general it is your responsibility, as part of writing unsafe code, to ensure this does not happen. Consider that one can take a reference or raw pointer to a
static mutand then read through that. It's fine to create the reference, and by the time you read through it we can't know whether it points to astatic mutor not.The
constdoes something, but it can't check everything. Some things are only checked at "runtime" (i.e. during const-eval), at least with const_refs_to_static.fix: ExternalOneByteStringResource is not guaranteed to be valid UTF-8 (
nikomatsakis commentedon Jul 25, 2024
Stabilization proposal:
#128183
nikomatsakis commentedon Aug 28, 2024
Seeing as #128183 is in FCP, I'm assigning @dingxiangfei2009 to prepare the stabilization PR:
@rustbot assign @dingxiangfei2009
Rollup merge of rust-lang#129759 - dingxiangfei2009:stabilize-const-r…
RalfJung commentedon Dec 14, 2024
This is stable and documented, so we can close the tracking issue. :)
rust: upgrade to Rust 1.78.0
rust: upgrade to Rust 1.78.0
fix(AN8-B): cpu.rs static-in-const ref breaks Rust 1.82 MSRV (CI fail…
fix(msrv,readme): unblock 1.82 MSRV + sync test count
feat(perf): Criterion microbench harness (#54) (#92)