Stream: t-compiler/rust-analyzer

Topic: Rust-analyzer - Modifier for callable variables


Gray Jack (Oct 15 2020 at 21:49, on Zulip):

I wanted to add a modifier for variables that are callable for a while now. Motivated by when creating a fn pointer or a closure the variable name in call expression not being colorized.

For plain fn pointer and closures I have something kinda working on a local branch

I have 2 questions:

  1. Is that a wanted feature?
  2. How do I check if a variable is of a type that implements the Fn{Once, Mut} traits to properly add the modifier for parameter generic over those traits?
matklad (Oct 15 2020 at 21:51, on Zulip):

Seems reasonable

matklad (Oct 15 2020 at 21:51, on Zulip):

Do you need a modifier though?

matklad (Oct 15 2020 at 21:52, on Zulip):

Can't you just use "Function" tag?

matklad (Oct 15 2020 at 21:52, on Zulip):

Hm, I guess function and local would be in conflict, and a modifer might be indeed better...

matklad (Oct 15 2020 at 21:53, on Zulip):

You can use this method to determine if a type is a callable or not: https://github.com/rust-analyzer/rust-analyzer/blob/0d45802d671f94cb768b93a64882733396cfbe2d/crates/hir/src/code_model.rs#L1450

matklad (Oct 15 2020 at 21:53, on Zulip):

And I think other code in highlighting already fetches types for locals to underly &mut

Gray Jack (Oct 15 2020 at 22:01, on Zulip):

Thanks a bunch!! I'll send a PR as soon as I can

Gray Jack (Oct 16 2020 at 08:48, on Zulip):

It seems that as_callable only returns Some in case of FnDef, Closure and FnPtr

Gray Jack (Oct 16 2020 at 08:50, on Zulip):

In the case of generics of Fn traits as_callable returns None

Gray Jack (Oct 16 2020 at 08:51, on Zulip):

So how do I check if a Type implements FnOnce/FnMut/Fn?

matklad (Oct 16 2020 at 09:25, on Zulip):

Hm, I think thats a bug

matklad (Oct 16 2020 at 09:25, on Zulip):

the thing should return Some for generics

Gray Jack (Oct 16 2020 at 09:33, on Zulip):

the function "callable_sig" returns Some when TypeCtor::FnPtr, TypeCtor::FnDef and TypeCtor::Closure, but I'm not exactly comfortable with the code of "callable_sig", so maybe I'm the one missing something

matklad (Oct 16 2020 at 09:38, on Zulip):

Hm, yeah, it feels like that should also handle Ty::PLaceholder

matklad (Oct 16 2020 at 09:38, on Zulip):

@Florian Diebold might be the right person to ask this :-)

Florian Diebold (Oct 16 2020 at 09:58, on Zulip):

Fn traits aren't handled by callable_sig because they need some more involved handling during inference. A function with the signature of Ty::callable_sig can't actually check for the Fn trait because it needs the environment (also note that the expected number of args is actually relevant for inference). I think this needs separate and slightly different handling outside of inference, similar to that function and to the way we check for Future, for example. I'd be a bit worried about doing so many trait solves, but if you just do it for type parameters it'd probably be ok

Florian Diebold (Oct 16 2020 at 10:01, on Zulip):

(and I don't think a helper method on Ty should be doing any trait solving in general)

Gray Jack (Oct 16 2020 at 10:07, on Zulip):

The code to check if implements Fn traits is somewhat similar to the code in impls_future?
If yes what is the string I have to pass to lang_item?

Florian Diebold (Oct 16 2020 at 10:11, on Zulip):

FnTrait::FnOnce.lang_item_name()

Florian Diebold (Oct 16 2020 at 10:11, on Zulip):

i.e. "fn_once"

Gray Jack (Oct 16 2020 at 10:21, on Zulip):

Should I make lang_item_name public or just use "fn_once"?

Florian Diebold (Oct 16 2020 at 10:25, on Zulip):

actually, you could just use get_id

Gray Jack (Oct 16 2020 at 10:31, on Zulip):

True, I missed that

Last update: Jul 26 2021 at 12:30UTC