Skip to content

Add a debug phase to assert against dangling ItemId references without an associated Item #209

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
fitzgen opened this issue Nov 4, 2016 · 4 comments

Comments

@fitzgen
Copy link
Member

fitzgen commented Nov 4, 2016

We should traverse the Item graph and ensure that for every ItemId reference an Item has, there is an entry in BindgenContext::items for that ItemId. If there isn't then it is a dangling reference, codegen will blow up, and we did something wrong in parsing.

Bonus points if the graph traversal is BFS and we can dump to stdout the shortest path to the dangling reference.

We can do this in a conservative manner with the TypeCollector trait, but to get precise results that consider all ItemId references, we'll need a similar trait for all Items, not just Items that are Types (which is what TypeCollector is).

@fitzgen
Copy link
Member Author

fitzgen commented Nov 4, 2016

FWIW, I've been debugging dangling references in codegen, and it requires rr to get anywhere. If we had this assertion phase, I think it would be easier to debug.

@impowski
Copy link
Contributor

impowski commented Dec 2, 2016

I could take it because it will be interesting to play around graphs. As always some sneak peak where to start and potential problems.

@fitzgen
Copy link
Member Author

fitzgen commented Dec 2, 2016

Great :)

Here is a modest / version 0 / MVP approach that doesn't do a full shortest path to the dangling reference, nor finds all inter item references (just those exposed by TypeCollector implementations):

This phase should happen after parsing, just before code generation. Maybe called directly from BindgenContext::gen? It would iterate over items in the context, call TypeCollector::collect_types on each item, and for each sub-item assert that the context has the sub-item (ie it is not a dangling ItemId reference).

Here's a sketch:

for (id, item) in ctx.items() {
    let mut sub_items = ItemSet::new();
    item.collect_types(ctx, &mut sub_items, &());

    sub_items.insert(item.parent_id());

    for sub in sub_items {
        assert!(ctx.fallible_resolve_item(sub).is_some(),
               "Should not dangle: {:?} references {:?}", id, sub);
    }
}

bors-servo pushed a commit that referenced this issue Dec 15, 2016
Add assert for dangling references without an associated ItemId

So I think this is it? Fix issue #209
r? @fitzgen
@fitzgen
Copy link
Member Author

fitzgen commented Dec 15, 2016

Fixed in #312

@fitzgen fitzgen closed this as completed Dec 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants