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

fix: Fix actual token lookup in completion's expand() #18889

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

ChayimFriedman2
Copy link
Contributor

It should be left biased, not right biased, because when e.g. the use has typed h then requested completion, the h is what we want to find, not the next token (which might indeed be inside a macro call).

I'm not sure why I wrote right_biased() to begin with (I remember I had a reason and not just "both should work"), I might've copied the code in expand_and_analyze() (which is wrong, because there it lookups on the speculative file, where right biased will always find the correct token and left biased not).

This is still not perfect, because there might not be an identifier already typed then we might still end up in a macro call, but this is the best we can do.

Fixes #18888. The reason the final assert worked is not because it somehow "reset" things, but because there was no macro call after it.

It should be left biased, not right biased, because when e.g. the use has typed `h` then requested completion, the `h` is what we want to find, not the next token (which might indeed be inside a macro call).

I'm not sure why I wrote `right_biased()` to begin with (I remember I had a reason and not just "both should work"), I might've copied the code in `expand_and_analyze()` (which is wrong, because there it lookups on the speculative file, where right biased will always find the correct token and left biased not).

This is still not perfect, because there might not be an identifier already typed then we might still end up in a macro call, but this is the best we can do.
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 9, 2025
@lnicola
Copy link
Member

lnicola commented Jan 9, 2025

Could we add a test for this?

@Veykril
Copy link
Member

Veykril commented Jan 9, 2025

We go have a pick_best_token helper as well for ranking the options by their kind if useful here.

@ChayimFriedman2
Copy link
Contributor Author

We go have a pick_best_token helper as well for ranking the options by their kind if useful here.

I don't think this will help, I mean I can try both left and right and give left a higher score, but this will add overhead & I don't think it's worth it, it is also going to mess up with the rankings.

I will (hopefully) add a test today, but can't now, I have other things to do.

@Veykril Veykril added this pull request to the merge queue Jan 9, 2025
@Veykril
Copy link
Member

Veykril commented Jan 9, 2025

Test can follow whenever then

Merged via the queue into rust-lang:master with commit cc016df Jan 9, 2025
9 checks passed
@ChayimFriedman2 ChayimFriedman2 deleted the subtle-completion branch January 9, 2025 13:00
@davidbarsky
Copy link
Contributor

I’d be happy to add a test: where’s be a good spot?

@ChayimFriedman2
Copy link
Contributor Author

I think it can live in the top-level ide-completion/src/tests.rs. A proc macro isn't important for this; a macro_rules that wraps the entire thing will work just as well. What's important is that there will be a macro call next to the identifier typed, and that the whole thing will be wrapped in a macro call, so that when we fall back (after we failed to expand) to the original position we can't analyze it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

#18723 subtly breaks completions
5 participants