-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Code completion not working in #[tokio::main] #13355
Comments
I can reproduce this with |
I've narrowed it down to:
Here's the relevant diff: 7e2b983...2b472f6 I'm going to try and bisect this. Edit: while bisecting, I encountered "Broken pipe (os error 32)" at dadb832, so I'm going to try and switch to 1.63.0 for the rest of the bisection and hope it still tells us what we want to know. |
From a quick look (inserting a |
My bisection pointed to fba6adf, so I must've messed something up, unless doc comments are somehow load-bearing. Starting again... (with 1.63.0 the whole time). |
We are hitting this early return which disables completions
Which is kind of weird, because I don't think any code regarding this has changed there? |
Ye those commits are not related I'm failry certain, so from what I've checked, the problem is that we map the token we are completing to the |
Upon closer inspection, it doesn't work with this version either (at least, with rustc 1.63.0). I'm going to stop posting comments until I can pinpoint it to one commit. edit: I'm keeping track of my bisect work here https://gist.github.com/fasterthanlime/43142d7bb3bcae4869441852bcee830b edit: latest bisect points to ebfbb31 (but that's for rustc 1.63.0, it seems that broke at a different place)
|
I assume proc-macro server changes might've changed span association for tokens for r-a (not sure why or how, but it's suspicious given that the commit there removes randomness so I wouldn't be surprised). Tokio with the following patch works as intended tokio-rs/tokio@f307531 (which makes it skip the span on the initial tokens). So I wouldn't consider this a regression here (I am honestly baffled it supposedly worked for people before?), as this was just sheer luck that it didn't break so far. Also note, this only affects completions for the tail expression. If you are trying to complete in non-tail position completions work fine, since those spans aren't being re-used. |
I'm glad you were able to narrow it down so much @Veykril but idk how I feel about a patch to tokio — a fix in rust-analyzer would fix it for all tokio versions published so far, so I'd rather have that (and a regression test) if possible. I'm not sure how to write such a test right now. |
Well to be honest, I don't think we can fix it in r-a, unless we hardcode for the tokio proc-macro which I will absolutely fight against doing. I am still puzzled how it worked before ebfbb314c03cd8e70198eebf5a96ece8a2f79e51though, so maybe I am missing something obvious here. But from what I know the problem is just the fact that tokio re-uses the span, which is the only element r-a can use to calculate completions for proc-macro expansions. |
Oh I'm right there with you, that would be awful. You certainly have a much better understanding of the actual completion code than I do, my observations are only based on the fact that you could get completions in caf23f2, but you can't anymore in ebfbb314c03cd8e70198eebf5a96ece8a2f79e511, so my immediate instinct would be to go and diff the two expansions and see what changed, because I think we're in either of those situations:
If it can be fixed by somehow returning to the older behavior, that'd mean tokio doesn't need to be patched and that seems like a better outcome. Of course someone needs to put in the work to do that research! |
Note that even with the tokio patch, completion is not behaving as expected. The first half of this video is "without tokio::main", and typing Completion-In-Tokio-Main-1080p.mp4(I've verified that completion works as expected with rustc 1.63 and caf23f2) |
Oh, completion works in caf23f2 because... expansion fails:
|
The latter is the case, the problem is that tokio is emitting its extra tokens with the span of the tail expression (for diagnostics purposes when a type mismatch happens), this unfortunately trips up r-a because it uses the spans to figure out a lot of things (completions is just one of the things, as you see renaming is also affected and a lot of other things). r-a usually just picks the first token that matches for a span, which in tokio's case will always be a |
Would it be possible to give each token and token tree passed to the proc macro a unique id and remap based on that? If a proc macro passes a part of the input back unchanged it would have the same unique id, and it it changes it, a different id would be assigned to the part that was changed. |
Or maybe we could just prefer tokens that are the same as the original token if we have multiple choices? |
I think there's more going on though — as mentioned in #13355 (comment), your patch doesn't quite fix the problem. |
Ye completions still acting funky comes from the fact that we are now mapping the identifier to a semicolon which has me very confused .. god I hate macros |
Okay, so I went digging in completions a bit and found some general problems there that I fixed in #13386, with this, completions work correctly with the tokio patch applied. I still have no idea why the behavior changed though, will do some digging for that next, although I still don't consider that a bug. So I might instead just look into ways of making the token mapping here smarter for completions as that is probably a better use of the time. |
Yay! Fwiw my position has changed since I discovered the only reason it /did/ work was because macro expansion didn't work at all. The tokio patch seems like the way to go now, to me. |
Ye I'll probably PR the patch to tokio anyways since it does simplify things in general.
We can most likely do this here.
Isn't this what are basically doing already? Our span is a token id, its just the fact that tokio copies the span and re-uses it for unrelated tokens. |
My suggestion is to tie the token id to the specific token value, so it can't be re-used for unrelated tokens no matter what a proc macro does. |
I guess that would be something for #9403 then. We want both behaviors in a sense, completion really wants to drill down the unique identity of a token (for now, ideally we would inspect all usages of a token in expansions, though maybe only for decl macros), but we also want to see span re-uses for some other features. |
I'll close this as its mostly fixed, and the problematic parts are tracked in the linked issue now. |
fwiw I opened a PR for the span patch tokio-rs/tokio#5092 though it would be fully understandable for them to not merge this given its a workaround for a r-a bug |
Yeah exactly, one big reason I wanted to make sure this couldn't be fixed on the r-a side is that I think (no matter how useful r-a is) there's limited political capital to spend and get crate maintainers to merge fixes "purely for r-a". From previous experience, I can totally see some of them going "rustc likes my code, that's good enough for me". |
Good work ya'll. |
rust-analyzer version: rust-analyzer version: 0.4.1232-standalone (476d043 2022-10-04)
rustc version: rustc 1.64.0 (a55dd71d5 2022-09-19)
relevant settings:
I'm not getting code completion in
#[tokio::main]
but it seems based on old issues that it should work?Screen.Recording.2022-10-05.at.8.00.43.PM.mov
This is happening in a fresh project with the following Cargo.toml:
Sorry if I should have resurrected an old issue instead of starting a new one.
The text was updated successfully, but these errors were encountered: