Stream: t-compiler/rust-analyzer

Topic: Resolving methods

Phil Ellison (Jan 01 2021 at 19:55, on Zulip):

I'm not sure how to address this comment: Could someone please point me in the right direction. I can use self.infer.method_resolution to get hold of a FunctionId for the method being called (e.g. filter_map) and Resolver::resolve_known_trait to get hold of a TraitId for std::iter::Iterator, but I'm not sure what I can do to relate the two (i.e. how to conclude that this FunctionId is for a method on the iterator trait identified by TraitId).

Florian Diebold (Jan 01 2021 at 20:10, on Zulip):

with the FunctionId, you can do id.lookup(db.upcast()).container to get the container, which you can then check whether it's equal to AssocContainerId::TraitId(iterator_trait_id) to see whether it's a method from Iterator

Florian Diebold (Jan 01 2021 at 20:13, on Zulip):

I wish there was a more straightforward way to do that in the HIR crates (in code_model, it's easier)

Phil Ellison (Jan 01 2021 at 20:16, on Zulip):

Thanks! I'll give this a go.

Florian Diebold (Jan 01 2021 at 20:28, on Zulip):

alternatively, you could do db.trait_data(trait_id).items.get(name![filter_map]) to get the method by name

Phil Ellison (Jan 01 2021 at 20:34, on Zulip):

Perfect, thanks.

Phil Ellison (Jan 01 2021 at 21:13, on Zulip):

This turned out to be surprisingly easy!

Last update: Jul 26 2021 at 13:30UTC