Stream: t-compiler/rust-analyzer

Topic: hir_def ast::Item::Module


kev (Dec 11 2020 at 11:54, on Zulip):

In this line, I read it as a Module does not have inner items, can someone explain why that is the case?

https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir_def/src/item_tree/lower.rs#L100

kev (Dec 11 2020 at 11:55, on Zulip):

Contrasting, ast::Item::Func does have inner items

matklad (Dec 11 2020 at 12:06, on Zulip):

Good question @kev !

I think the primary reason is historical -- items nested in fn's and module items are treeted differently higher in the stack (CrateDefMap)

matklad (Dec 11 2020 at 12:07, on Zulip):

But I guess on the ItemTree level it might make sense to unify the representation? @Jonas Schievink , do you think we can maybe use some smart encoding here?

matklad (Dec 11 2020 at 12:07, on Zulip):

Like, flattening all items in a single array, and using some indexing scheme to infer the tree structure?

Jonas Schievink [he/him] (Dec 11 2020 at 12:14, on Zulip):

hmm, maybe

Jonas Schievink [he/him] (Dec 11 2020 at 12:14, on Zulip):

Not really sure how that would look, I'm not very good with algorithms :)

Jonas Schievink [he/him] (Dec 11 2020 at 12:14, on Zulip):

I guess you're imagining something like how you can store a binary heap in a linear array, and use indices to navigate it?

matklad (Dec 11 2020 at 12:15, on Zulip):

Sortof, yeah

matklad (Dec 11 2020 at 12:15, on Zulip):

The encoding would have to be more complicated, as that's n-ary tree

matklad (Dec 11 2020 at 12:16, on Zulip):

But something like this would contain all the requred information:

item_tree: Vec<(usize, Item)>

where usize is the index of parent

matklad (Dec 11 2020 at 12:19, on Zulip):

An alternative would be Vec<(Item, ops::Range<usize>)>, where the range specifies the children

Laurențiu (Dec 11 2020 at 12:20, on Zulip):

Do we need to update those (e.g. to add children)?

matklad (Dec 11 2020 at 12:21, on Zulip):

Not really, we build the thing once

kev (Dec 11 2020 at 12:37, on Zulip):

Ahh I see, thank you for explaining @matklad :) Peeking into CrateDefMap now

kev (Dec 11 2020 at 12:41, on Zulip):

Hmm oh I think I understand fully, items inside of a function cannot be called from outside of that scope, but items in mod can, so I guess you needed a way to represent this distinction. So modules have their own Scope Variant

Last update: Jul 29 2021 at 08:45UTC