-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Implement constants using & but not ~ or @ #2317
Comments
Thought about this a bit more:
I understand this is a somewhat ragged line cut through the space of constant-ness, but I think it can be defended in a principled way: the compiler does what compilers are good at -- primitives of various sorts, read-only memory slabs and addresses -- and the libraries do the rest. This does mean that sizeof, alignof and offsetof, applied to rust types, remain "runtime" values, even though the compiler implements them as intrinsics and they wind up as emitted / inlined constants. Again, I think the main reason we'd want to have these functions evaluated much earlier -- i.e. "before types" -- has to do with C interop, and can be handled by a syntax extension that returns literals calculated by clang, but I could be wrong. Input welcome. |
… r=RalfJung move arc_drop test to miri-test-libstd That's where we have a bunch of slow and unlikely-to-regress tests already. Miri's test suite should primarily test Miri; we sometimes add libstd tests when they are cheap and/or exercise interesting code paths in Miri. rust-lang/miri#2248 already ensures that we allow code like `Arc` with more direct tests.
I think we have a reasonable answer for structured constant expressions now, which is to construct them using &-pointers rather than boxes. Boxes are for dynamic allocation only.
I believe issues #1272, #1417, #571 and #570 all relate to this and should probably be merged into this one. I think the correct course of action is to define two subsets of the expression language:
The compiler should have a pass that classifies each expression as either dynamic, constant, or integer-constant, based on its operators and operands.
Only constant expressions should be legal initializers for a
const
item. The compiler back-end can evaluate constant expressions to read-only memory using the LLVM constant instuctions.The compiler front-end can evaluate integer-constant expressions, in order to deduce array bounds, structure sizes, enumeration discriminators and similar "size-like" values. This is what we're currently using
eval_const_expr
for (in bug #1417) and it should be rigorously defined as part of the language. In particular, we might in the future permit integer-constant expressions to show up inside portions of the type grammar (particularly relating to constant sizes given for arrays).The compiler back-end should arrive at the exact same integer values as the front-end did, when translating an integer-constant expression.
We may also wish to look into a form of
const fn
as in C++0xconstexpr
functions. At this point I think it's effectively already reserved syntax for us so we can punt for now.It may also be useful to add sizeof(), alignof(), and similar "built-in" constant functions implemented using compiler intrinsics.
The text was updated successfully, but these errors were encountered: