User-defined quick fixes?

I've been working on some procedural macros recently, and I really like how you can use syn::Error to create really helpful spanned compiler errors for people using your macros. Often, I will try to offer up a suggested change that the user could use to fix their code.

When I use an LSP like RLS or Rust Analyzer, it can often provide quick fixes to compiler errors that I am getting. With a click of a button or quick keypress, the fix is implemented for me much quicker than if I had needed to type it out myself.

What I am wondering about is whether this is something we could allow macro writers to add to their existing compiler errors. What I'm picturing is some sort of "substitution" struct that you could pass that encodes which span would be changed and the contents of the fixed code.

I'm not super familiar with how quick fixes are currently implemented in Rust language servers, so I'm not sure how difficult this would be to implement, but I'm curious what folks think about this type of feature.

3 Likes

I think this would be a good fit for an addition to the span diagnostics API: https://github.com/rust-lang/rust/issues/54140

One limitation is that proc-macros have significantly less information available to them than the compiler does (e.g. no type information). This may limit the kinds of suggestions that proc-macros can usefully emit.

2 Likes