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

RA inlay hint cannot work properly in functions with proc_macro annotations #14527

Closed
lh123 opened this issue Apr 7, 2023 · 2 comments
Closed
Labels
A-inlay-hints inlay/inline hints A-proc-macro proc macro C-bug Category: bug

Comments

@lh123
Copy link
Contributor

lh123 commented Apr 7, 2023

rust-analyzer version: rust-analyzer version: 0.3.1463-standalone

rustc version: rustc 1.68.2 (9eb3afe9e 2023-03-27

RA parameterHints/chainingHints cannot work properly in functions with proc_macro annotations, but typeHints seem to work normally

Example

Proc macro

use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn test_attr(_args: TokenStream, input: TokenStream) -> TokenStream {
    let funtion: syn::ItemFn = match syn::parse(input) {
        Ok(function) => function,
        Err(err) => return err.to_compile_error().into(),
    };
    quote::quote! {
        #funtion
    }
    .into()
}

Test

#[cfg(test)]
mod tests {
    use test_ra::test_attr;

    fn add(a: i32, b: i32) -> i32 {
        a + b
    }

    #[test_attr]
    fn test() {
        // parameterHints/chainingHints not works, but typeHints works well?
        let _a = add(5, 6);
        let _b: Vec<_> = [1, 2, 3]
            .iter()
            .map(|item| item)
            .map(|item| item)
            .map(|item| item)
            .collect();
    }

    fn test1() {
        let _a = add(5, 6);
        let _b: Vec<_> = [1, 2, 3]
            .iter()
            .map(|item| item)
            .map(|item| item)
            .map(|item| item)
            .collect();
    }
}

image

@lh123 lh123 added the C-bug Category: bug label Apr 7, 2023
@lh123 lh123 changed the title RA parameterHints cannot work properly in functions with proc_macro annotations RA parameter inlay hint cannot work properly in functions with proc_macro annotations Apr 7, 2023
@lh123 lh123 changed the title RA parameter inlay hint cannot work properly in functions with proc_macro annotations RA inlay hint cannot work properly in functions with proc_macro annotations Apr 7, 2023
@mdx97
Copy link
Contributor

mdx97 commented Apr 8, 2023

I spent a bit of time looking into this one (more specifically, why the parameter hints weren't showing) and it may be a bit over my head, so I am just going to context dump here...

Generating the hints for parameter types seems to be done in crates/ide/src/inlay_hints/param_name.rs. Like several of its sibling modules, it has a hints function that is called at a higher level in order to generate hints for this specific hint type.

The body of this function calls get_callable, which returns an Option that will be None for the call site inside the function with an attribute, and Some(...) for the other call site. This can be traced back to what is returned by the call to Semantics::type_of_expr in the ast::Expr::CallExpr match arm.

Tracing this back further, you get to the SemanticsImpl::analyze_impl function. This is where the core discrepancy seems to lie... the call site that properly generates the parameter type hints has the variant ChildContainer::DefWithBodyId for the container variable, while the call site inside the function with an attribute macro has ChildContainer::ModuleId.

Why this is the case, I have no idea and I must stop for the night. Maybe someone with more knowledge can figure it out based on this information :)

@lowr
Copy link
Contributor

lowr commented Jul 31, 2023

The root cause of this bug turned out to be #14959. It has since been fixed by #14960, but you need to update the proc-macro server (as opposed to rust-analyzer) that's usually installed via rustup along with rustc. The fix only made it to the current beta (1.72), so to actually see this fixed in your editor, you need to either wait for the next stable release or use the beta or nightly toolchain.

Closing as fixed but please be noted above. Thanks for the report!

@lowr lowr closed this as completed Jul 31, 2023
@lowr lowr added A-inlay-hints inlay/inline hints A-proc-macro proc macro labels Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inlay-hints inlay/inline hints A-proc-macro proc macro C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants