InlayHints vanish with tracing instrument macro on method

I use rust-analyzer via VSCode. (latest stable with v0.3.2457).
When adding the tracing::instrument macro to a method, almost all method chain inlay hints vanish. I tried to extract a minimal version, the only dependencies are ureq and tracing. I have not changed any settings in the inlayHints section in the rust-analyzer config

When I remove the instrument-macro from the bar-method, the inlay hints are shown like in the main method.

I am fairly new to rust, so maybe this works as intended and I did not find the corresponding documentation yet. I did not see any obvious errors in the output.

Is this working as intended?

1 Like

My educated guess is that this is due to instruments being a proc-macro that modifies the function generating a new one. Proc macros can do arbitrary transformations[1] on the syntax tree for the item they annotate, and as such your IDE might have issues fully understanding what is going on.

With macro_rules (declarative macros) Rust Analyzer can often figure things out, but with proc macros it is much harder.


  1. Derive proc macros are more limited, but this is an attribute style proc macro which is more powerful. And function style proc macros are more powerful still: their input doesn't even need to be valid Rust code. ↩︎

I tested it again with another proc macro and got the same result. This seems to be the correct answer.

For proc macros, the macro author needs to make sure spans (i.e. source locations) from the input are passed on to the output. That's not always possible or easy to know/remember. Even then, Rust Analyzer may not always be able to use them.

For future reference, I just found out about the setting to ignore certain proc macros, this brings back the chain hints. Example for VSCode settings.json:

  "rust-analyzer.procMacro.ignored": {
      "tracing-attributes": [
          "instrument"
      ]
  },
1 Like

(I am a rust-analyzer team member).

This is a bug; function macros are ignored deliberately, because they can mess up with their inputs in unexpected ways, but attribute macros should not.

Other people have reported similar problems recently (RA `inlay hint` cannot work properly in functions with `proc_macro` annotations · Issue #14527 · rust-lang/rust-analyzer · GitHub). I've created an issue to track that: Parameter and chaning inlay hints no longer work in attribute proc macros · Issue #19834 · rust-lang/rust-analyzer · GitHub

4 Likes