Stream: t-compiler/rust-analyzer

Topic: Add qualify path assist #6172


Lukas Wirth (Oct 14 2020 at 18:11, on Zulip):

For https://github.com/rust-analyzer/rust-analyzer/pull/6172 to be able to qualify trait method calls properly I need access to the trait of the method, all I have now is a ModPath though. And currently resolving that to anything doesn't seem possible/implemented on Semantics. Should there ideally be a resolve_mod_path in semantics that is equivalent to resolve_path? As rebuilding an ast::Path from a ModPath won't work for semantics lookup.

matklad (Oct 14 2020 at 18:12, on Zulip):

Hm, I think it should work the other way aroudn

matklad (Oct 14 2020 at 18:12, on Zulip):

The thing that gives you ModPath should be adjusted to hand (ModPath, Trait) instead

matklad (Oct 14 2020 at 18:12, on Zulip):

How do you get ModPath?

Lukas Wirth (Oct 14 2020 at 18:14, on Zulip):

It's what ImportAssets gives back currently, ModPath of the trait and it's function name. I guess modifying that to give the trait back together with the path and name would be a solution?

Lukas Wirth (Oct 14 2020 at 18:14, on Zulip):

https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/assists/src/utils/import_assets.rs#L30-L33 This here is what is being returned basically

Lukas Wirth (Oct 14 2020 at 18:15, on Zulip):

oh wait

matklad (Oct 14 2020 at 18:15, on Zulip):

hm

matklad (Oct 14 2020 at 18:15, on Zulip):

hir::Type should contain the stuff you need

Lukas Wirth (Oct 14 2020 at 18:15, on Zulip):

ye no that was wrong on my part, thats not whats being returned :sweat_smile:

Lukas Wirth (Oct 14 2020 at 18:16, on Zulip):

ImprotAssets returns a BTreeSet<ModPath>

Lukas Wirth (Oct 14 2020 at 18:16, on Zulip):

That's where the paths come from

Lukas Wirth (Oct 14 2020 at 18:16, on Zulip):

The type that was there is the type of the method callee

matklad (Oct 14 2020 at 18:17, on Zulip):

Yeah

matklad (Oct 14 2020 at 18:17, on Zulip):
                let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
                if let Some(prefix_kind) = prefixed {
                    self.module_with_name_to_import.find_use_path_prefixed(db, item, prefix_kind)
                } else {
                    self.module_with_name_to_import.find_use_path(db, item)
                }
matklad (Oct 14 2020 at 18:17, on Zulip):

So this is a bit of code which erases the knowledge about the original item

matklad (Oct 14 2020 at 18:18, on Zulip):

I think we shoudn't forget the item, and carry it together with thr path

Lukas Wirth (Oct 14 2020 at 18:21, on Zulip):

Seems like the best option

Lukas Wirth (Oct 14 2020 at 18:27, on Zulip):

hm, the tuple (ModPath, ItemInNs) unfortunately isn't Ord so BTreeSet won't really work. I guess a wrapper struct that delegates the Ord impl just to the ModPath should work?

matklad (Oct 14 2020 at 18:28, on Zulip):

derive(Ord) on ItemInNs should be fine as well

Lukas Wirth (Oct 14 2020 at 18:28, on Zulip):

Alright

Lukas Wirth (Oct 14 2020 at 18:29, on Zulip):

ModuleDefId and MacroDefId aren't Ord either tho

Lukas Wirth (Oct 14 2020 at 18:29, on Zulip):

and that goes down to all the other IDs as well

Lukas Wirth (Oct 14 2020 at 18:29, on Zulip):

:sweat_smile:

Lukas Wirth (Oct 14 2020 at 18:30, on Zulip):

or almost all it seems

matklad (Oct 14 2020 at 18:30, on Zulip):

Yeah, it's fine to derive them

matklad (Oct 14 2020 at 18:30, on Zulip):

OTOH, are we really sure that BTreeSet is the right thing here?

Lukas Wirth (Oct 14 2020 at 18:30, on Zulip):

The sole reason for BTreeSet seems to be sorted iteration I believe?

matklad (Oct 14 2020 at 18:30, on Zulip):

I am not a fan of this data structure usually, as it is allocation-eager

matklad (Oct 14 2020 at 18:30, on Zulip):

perhaps returning a sorted vec would work better?

matklad (Oct 14 2020 at 18:31, on Zulip):

and custom sorting with .sort_by is easy

Lukas Wirth (Oct 14 2020 at 18:31, on Zulip):

That should work

matklad (Oct 14 2020 at 18:32, on Zulip):

maybe through in Vec::dedup at the end if uniquness is also required

matklad (Oct 14 2020 at 18:32, on Zulip):

/me must confess that sorted vector is their favorite data structure

Lukas Wirth (Oct 14 2020 at 18:58, on Zulip):

I can agree with that confession :)

Lukas Wirth (Oct 14 2020 at 19:49, on Zulip):

Okay, trait methods now work as well.

Last update: Jul 24 2021 at 19:15UTC