Skip to content

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

Closed
wants to merge 5 commits into from
Closed

Add DatatypeDef #22564

wants to merge 5 commits into from

Conversation

Aatch
Copy link
Contributor

@Aatch Aatch commented Feb 20, 2015

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.

@rust-highfive
Copy link
Contributor

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.

@eddyb
Copy link
Member

eddyb commented Feb 20, 2015

I would prefer a name like DataType, but that's... bikeshedding.
cc @nikomatsakis @nick29581

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"),
};
Copy link
Member

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?

Copy link
Contributor Author

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.

@eddyb
Copy link
Member

eddyb commented Feb 20, 2015

LGTM modulo some nits, but I'd be happy with a second pair of eyes.

@alexcrichton
Copy link
Member

r? @nikomatsakis

(I'm probably not the right reviewer for this)

@nikomatsakis
Copy link
Contributor

Sorry for the delay. I somehow missed this PR.


pub fn ty(&self) -> Ty<'tcx> {
self.ty.get()
}
Copy link
Contributor

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.)

@nikomatsakis
Copy link
Contributor

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.
Copy link
Contributor

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.

@nikomatsakis
Copy link
Contributor

I think I'd like to see the Cell<Option<Ty<'tcx>>> replaced with an Ivar<Ty<'tcx>>, where Ivar would be a type in librustc/middle/util/ivar.rs:

/// 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 Sync (we'd have to use atomic writes).

@Aatch
Copy link
Contributor Author

Aatch commented Feb 26, 2015

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 Ivar type and used it for DatatypeDef

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*/
Copy link
Contributor

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.

@nikomatsakis
Copy link
Contributor

@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 ty_struct and ty_enum into ty_data or something like that. Just something to think about, for now.

@nikomatsakis
Copy link
Contributor

Modulo those nits though I'm ready to r+.

data: Cell<Option<T>>
}

impl<T:Copy> Ivar<T> {
Copy link
Member

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)

@Aatch
Copy link
Contributor Author

Aatch commented Feb 27, 2015

@nikomatsakis yeah, I looked at merging them into a ty_data, but it's a non-trivial change on top of this already fairly extensive change. Definitely something to do in the near future though.

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
@nikomatsakis
Copy link
Contributor

r+ from me, but of course there are tidy errors it looks like

@bors
Copy link
Collaborator

bors commented Feb 28, 2015

☔ The latest upstream changes (presumably #22801) made this pull request unmergeable. Please resolve the merge conflicts.

@Ms2ger
Copy link
Contributor

Ms2ger commented Feb 28, 2015

There's a lot of &x[] slicing syntax, that I believe to be deprecated.

@nikomatsakis
Copy link
Contributor

I was thinking about Ivar and I remembered that as I originally envisioned it, Ivar wouldn't require T:Copy but, rather, would take advantage of the fact that because it is only set once, it can return an &T to the contents even without using a RefCell (this would require unsafe code, of course). This doesn't have to go in the PR, I'm just noting it for future reference. ;)

@eddyb
Copy link
Member

eddyb commented Feb 28, 2015

@nikomatsakis sounds like an "OnceCell".

@pnkfelix
Copy link
Member

pnkfelix commented Mar 1, 2015

@eddyb I am not 100% sure I would apply the Cell terminology here, since part of the point is that these things do not start with an initial value, while all of the current kinds of cells do start with an initial value, right?

@eddyb
Copy link
Member

eddyb commented Mar 1, 2015

@pnkfelix fair enough, I was just trying to force it into std::cell, but it probably fits better somewhere in std::sync.

@nikomatsakis
Copy link
Contributor

@Aatch do you plan to rebase this?

@nikomatsakis
Copy link
Contributor

@Aatch would you object if I rebased this?

@Aatch
Copy link
Contributor Author

Aatch commented Apr 17, 2015

@nikomatsakis go for it. Sorry, I've been really busy with work + life recently, not much time for compiler-hacking.

@nikomatsakis
Copy link
Contributor

@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.

@alexcrichton
Copy link
Member

Closing in favor of @nikomatsakis's upcoming rebase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants