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

Allow proc macros to expose their grammar to rust analyzer for syntax error understanding #7402

Open
lahwran opened this issue Jan 22, 2021 · 10 comments
Labels
A-completion autocompletion A-macro macro expansion A-proc-macro proc macro C-Architecture Big architectural things which we need to figure up-front (or suggestions for rewrites :0) ) S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@lahwran
Copy link

lahwran commented Jan 22, 2021

My understanding of rust-analyzer - correct me if I'm wrong - is that it can partially analyze code with syntax errors as long as it doesn't use proc macros, and that this is core functionality as part of providing completions for partially written lines such as let x = . If that is correct, what would it take for proc macros to expose their grammar to get the same level of support as the rest of rust when code is incomplete? I'm most interested in getting tab completion for identifiers in proc macros, because I have several different proc macros I'd like to use very heavily and don't want to lose completion functionality in them.

@edwin0cheng edwin0cheng added the A-macro macro expansion label Jan 22, 2021
@lnicola lnicola added C-Architecture Big architectural things which we need to figure up-front (or suggestions for rewrites :0) ) S-unactionable Issue requires feedback, design decisions or is blocked on other work labels Jan 22, 2021
@flodiebold
Copy link
Member

flodiebold commented Jan 22, 2021

The proc macros need to be written in a way that passes through even incomplete syntax. Most proc macros (maybe all) will probably fail to expand if their input is not syntactically correct. They would instead need to try to expand as best as possible. Ideally there should be a way for proc macros to return an expansion, but also signal an error (this might be an interesting thing to prototype / think about, but would require support in the compiler); but for proc macros where parts of the input are just normal Rust syntax that's passed through to a certain place in the output, that's not really necessary (they can just pass through the invalid syntax and rely on the compiler to complain afterwards).

I think it'd be interesting to look at concrete cases that aren't working right now and what it would take to make them work.

(I think "exposing their grammar" is probably not necessary or the right approach, except maybe for syntax highlighting completely custom DSLs...)

@jonas-schievink
Copy link
Contributor

I don't think this is much of a rust-analyzer issue, the proc macro ecosystem just needs to become more IDE-friendly

@edwin0cheng
Copy link
Member

edwin0cheng commented Jan 22, 2021

Totally agree on @jonas-schievink and @flodiebold .

And one thing we could improve here is :
https://github.com/rust-analyzer/rust-analyzer/blob/607b9ea160149bacca41c0638f16d372c3b235cd/crates/proc_macro_srv/src/rustc_server.rs#L415

We do not implement all span information for proc-macro (which is hard) such that error recovery is impossible in some cases.

@matklad
Copy link
Member

matklad commented Jan 30, 2021

@ogoffart of https://sixtyfps.io came up with ingeniously simple scheme for this:

I have some ideas on how rust-analyzer could use some kind of
"in-band-signaling" to help with auto-complete within proc-macro. I don't know
if you ever considered it.
For example, consider
some_macro!(hello i am a macro)
The cursor is located just after "am", and the user press ctrl+space to
auto-complete. rust-analyzer would send this token stream to the macro:
"hello i __rust_analyzer_completion a macro"
if the macro expand to some rust code, rust_analyzer would find the
__rust_analyzer_completion token in the expansion, and run normal rust
completion code to auto-complete the rust code.
But the macro would also understand the __rust_analyzer_completion within the
token stream, and instead of expanding to its normal expansion, expand to
something like __rust_analyzer_completion![ "am", "have", "foo", "bar" ]

Notably, we already do the __rust_analyzer_completion thing here:

https://github.com/rust-analyzer/rust-analyzer/blob/557c1e36ddbb19cc76f7a1f04d1b327942aafcb9/crates/completion/src/context.rs#L107-L114

@matklad
Copy link
Member

matklad commented Jan 30, 2021

cc @vlad20012

@ModProg
Copy link
Contributor

ModProg commented May 17, 2022

I could imagine one case where exposing something like a gramar would make sense, and that is proc macros that use a relatively simple syntax to just accept some attributes like e.g.

  • #[attr(1, 4, 5)]
  • #[attr(prop="<syn::Path>")]
  • #[attr(prop=syn::Expr)]
  • #[attr(prop(propa=<usize>, probb=<str>))]

@jssblck
Copy link

jssblck commented Mar 10, 2024

i am writing a custom dsl macro and would love to add intellisense support for the dsl, so i found this issue. has there been any movement on this elsewhere, or is this issue still the current state?

i'd really like to make the experience as good as i can get it, the dsl is actually effectively rust syntax but the things being named in the dsl aren't actually in the program.

right now i'm vaguely thinking i can generate the things being named into actual syntax, then produce partially formed token streams in an effort to contort Rust and R-A into producing completions and useful errors... i'm not sure how feasible that is but it's the closest i've got

@Veykril
Copy link
Member

Veykril commented Mar 10, 2024

The best info on this we have currently is #15452.

@jssblck
Copy link

jssblck commented Mar 10, 2024

oh awesome, thanks! sounds like the rough idea should work then!

@chipnertkj
Copy link

I have opened a discussion thread in the Rust Internals Forum related to this issue.
If there is something constructive you could add to the discussion (criticism, possible solutions, concerns, use cases), please head through the link below.
https://internals.rust-lang.org/t/discussion-adding-grammar-information-to-procedural-macros-for-proper-custom-syntax-support-in-the-toolchain/21496

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion autocompletion A-macro macro expansion A-proc-macro proc macro C-Architecture Big architectural things which we need to figure up-front (or suggestions for rewrites :0) ) S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests

10 participants