Stream: t-compiler/wg-rls-2.0

Topic: Finding a ra_hir::Trait by name/path

Matthew Hall (Mar 31 2020 at 18:42, on Zulip):

Hey all, I'm looking into I've got a basic version working that creates a Fom impl for the variant your cursor is over iff it's a tuple variant with one element. I thought it best to also disallow the assist if there's already a From impl for the enum with the matching type. Checking for the type is straight forward:

let e: hir::Enum = sema.to_def(&variant.parent_enum())?;
let e_ty = e.ty(sema.db);
let impls = ImplDef::all_in_crate(sema.db, krate).iter().filter(|i| i.target_ty(sema.db) == e_ty);

I'm a bit more stuck with matching the trait up. I tried a couple of things but to no avail:

Any pointers would be appreciated :)

Florian Diebold (Mar 31 2020 at 18:50, on Zulip):

Although From should be in scope I thought this may turn something up, sadly there are no results at all. I note that the tests of auto_import define all the modules they import from, so perhaps this is expected?

yes, unit tests only have what you define, they don't have std.

ImportsLocator is probably not the right thing to use here though. I think you should be able to build a HIR Path to ::core::convert::From and resolve that using semantics.scope(..).resolve_hir_path() to get the trait

Florian Diebold (Mar 31 2020 at 18:53, on Zulip):

ImplDef needs a method to get the resolved implemented trait; the way to get that is db.impl_trait(impl_id).map(|it| it.value.trait_)

Florian Diebold (Mar 31 2020 at 18:55, on Zulip):

instead of going through impls yourself, you could also use the trait solver. that would be the 'proper' way, but needs some more API plumbing... there's actually a special case implemented as Type::impls_future

Matthew Hall (Mar 31 2020 at 19:52, on Zulip):

Interesting, thanks. I'll give those ideas ago and see if I can get either the naiive or the proper way working :)

Last update: Sep 30 2020 at 15:30UTC