-
Notifications
You must be signed in to change notification settings - Fork 717
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
Conversation
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.
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 { |
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.
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() } } | ||
} |
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.
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() { |
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.
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() } } | ||
} |
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.
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?
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.
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() } } | ||
} |
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.
This is the same thing as above.
src/ir/analysis/derive_default.rs
Outdated
match *ty.kind() { | ||
// Handle the simple cases. These can derive debug without further | ||
// information. | ||
|
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.
Nitpick: remove this newline.
src/ir/analysis/derive_default.rs
Outdated
|
||
TypeKind::Void | | ||
TypeKind::Named | | ||
TypeKind::TemplateInstantiation(..) | |
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.
Yeah, as mentioned on IRC, I think a template instantiation should derive default if its definition and arguments derive default.
src/ir/analysis/derive_default.rs
Outdated
if info.needs_explicit_vtable(self.ctx, item) { | ||
trace!(" comp that needs vtable cannot derive Default"); | ||
return self.insert(id); | ||
} |
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.
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...
src/ir/analysis/derive_default.rs
Outdated
} | ||
} | ||
|
||
if self.ctx.lookup_item_id_has_vtable(&item.id()) { |
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.
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()) |
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.
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.
☔ The latest upstream changes (presumably #862) made this pull request unmergeable. Please resolve the merge conflicts. |
@bors-servo r+ |
📋 Looks like this PR is still in progress, ignoring approval |
@bors-servo r+ |
📌 Commit 0231a21 has been approved by |
Can derive default analysis r? @fitzgen
☀️ Test successful - status-travis |
r? @fitzgen Fix: #856