-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Sizeof, alignof, offsetof, typeof #591
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,93 @@ | ||
- Start Date: 2015-01-17 | ||
- RFC PR #: (leave this empty) | ||
- Rust Issue #: (leave this empty) | ||
|
||
# Summary | ||
|
||
Add `sizeof`, `alignof` and `offsetof` operators similar to the ones found in C/C++. | ||
|
||
# Motivation | ||
|
||
- Currently it is not possible to use size / alignment of a type in const expressions. | ||
- It is not possible to find out size / alignment of a struct field without creating an instance of that struct. | ||
- The equivalent of C's `offsetof(struct, field)` is not supported at all. | ||
|
||
These are very annoying shortcomings for a "systems" language, so let's fix them! | ||
|
||
# Detailed design | ||
|
||
This RFC proposes to add the following operators: | ||
|
||
| Operator | Evaluates to... | | ||
|---------------------|------------------| | ||
|`sizeof(T)` |The size of type T.| | ||
|`alignof(T)` |The minimal alignment of type T.| | ||
|`offsetof(F in S)` |Offset of field F in struct S. F may be either an identifier for regular structs or an integer index for tuples and tuple structs.| | ||
|
||
These yield a value of type `usize` and may be used in any expression contexts. | ||
|
||
A more formal grammar: | ||
``` | ||
expr : ... | sizeof_expr | alignof_expr | offsetof_expr | ||
sizeof_expr : "sizeof" "(" type ")" | ||
alignof_expr : "alignof" "(" type ")" | ||
offsetof_expr : "offsetof" "(" struct_field "in" struct_type ")" | ||
struct_field : ident | integer | ||
``` | ||
|
||
Examples: | ||
```rust | ||
struct MyStruct { | ||
pub field1: i32, | ||
pub field2: f64 | ||
} | ||
|
||
sizeof(MyStruct) == 16; | ||
alignof(MyStruct) == 8; | ||
offsetof(field2 in MyStruct) == 8; | ||
``` | ||
|
||
# Alternatives | ||
|
||
- Beef up rustc's constant folder, so it can see though \<raw-pointer-deref\>-\<field-select\>-\<get-address-of\> combos. It would then be possible to express `sizeof` and `offsetof` in terms of raw pointer operations. For example: `macro_rules! offsetof(($f:ident in $t:ty) => (unsafe{ &(*(0 as *const $t)).$f as usize }));` | ||
Cons: The implementations of these operators via pointer arithmetic use dubious tricks like dereferencing a null pointer, and may become broken by future improvements in LLVM optimizations. Also, this approach seems to be falling out of fashion even in C/C++ compilers (I guess for the same reason): `alignof`is now a standard operator in C++11; the `offsetof` hasn't been standartized yet, but both GCC and clang implement it as a custom built-in. | ||
|
||
- Implement a limited form of compile-time function evaluation (CTFE) by hard-coding knowledge of intrinsics such as `size_of<T>()` and `align_of<T>()` into the constant folder. | ||
Cons: | ||
- There is no syntax in Rust for referring to a struct field, other than in assignment context, so the equivalent of C++'s `sizeof Struct::field` cannot be expressed in it. | ||
- For `offsetof` this is an even bigger problem, because its core functionality requires referring to the field, whose offset we want to determine. We could represent it as a string, i.e. `offset_of<T>(field: &str)`, but then what do we do in non-const contexts? If we allow arbitrary string parameters, we'd have to bring back runtime reflection. If we don't, it would be the only function in the language that *requires* a literal parameter. | ||
|
||
# Unresolved questions | ||
|
||
- Should we allow usage of private struct fields with `offsetof` or only of the public ones? | ||
- Can these operators be made to work in array length expressions (and eventually in numeric type parameters,- when these get implemented)? The trouble is that array lengths need to be known in the type checking phase, however the physical data type layouts are known only to LLVM in trans phase. | ||
|
||
|
||
## Syntax bikeshedding | ||
|
||
The above syntax looks very similar to regular function calls. Some alternatives: | ||
- Macro-like syntax: `sizeof!(MyStruct); alignof!(f64); offsetof!(field2 in MyStruct))`. | ||
Pros: distinct from regular functions; "non-standard" syntax is par for the course in macros; it's immediately obvious that these are evaluated at compile time. | ||
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. We have some compiler-builtin macroses already, so adding a few new ones would not be too bad IMO. And they can still be expanded to their literal value. That being said, I think I’d prefer the macro version. |
||
Cons: these macros would be "un-expandable", because there'd be no syntax to expand them to. | ||
- Something completely new? `sizeof#(MyStruct); offsetof#(field2 in MyStruct)`? `sizeof<(MyStruct)>; offsetof<(MyStruct, field2)>`? ... | ||
|
||
# Future directions | ||
|
||
Add `typeof` operator for finding out the type of an expression or a struct field. This would allow Rust to have parity with C/C++ `sizeof` operator that accepts both type and value arguments. | ||
|
||
| Operator | Evaluates to... | | ||
|---------------------|------------------| | ||
|`typeof(F in S)` |The type of field F in struct S. F may be either an identifier for regular structs or an integer index for tuples and tuple structs.| | ||
|`typeof(E)` |The type of expression E.| | ||
|
||
|
||
Examples: | ||
``` | ||
sizeof(typeof(field1 in MyStruct)) == 4; | ||
|
||
let mystruct = MyStruct { field1:1, field2:2 }; | ||
sizeof(typeof(mystruct.field1)) == 4; | ||
|
||
sizeof(typeof(1 + 2)) == 4; | ||
sizeof(typeof(1 in (i32, f64))) == 8; | ||
``` |
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.
do we need preferred alignment as well as minimal alignment?