Skip to content
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

Can derive default analysis #861

Merged
merged 1 commit into from
Jul 31, 2017
Merged

Can derive default analysis #861

merged 1 commit into from
Jul 31, 2017

Conversation

photoszzt
Copy link
Contributor

@photoszzt photoszzt commented Jul 27, 2017

r? @fitzgen Fix: #856

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! I left a bunch of comments, but its mostly just nitpicking.

src/ir/layout.rs Outdated
type Extra = ();

fn can_derive_default(&self, _: &BindgenContext, _: ()) -> bool {
fn can_trivially_derive_default(&self, _: &BindgenContext, _: ()) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like this is the only impl, and since it is not using the Extra associated type, nor the ctx parameter, we should remove them.

pub struct Outer {
pub i: u8,
}
impl Default for Outer {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expectation change should be fine.

-> bool {
// We can reach here recursively via template parameters of a member,
// for example.
if self.detect_derive_default_cycle.get() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also remove the detect_derive_default_cycle member now.

pub struct e<c> {
pub d: RefPtr<c>,
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<c>>,
}
impl <c> Default for e<c> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expectation change seems to be from how if a type is hidden/blacklisted it will never end up inside this analysis's worklist because it isn't in the whitelisted items set, and therefore will never end up in the cannot_derive_default set. This means that our default is "can derive" for these types.

It isn't clear to me what the old default was; I think we would have looked inside the blacklisted item, which I'm not sure is what we want either. The type is blacklisted for some reason or another, and bindgen doesn't have the whole picture so how can it make an opinionated decision?

In light of that, I think the best thing to do would be to act conservatively, similar to how we do with blacklisted items in the template parameter analysis. With template params, we conservatively say that blacklisted items use all of their template parameters. In this case, conservative means "assume it cannot derive Default if it is blacklisted". As far as implementing this behavior goes, it means that we need to initially populate cannot_derive_default with all the items that are reachable from the whitelisted item set, but are not actually in the whitelisted item set (in other words, populate it with the blacklisted items).

@emilio wdyt? Does defaulting to "we should conservatively deny deriving default for blacklisted items" sound right to you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds fine off-hand... In general note that deriving default is not on by default, so it's less likely to break stuff. As long as we keep it working and consistent I think i'd be fine with it.

pub struct HasRefPtr<T> {
pub refptr_member: RefPtr<HasRefPtr_TypedefOfT<T>>,
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
pub type HasRefPtr_TypedefOfT<T> = T;
impl <T> Default for HasRefPtr<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same thing as above.

match *ty.kind() {
// Handle the simple cases. These can derive debug without further
// information.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: remove this newline.


TypeKind::Void |
TypeKind::Named |
TypeKind::TemplateInstantiation(..) |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, as mentioned on IRC, I think a template instantiation should derive default if its definition and arguments derive default.

if info.needs_explicit_vtable(self.ctx, item) {
trace!(" comp that needs vtable cannot derive Default");
return self.insert(id);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ever the case that a type can need an explicit vtable but not be in the has_vtable set??

I think this case is redundant...

}
}

if self.ctx.lookup_item_id_has_vtable(&item.id()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick:

if item.has_vtable(self.ctx) {
    ...
}

src/ir/item.rs Outdated
}
_ => false,
}
ctx.options().derive_default && ctx.lookup_item_id_can_derive_default(self.id())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there should only be two CanDeriveDefault impls left: Item and ItemId and neither use the Extra type parameter, so we should remove it now.

@bors-servo
Copy link

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

@fitzgen
Copy link
Member

fitzgen commented Jul 31, 2017

@bors-servo r+

@bors-servo
Copy link

📋 Looks like this PR is still in progress, ignoring approval

@fitzgen fitzgen changed the title WIP can derive default analysis Can derive default analysis Jul 31, 2017
@fitzgen
Copy link
Member

fitzgen commented Jul 31, 2017

@bors-servo r+

@bors-servo
Copy link

📌 Commit 0231a21 has been approved by fitzgen

@bors-servo
Copy link

⌛ Testing commit 0231a21 with merge d1d3500...

bors-servo pushed a commit that referenced this pull request Jul 31, 2017
@bors-servo
Copy link

☀️ Test successful - status-travis
Approved by: fitzgen
Pushing d1d3500 to master...

@bors-servo bors-servo merged commit 0231a21 into rust-lang:master Jul 31, 2017
This was referenced Jul 31, 2017
@photoszzt photoszzt deleted the derive_default branch July 31, 2017 20:30
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.

4 participants