-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow blocks in constants #71
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
- Start Date: 2014-05-07 | ||
- RFC PR #: (leave this empty) | ||
- Rust Issue #: (leave this empty) | ||
|
||
# Summary | ||
|
||
Allow block expressions in statics, as long as they only contain items | ||
and a trailing const expression. | ||
|
||
Example: | ||
|
||
```rust | ||
static FOO: uint = { 100 }; | ||
static BAR: fn() -> int = { | ||
fn hidden() -> int { | ||
42 | ||
} | ||
hidden | ||
}; | ||
``` | ||
|
||
# Motivation | ||
|
||
This change allows defining items as part of a const expression, | ||
and evaluating to a value using them. | ||
This is mainly useful for macros, as it allows hiding complex machinery behind something | ||
that expands to a value, but also enables using `unsafe {}` blocks in a static initializer. | ||
|
||
Real life examples include the `regex!` macro, which currently expands to a block containing a | ||
function definition and a value, and would be usable in a static with this. | ||
|
||
Another example would be to expose a static reference to a fixed memory address by | ||
dereferencing a raw pointer in a const expr, which is useful in | ||
embedded and kernel, but requires a `unsafe` block to do. | ||
|
||
The outcome of this is that one additional expression type becomes valid as a const | ||
expression, with semantics that are a strict subset of its equivalent in a function. | ||
|
||
# Drawbacks | ||
|
||
Block expressions in a function are usually just used to run arbitrary code before | ||
evaluating to a value. Allowing them in statics without allowing code | ||
execution might be confusing. | ||
|
||
# Detailed design | ||
|
||
A branch implementing this feature can be found at | ||
https://github.com/Kimundi/rust/tree/const_block. | ||
|
||
It mainly involves the following changes: | ||
|
||
- const check now allows block expressions in statics: | ||
- All statements that are not item declarations lead to an compile error. | ||
- trans and const eval are made aware of block expressions: | ||
- A trailing expression gets evaluated as a constant. | ||
- A missing trailing expressions is treated as a unit value. | ||
- trans is made to recurse into static expressions to generate possible items. | ||
|
||
Things like privacy/reachability of definitions inside a static block | ||
are already handled more generally at other places, as the situation is | ||
very similar to a regular function. | ||
|
||
The branch also includes tests that show how this feature works in practice. | ||
|
||
# Alternatives | ||
|
||
Because this feature is a straight forward extension of the valid const expressions, | ||
it already causes a very minimal impact on the language, with most alternative ways | ||
of enabling the same benefits being more complex. | ||
|
||
For example, a expression AST node that can include items but is only usable from procedural macros | ||
could be added. | ||
|
||
Not having this feature would not prevent anything interesting from getting implemented, | ||
but it would lead to less nice looking solutions. | ||
|
||
For example, a comparison between static-supporting `regex!` with and without this feature: | ||
|
||
```rust | ||
// With this feature, you can just initialize a static: | ||
static R: Regex = regex!("[0-9]"); | ||
|
||
// Without it, the static needs to be generated by the | ||
// macro itself, alongside all generated items: | ||
regex! { | ||
static R = "[0-9]"; | ||
} | ||
``` | ||
|
||
# Unresolved questions | ||
|
||
None so far. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think dereferencing pointers is legal in blocks (although I guess one could just regard this as an extension).
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.
See rust-lang/rust#13973 - if that's a bug that gets fixed by not allowing dereferences in a static then the example indeed becomes invalid.