Skip to content

Add DeclarationDescriptor and ReferenceDescriptor #159

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
wants to merge 2 commits into from

Conversation

kjeremy
Copy link
Contributor

@kjeremy kjeremy commented Oct 24, 2018

Fixes #142
Fixes #146


return ret;
}

// Find the symbol we are looking for
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

At this point I would like to use the DeclarationDescriptor machinery and was thinking of a way to work backwards to an ast::BindingPat. Something like:

 let maybe_binding = find_node_at_offset::<ast::BindPat>(syntax, offset)
            .or_else(||
                find_node_at_offset::<ast::NameRef>(syntax, offset)
                    .and_then(|name_ref| resolve_local_name(name_ref))
                    .and_then(|resolved| {
                        eprintln!("** resolved: {:?}", resolved);
                        let n = find_node_at_offset::<ast::BindPat>(syntax, resolved.1.start());
                        eprintln!("**** n: {:?}", n);
                        n
                    })
            );

        if maybe_binding.is_none() {
            eprintln!("** maybe_binding is none");
            return ret;
        }

        let binding = maybe_binding.unwrap();

        let decl = DeclarationDescriptor::new(binding);

Which would simplify this function quite a bit. However find_node_at_offset::<ast::BindPat>(syntax, resolved.1.start()); always returns None. I'm sure that I am missing something obvious. Dumping the syntax for my test shows that I should be resolving an ast::BindPat there.

Copy link
Contributor Author

@kjeremy kjeremy Oct 24, 2018

Choose a reason for hiding this comment

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

Okay actually... if I change the offset to: resolved.1.end() it seems to work().

}

#[derive(Debug)]
pub struct DeclarationDescriptor<'a> {
Copy link
Member

Choose a reason for hiding this comment

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

Descriptors should be owned values and, ideally, should not directly reference syntax trees.

The idea is that, when processing code, we convert from syntax trees to a descriptor tree (which is more compact) and then throw away syntax to save memory.

We should have a method to map between descriptors and syntax on as needed basis. For example, we can store some sort of syntax node id in the descriptor, and use it to retrieve the node.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrestled with this but found that it was really the only way to implement find_all_refs without passing in the syntax. If we did that then we need to make sure that the syntax is the right one for the stored TextRange (maybe by storing the FileId as well?).

Can you sketch out a little what this would look like? I'm assuming that we do not have the concept of a syntax node id. If we had one where would it look for the corresponding syntax?

Copy link
Member

Choose a reason for hiding this comment

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

Can you sketch out a little what this would look like?

I won't have time until next week to give significant advice unfortunately, but I want to document/refactor/flesh-out the descriptor infrastructure more in the coming weeks.

The motivation for descriptors is that there isn't a 1-to-1 correspondance between rust source code and rust semantic model. If you have

#[path="a.rs"]
mod foo;
#[path="a.rs"]
mod bar;

Then the source of a.rs is present in the semantic model as two unrelated modules.

So, while SyntaxTrees correspond to source files, descriptors should correspond to the semantic model, and most of the analysis should work solely with descriptors.

Practically speaking, I think we should give ids to all syntax nodes and descriptors, and then have a free-form mapping between these ids.

I think a good star might be just using a triple of (FIleId, TextRange, SyntaxKind) as a syntax node ID.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. I'll have to think about how this should work when converting from id to node inside find_all_refs. It looks like we'll need to access (pass in?) the db somehow.

Copy link
Member

Choose a reason for hiding this comment

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

See #169 for the syntax ptr stuff. I'll try to move some of the desciptors to this.

@matklad
Copy link
Member

matklad commented Oct 31, 2018

bors delegate+

@bors
Copy link
Contributor

bors bot commented Oct 31, 2018

✌️ kjeremy can now approve this pull request

@matklad
Copy link
Member

matklad commented Oct 31, 2018

Ive moved scopes from editor to analysis, that's probably the cause of the merge conflict :(

@matklad
Copy link
Member

matklad commented Oct 31, 2018

@kjeremy I'll rebase this myself, to merge the rustfmt PR

@matklad
Copy link
Member

matklad commented Oct 31, 2018

merged!

@matklad matklad closed this Oct 31, 2018
@kjeremy kjeremy deleted the decl_desc branch October 31, 2018 20:47
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.

2 participants