Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust-analyzer fails to infer types for Endpoint::Response #28

Open
lazulit3 opened this issue Dec 17, 2024 · 0 comments
Open

rust-analyzer fails to infer types for Endpoint::Response #28

lazulit3 opened this issue Dec 17, 2024 · 0 comments

Comments

@lazulit3
Copy link

When trying out rustify and rustify_derive I noticed that rust-analyzer (in my editor) was failing to infer my response types specified via Endpoint::Response for my endpoints.

Symptoms

After defining an endpoint, making a request, and storing the response parsed into your Endpoint::Response type, rust-analyzer will have type {unknown} for the variable instead of your defined response type. (This occurs even though the type can be known concretely.)

You can reproduce this using a text editor with rust-analyzer configured as an LSP.
Personally, I am testing using Zed (editor) with rust-analyzer 1.83.0 (90b35a62 2024-11-26)

The type of a variable can be known by hovering over a variable name; rust-analyzer will provide type information on hover.

This can be reproduced on master (1ed432d) in the example at

let result = endpoint.exec(&client).await;

Hover your mouse over result or use an editor keybinding for hover (on MacOS with Zed, that's cmd-k cmd-i

This is what I see:

Screenshot 2024-12-17 at 3 34 54 PM
Screenshot 2024-12-17 at 3 35 22 PM

Cause

I cannot comment on why rust-analyzer is unable to handle this case, but it seems to me that the symptom occurs because the Endpoint trait implementation in the derive macro is wrapped in this block expression:

let const_name = format!("_DERIVE_Endpoint_FOR_{}", id);
let const_ident = Ident::new(const_name.as_str(), Span::call_site());
quote! {
#[allow(non_local_definitions)]
const #const_ident: () = {

I assume the block expression was provided to allow the local use statements there.

Recommendation

I have verified that updating the derive macro to remove the block expression wrapping the Endpoint trait implementation resolves this issue.

Since the block expression is mainly for convenience with the use statements (as far as I understand), we can alternatively update references to those types to use full import paths. For example, ClientError to rustify::errors::ClientError.

Although this may make the macro code slightly thicker, I believe the improvement to developer UX (fixing type inference in editor) is preferable. I will open a PR for your consideration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant