Stream: t-compiler/rust-analyzer

Topic: purpose of `hir::Type::walk`


Jonas Schievink [he/him] (Jun 02 2021 at 17:05, on Zulip):

I'm trying to find all references to type parameters in a given type, to account for them in an assist. It looks like hir::Type::walk skips them in many places. I'm not entirely sure what the purpose of that method is, can someone enlighten me? Is it supposed to skip parts of the type?

Jonas Schievink [he/him] (Jun 02 2021 at 17:21, on Zulip):

We don't really seem to have any type visiting infra that also handles lifetimes and consts, hmm

Lukas Wirth (Jun 02 2021 at 17:27, on Zulip):

Can't you use the chalk Visitor/Fold traits?

Jonas Schievink [he/him] (Jun 02 2021 at 17:31, on Zulip):

probably yeah, are they supposed to be used outside of hir_ty?

Lukas Wirth (Jun 02 2021 at 17:32, on Zulip):

Probably not, I don't think we expose chalk outside of hir_ty/hir?

Jonas Schievink [he/him] (Jun 02 2021 at 17:32, on Zulip):

hir_ty also has its own simlified TypeWalk trait

matklad (Jun 02 2021 at 17:33, on Zulip):

The overall design constraint here is that we don't really want to expose the inner strucutre of Type to the IDE

matklad (Jun 02 2021 at 17:40, on Zulip):

@Jonas Schievink [he/him] what assist would that be?

Jonas Schievink [he/him] (Jun 02 2021 at 17:40, on Zulip):

extract_type_alias

matklad (Jun 02 2021 at 17:46, on Zulip):

Hm, why do you need to know the structure of the type for that? Why isn't hir::Type::to_string not enough?

Jonas Schievink [he/him] (Jun 02 2021 at 17:51, on Zulip):

because the extracted type might reference type parameters of the containing item

Jonas Schievink [he/him] (Jun 02 2021 at 17:51, on Zulip):

and I want to make the type alias generic over those

Jonas Schievink [he/him] (Jun 02 2021 at 17:52, on Zulip):
impl<T> Bar<T> {
    fn f<U>(&self) -> $0X<T, U, V>$0 {}
}

should result in

type Type<T, U> = X<T, U, V>;

impl<T> Bar<T> {
    fn f<U>(&self) -> Type<T, U> {}
}
Florian Diebold (Jun 02 2021 at 17:57, on Zulip):

I think hir::Type::walk is currently only used for hover links, which have ... special requirements

Florian Diebold (Jun 02 2021 at 17:57, on Zulip):

it might be better to just expose special-purpose methods for now

Florian Diebold (Jun 02 2021 at 17:58, on Zulip):

it's still kind of surprising that it skips parameters though

Florian Diebold (Jun 02 2021 at 17:58, on Zulip):

I don't think places that are actually visible in the code should be skipped

Florian Diebold (Jun 02 2021 at 17:59, on Zulip):

soo maybe you can actually improve it and use it

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

the TypeWalk trait should go away

Last update: Jul 28 2021 at 04:00UTC