-
Notifications
You must be signed in to change notification settings - Fork 17
Towards dynamic const-qualify #27
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
Changes from all commits
a18aafc
a9c7047
50a1829
cbd7e7e
53d0200
7f8c81c
87c3c87
95acdf2
a29f147
1d2b7b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Statics | ||
|
||
Statics (`static`, `static mut`) are the simplest kind of compile-time evaluated data: | ||
* The user explicitly requested them to be evaluated at compile-time, | ||
so evaluation errors from computing the initial value of a static are no concern | ||
(in other words, [const safety](const_safety.md) is mostly not an issue). | ||
* They observably get evaluated *once*, with the result being put at some address known at run-time, | ||
so there are no fundamental restrictions on what statics can do. | ||
* The compiler checks that statics are `Sync`, justifying sharing their address across threads. | ||
* [Constants](const.md) and [promoteds](promotion.md) are not allowed to read from statics, | ||
so their final value does not have have to be [const-valid](const_safety.md#const-safety-check-on-values) in any meaningful way. | ||
As of 2019-08, we do check them for validity anyway, to be conservative; and indeed constants could be allowed to read from frozen statics. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be nice personally; less times to invent an ephemeral constant just to please the check and you also get to use exported statics from other crates. Any future compat hazards with some new feature? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discovered a problem... If we want to have a scheme like rust-lang/rfcs#2492 but centered around There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Centril sorry, I do not follow... why is it harder for separate compilation to support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case it would be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As long as whatever instantiates the |
||
|
||
## `Drop` | ||
|
||
The compiler rejects intermediate values (created and discarded during the computation of a static initializer) that implement `Drop`. | ||
The reason for this is simply that the `Drop` implementation might be non-`const fn`. | ||
This restriction can be lifted once `const impl Drop for Type` (or something similar) is supported. | ||
|
||
```rust | ||
struct Foo; | ||
|
||
impl Drop for Foo { | ||
fn drop(&mut self) { | ||
println!("foo dropped"); | ||
} | ||
} | ||
|
||
static FOOO: Foo = Foo; // Ok, drop is never run | ||
|
||
// Not ok, cannot run `Foo::drop` because it's not a const fn | ||
static BAR: i32 = (Foo, 42).1; | ||
``` | ||
|
||
*Dynamic check.* The Miri engine dynamically checks that this is done correctly | ||
by not permitting calls of non-`const` functions. |
Uh oh!
There was an error while loading. Please reload this page.