-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add DatatypeDef #22564
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
Add DatatypeDef #22564
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see CONTRIBUTING.md for more information. |
I would prefer a name like |
let ty = match ty::expr_ty_opt(cx.tcx, e) { | ||
Some(ty) => ty, | ||
None => cx.tcx.sess.span_bug(e.span, "No type for expression"), | ||
}; |
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.
Doesn't/can't ty::expr_ty
handle this?
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.
Ah right, that reminds me, I was going to fold the span_bug
stuff into ty::expr_ty
. Makes debugging easier.
LGTM modulo some nits, but I'd be happy with a second pair of eyes. |
(I'm probably not the right reviewer for this) |
Sorry for the delay. I somehow missed this PR. |
|
||
pub fn ty(&self) -> Ty<'tcx> { | ||
self.ty.get() | ||
} |
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.
The current setup, iiuc, initially stores a ty_err and then updates it to the actual type. This seems like it will hide bugs where we read the type of a field before it is fully initialized. I would prefer to use Cell<Option<Ty<'tcx>>>
and initially set it to None
. Then ty
can call self.ty.get().unwrap()
, which will give the same (stronger) guarantees we get today. Similarly set_ty
can check that the type is set exactly once. (There is no space inefficiency because Ty<'tcx>
is a non-nullable-pointer.)
ok, I have to run for the moment. I'm a big fan of this general approach. I've been wanting to see the struct/enum code made nicer (and unified!) for a long time, and I think moving away from storing all the information in distinct side-tables is generally a good idea. I've only had time though to read over the high-level details of what is here, so I'll want some more time to soak in finer points. |
ty::ty_struct(ctor_id, _) => { | ||
// RFC 736: ensure all unmentioned fields are visible. | ||
// Rather than computing the set of unmentioned fields | ||
// (i.e. `all_fields - fields`), just check them all. |
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.
Nit: Please don't drop this comment.
I think I'd like to see the /// A "write once" cell.
pub struct Ivar<T:Copy> {
data: Cell<Option<T>>
}
impl<T:Copy> Ivar<T> {
pub fn get(&self) -> Option<T> {
self.data.get()
}
pub fn fulfill(&self, value: T) {
assert!(self.data.get().is_none());
self.data.set(value);
}
pub fn unwrap(&self) -> T {
self.get().unwrap()
}
} This has the advantage that (a) privacy ensures that we will only write to the cell once and (b) it can, in the future, be made |
I've squashed all the previous commits since rebasing was getting difficult due to functions being removed/replaced and me having to manually re-remove them post-rebase. I have also added the |
encode_struct_fields(rbml_w, &fields[..], def_id); | ||
/* Encode def_ids for each field and method | ||
for methods, write all the stuff get_trait_method | ||
needs to know*/ |
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.
Pre-existing,but this comment formatting is terrible.
@Aatch I commented on a few points that were made before but not corrected in your latest rebase. Overall I'm pretty happy with this. One thing I was wondering -- perhaps it would make sense to merge |
Modulo those nits though I'm ready to r+. |
data: Cell<Option<T>> | ||
} | ||
|
||
impl<T:Copy> Ivar<T> { |
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 personally know what an "I-var" is, but it might be nice to either have some comments here just saying what the big picture is about them, or a pointer to http://en.wikipedia.org/wiki/Futures_and_promises or something.
(This comment should have been on the struct definition itself)
@nikomatsakis yeah, I looked at merging them into a |
Add a DatatypeDef type that contains information for both structs and enums. Most places already treated structs like single-variant enums or vice-versa, so this actually simplifies a lot of code.
Adds an Ivar for write-once variables, and makes DatatypeDef use it. Removes some now-unused arguments.
Re-add removed comment in librustc_privacy Remove `vis` field from `VariantDef` Fixup weirdly-formatted comments in metadata::encoder
Documents `Ivar` and adds some common trait implementations for it
r+ from me, but of course there are tidy errors it looks like |
☔ The latest upstream changes (presumably #22801) made this pull request unmergeable. Please resolve the merge conflicts. |
There's a lot of |
I was thinking about |
@nikomatsakis sounds like an "OnceCell". |
@eddyb I am not 100% sure I would apply the |
@pnkfelix fair enough, I was just trying to force it into |
@Aatch do you plan to rebase this? |
@Aatch would you object if I rebased this? |
@nikomatsakis go for it. Sorry, I've been really busy with work + life recently, not much time for compiler-hacking. |
@Aatch I started it. Lots of conflicts (of course). I might just try to cherry-pick and re-implement instead...gotta finish a few other things first. |
Closing in favor of @nikomatsakis's upcoming rebase |
Add a DatatypeDef type that contains information for both structs and enums. Most places already treated structs like single-variant enums or vice-versa, so this actually simplifies a lot of code.
There is a small but significant performance increase in typechecking and translation (pre-optimisations) of about 5% in both phases. All the other phases seemed to have some performance improvements too, but the differences were too small to be meaningful.
This change is also a step towards overall simplification of the type system code. Given how many places are now treating enums and structs completely identically, a move towards a unified
ty_data
type is somewhat feasible.