Stream: t-compiler/rust-analyzer

Topic: Getting trait of an impl


Jade (Jun 01 2021 at 10:13, on Zulip):

I'm currently working on a feature to be able to goto definition a trait impl to get to the trait on the other side (kinda like https://github.com/rust-analyzer/rust-analyzer/pull/8988 but the other way around) and I'm having trouble actually finding the trait. I'm currently trying to do it through the route of getting the AssocItem and then the container, to get the impl.

From that impl, I can get a TraitRef, but as per the comment:

    // FIXME: the return type is wrong. This should be a hir version of
    // `TraitRef` (ie, resolved `TypeRef`).

this is not a really useful object to have, as is also supported by poking through the rustdocs. So I'm stuck here.

To be specific, I want to get the trait items in such a way that I can find the function on the trait itself matching the name of the function on the impl, then get its position.

Florian Diebold (Jun 01 2021 at 11:10, on Zulip):

you can use db.impl_trait to get the implement Chalk TraitRef; then you can use hir_ty::from_chalk_trait_id(trait_ref.trait_id) to get the trait ID and turn that into a hir::Trait. I think you could make that function just return Option<Trait> for now; it seems all uses of it just call is_some or is_none anyway?

Florian Diebold (Jun 01 2021 at 11:11, on Zulip):

ideally we'd have something like hir::Type that wraps a chalk_ir::TraitRef there (that's what the FIXME means), but for now just returning the Traitwould be fine

Last update: Jul 29 2021 at 09:30UTC