Skip to content

Commit 64d73c2

Browse files
committed
ir: Prefer using known semantic parents
When choosing a parent ID for a type that we are parsing, prefer known semantic parents over the provided parent ID. It seems like we shouldn't even be passing explicit parent IDs around (they're often buggy), and instead should expand the `known_semantic_parent` infrastructure, but I'll leave that to some future work. Fixes #1048
1 parent c160b20 commit 64d73c2

File tree

5 files changed

+519
-10
lines changed

5 files changed

+519
-10
lines changed

src/ir/comp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ impl CompInfo {
11521152

11531153
let inner = Item::parse(cur, Some(potential_id), ctx)
11541154
.expect("Inner ClassDecl");
1155+
assert_eq!(ctx.resolve_item(inner).parent_id(), potential_id);
11551156

11561157
ci.inner_types.push(inner);
11571158

src/ir/context.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -751,25 +751,29 @@ impl BindgenContext {
751751
let typerefs = self.collect_typerefs();
752752

753753
for (id, ty, loc, parent_id) in typerefs {
754-
let _resolved =
755-
{
756-
let resolved = Item::from_ty(&ty, loc, parent_id, self)
754+
let _resolved = {
755+
let resolved = Item::from_ty(&ty, loc, parent_id, self)
757756
.unwrap_or_else(|_| {
758757
warn!("Could not resolve type reference, falling back \
759758
to opaque blob");
760759
Item::new_opaque_type(self.next_item_id(), &ty, self)
761760
});
762-
let item = self.items.get_mut(&id).unwrap();
763761

764-
*item.kind_mut().as_type_mut().unwrap().kind_mut() =
765-
TypeKind::ResolvedTypeRef(resolved);
766-
resolved
767-
};
762+
let item = self.items.get_mut(&id).unwrap();
763+
*item.kind_mut().as_type_mut().unwrap().kind_mut() =
764+
TypeKind::ResolvedTypeRef(resolved);
765+
766+
resolved
767+
};
768768

769769
// Something in the STL is trolling me. I don't need this assertion
770770
// right now, but worth investigating properly once this lands.
771771
//
772772
// debug_assert!(self.items.get(&resolved).is_some(), "How?");
773+
//
774+
// if let Some(parent_id) = parent_id {
775+
// assert_eq!(self.items[&resolved].parent_id(), parent_id);
776+
// }
773777
}
774778
}
775779

@@ -953,7 +957,7 @@ impl BindgenContext {
953957
self.compute_bitfield_units();
954958
self.process_replacements();
955959
}
956-
960+
957961
self.deanonymize_fields();
958962

959963
// And assert once again, because resolving type refs and processing

src/ir/item.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,8 @@ impl ClangItemParser for Item {
12161216
ctx,
12171217
));
12181218
}
1219-
parent_id.or_else(|| ctx.known_semantic_parent(definition))
1219+
ctx.known_semantic_parent(definition)
1220+
.or(parent_id)
12201221
.unwrap_or(ctx.current_module())
12211222
}
12221223
None => relevant_parent_id,

0 commit comments

Comments
 (0)