Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: Keyword unreservations (pure, sizeof, alignof, offsetof) #2421
Conversation
Centril
added
T-lang
I-nominated
labels
Apr 26, 2018
Centril
reviewed
Apr 26, 2018
| *"go look at `std::mem::size_of` instead"*. However, we believe it is better | ||
| to allow users the freedom to use these keywords instead. | ||
|
|
||
| ## Rationale for `priv` |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Centril
Apr 26, 2018
Author
Contributor
@petrochenkov Could you elaborate a lil bit on possible use cases you foresee?
This comment has been minimized.
This comment has been minimized.
joshtriplett
Apr 26, 2018
Member
Considering how much activity we have around module proposals, including visibility, I think holding onto this one seems reasonable for now.
This comment has been minimized.
This comment has been minimized.
Centril
Apr 26, 2018
•
Author
Contributor
Aight. As we discussed on the lang team meeting; I'll scratch this one of the list in this round of unreservations. We can always revisit later.
Centril
reviewed
Apr 26, 2018
| In both 1. and 2., `pure` can be contextual. | ||
| We also don't think that the drawbacks are significant for `pure`. | ||
|
|
||
| ## Rationale for `unsize` |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
mikeyhew
Apr 26, 2018
unsized may be useful for custom DST, so I think it's worth holding onto for now
This comment has been minimized.
This comment has been minimized.
Centril
Apr 26, 2018
•
Author
Contributor
@mikeyhew Can you elaborate a bit? (possibly with a code example?)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
eddyb
Apr 27, 2018
Member
I'd use dyn for dynamic existentials, not dynamic sizes, e.g. you could imagine [T] expanding to dyn<n: usize> [T; n] and dyn Trait to dyn<T: Trait> T.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Centril
Apr 28, 2018
Author
Contributor
I've now scratched unsized given @mikeyhew's motivation and that unreserving it violates:
[..] we are sure that we have no intention of using the keyword in the future [..]
This comment has been minimized.
This comment has been minimized.
eddyb
Apr 30, 2018
Member
@Centril That's not a size, that's a length. Dynamic existential shorthands (when each bound type/value name is used only once, in an unambiguous way) could be dyn Trait and [T; dyn usize] (or just dyn, since the type can be inferred or specified elsewhere).
This comment was marked as resolved.
This comment was marked as resolved.
|
@withoutboats definitely did want to keep |
This comment has been minimized.
This comment has been minimized.
|
I don't want to keep |
This comment has been minimized.
This comment has been minimized.
|
@joshtriplett I dislike having macros that expand to builtins that cannot be written with other syntax, so I'm curious which approach you're looking for. I guess using an EDIT: I've marked the remaining |
This comment was marked as off-topic.
This comment was marked as off-topic.
I hope that proc macros one day become powerful enough to implement this. :) Until that time, I don't have a problem with builtin macros that can't today be written with other syntax. |
Centril
removed
the
I-nominated
label
Apr 26, 2018
This comment was marked as off-topic.
This comment was marked as off-topic.
I don't see how that could possibly be the case, as "offset of" requires layout, which is strictly staged after syntax expansion. OTOH, this should work, if we put it in macro_rules! offset_of {
($Struct:path, $field:ident) => ({
// Using a separate function to minimize unhygienic hazards
// (e.g. unsafety of #[repr(packed)] field borrows).
// Uncomment `const` when `const fn`s can juggle pointers.
/*const*/ fn offset() -> usize {
let u = $crate::mem::MaybeUninit::<$Struct>::uninit();
// Use pattern-matching to avoid accidentally going through Deref.
let &$Struct { $field: ref f, .. } = unsafe { &*u.as_ptr() };
let o = (f as *const _ as usize).wrapping_sub(&u as *const _ as usize);
// Triple check that we are within `u` still.
assert!((0..=$crate::mem::size_of_val(&u)).contains(&o));
o
}
offset()
})
} |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
@eddyb Two other questions: 1) Will that optimize down to a compile-time constant? 2) Does that still hold true if you use |
rkruppe
reviewed
Apr 26, 2018
| ## Rationale for `sizeof`, `alignof`, and `offsetof` | ||
|
|
||
| We already have [`std::mem::size_of`](https://doc.rust-lang.org/nightly/std/mem/fn.size_of.html) and similar which | ||
| are `const fn`s or can be. |
This comment has been minimized.
This comment has been minimized.
rkruppe
Apr 26, 2018
•
Member
offsetof can't be a const fn, at least not without some sort of static reflection being added to the language – which is a huge feature and AFAIK nothing specific in this direction has ever been proposed. I agree it could be unreserved (and implemented as a macro, as @eddyb showed), but this rationale here only applies to sizeof and alignof, not offsetof.
This comment has been minimized.
This comment has been minimized.
eddyb
Apr 26, 2018
Member
Note that I would prefer if the macro generated a const fn, so you could use it anywhere a constant value was expected. Most of it will soon become possible, with the exception of the assert, although that's mostly a sanity check and shouldn't be required for soundness.
This comment has been minimized.
This comment has been minimized.
joshtriplett
Apr 26, 2018
Member
@rkruppe In theory we could know the delta between fields of the same structure (or from the start of the structure to one field) statically at compile time, without needing full static reflection. And many uses of offsetof in C want it in a compile-time-constant context..
This comment has been minimized.
This comment has been minimized.
rkruppe
Apr 26, 2018
Member
@joshtriplett What you describe sounds more like a special keyword than a const fn! I don't see a nice, generic (i.e., which enables more than just const fn offsetof) extension to current Rust that allows one to write const fn offsetof in a library. Well, nothing that's smaller/simpler than the beginnings of a reflection system. Once you start introducing things like "reifying a field into a value and passing it to a function" (which seems necessary to even define the interface of const fn offsetof, let alone its implementation) you're already in reflection land.
(I agree that offset calculations need to work in constant contexts, but as @eddyb said, the macro is perfectly compatible with that!)
This comment has been minimized.
This comment has been minimized.
This comment was marked as off-topic.
This comment was marked as off-topic.
|
@joshtriplett |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
@eddyb Only with In this case, I just wanted to know if it affected code generation. And my concern was that that particular union approach presumes a certain semantics of unions, which is still under active discussion. Is there already a proposal for |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
@joshtriplett No, EDIT: both |
This comment has been minimized.
This comment has been minimized.
|
Update: I've removed |
Centril
added some commits
Apr 26, 2018
This comment was marked as resolved.
This comment was marked as resolved.
|
@joshtriplett Yeah; an internals thread would be best :) |
scottmcm
reviewed
Apr 27, 2018
text/0000-unreservations-2018.md Outdated
scottmcm
reviewed
Apr 27, 2018
text/0000-unreservations-2018.md Outdated
Centril
added some commits
Apr 27, 2018
Centril
changed the title
RFC: Keyword unreservations (pure, unsize, sizeof, alignof, offsetof, priv)
RFC: Keyword unreservations (pure, sizeof, alignof, offsetof)
Apr 30, 2018
This comment was marked as off-topic.
This comment was marked as off-topic.
|
I think that we should aim for proper Returning a proper constant is not possible with a normal macro implementation, however we could make it a "magic" macro like |
bjorn3
reviewed
May 2, 2018
text/0000-unreservations-2018.md Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
You mean because stack addresses are converted to integers? IMO this shouldn't be a problem because miri could understand that EDIT: For the record, I've had |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
Yea you should be able to write offset_of on nightly/beta by using int to ptr casts |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
@oli-obk AFAIK that's UB in the general case, #2421 (comment) uses a local |
This comment has been minimized.
This comment has been minimized.
|
Could we discuss the implementation detail / UB-ness of As mentioned in #2421 (comment) and #2421 (comment), even if it turns out miri can't obtain the field offset, |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
Hmm. OK. maybe we need rust-lang/rust#49172 first. Then again, if you only do it in a constant, is it really UB? :D miri has very clear semantics for this. |
This comment has been minimized.
This comment has been minimized.
|
I made a topic for it: https://internals.rust-lang.org/t/discussion-on-offset-of/7440 Just to double check: Everyone is fine with unreserving |
This comment has been minimized.
This comment has been minimized.
|
Based on discussion in the lang team meeting, we're ready to unreserve the remaining keywords in this RFC. @rfcbot fcp merge |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
May 3, 2018
•
|
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
rfcbot
added
proposed-final-comment-period
disposition-merge
labels
May 3, 2018
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
May 17, 2018
|
|
rfcbot
added
final-comment-period
and removed
proposed-final-comment-period
labels
May 17, 2018
brennie
suggested changes
May 23, 2018
text/0000-unreservations-2018.md Outdated
Centril
added some commits
May 23, 2018
brennie
approved these changes
May 24, 2018
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
May 27, 2018
|
The final comment period, with a disposition to merge, as per the review above, is now complete. |
rfcbot
added
finished-final-comment-period
and removed
final-comment-period
labels
May 27, 2018
Centril
referenced this pull request
May 27, 2018
Closed
Tracking issue for RFC 2421, "Keyword unreservations (pure, sizeof, alignof, offsetof)" #51115
Centril
merged commit 7a2ce51
into
rust-lang:master
May 27, 2018
This comment has been minimized.
This comment has been minimized.
|
Huzzah! This RFC is merged! Tracking issue: rust-lang/rust#51115 |
Centril commentedApr 26, 2018
•
edited
We unreserve:
pureunsizedsizeofalignofoffsetofprivIf you have a particular objection and use case for some keyword this RFC wants to unreserve, please speak up and leave a comment.