Can derive default analysis#861
Can derive default analysis#861bors-servo merged 1 commit intorust-lang:masterfrom photoszzt:derive_default
Conversation
fitzgen
left a comment
There was a problem hiding this comment.
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.
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.
| } | ||
| impl Default for Outer { | ||
| fn default() -> Self { unsafe { ::std::mem::zeroed() } } | ||
| } |
There was a problem hiding this comment.
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.
We can also remove the detect_derive_default_cycle member now.
| } | ||
| impl <c> Default for e<c> { | ||
| fn default() -> Self { unsafe { ::std::mem::zeroed() } } | ||
| } |
There was a problem hiding this comment.
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.
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 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.
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. | ||
|
|
src/ir/analysis/derive_default.rs
Outdated
|
|
||
| TypeKind::Void | | ||
| TypeKind::Named | | ||
| TypeKind::TemplateInstantiation(..) | |
There was a problem hiding this comment.
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.
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.
Nitpick:
if item.has_vtable(self.ctx) {
...
}| } | ||
| _ => false, | ||
| } | ||
| ctx.options().derive_default && ctx.lookup_item_id_can_derive_default(self.id()) |
There was a problem hiding this comment.
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