Skip to content

internal: Explicitly check for reference locals or fields in Name classification #9569

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

Merged
merged 3 commits into from
Jul 11, 2021

Conversation

Veykril
Copy link
Member

@Veykril Veykril commented Jul 11, 2021

Closes #7524
Inlines all the calls to reference related name classification as well as emits both goto definition targets for field shorthands.

@Veykril
Copy link
Member Author

Veykril commented Jul 11, 2021

Guess I'll rebase on top of #9567 once its merged

@Veykril
Copy link
Member Author

Veykril commented Jul 11, 2021

bors r+

bors bot added a commit that referenced this pull request Jul 11, 2021
9569: Explicitly check for reference locals or fields in Name classification r=Veykril a=Veykril

Closes #7524

Co-authored-by: Lukas Wirth <[email protected]>
/// `Definition`, which this name refers to.
pub fn referenced(self) -> Definition {
/// `Definition`, which this name refers to with a preference for the field reference in case of a field shorthand.
pub fn referenced_field(self) -> Definition {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, not sure if that's the best API: or_field feels too specific. I wonder if the best solution here is maybe not to have any methods at all, and just write explicit matches at every call-site? That's more code, but it makes it clear when we ignore some cases...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye the main reason I didn't feel like doing that was due to the extern crate variant which you conveniently got rid of now. So inling all of these might be fine now.

@@ -878,10 +877,11 @@ fn main() {
r#"
enum Foo {
Bar { x: i32 }
} //^
}
fn baz(foo: Foo) {
match foo {
Foo::Bar { x$0 } => x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, think we shouldn't change the test, the previous behavior feels more helpful.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that would be inconsistent in my eyes.
If you were to use goto_defintion on a reference to a local you would go to the pattern definition here, invoking it on there again I would expect to see all references of the local instead of going to the field definiton.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it seems like this actually regresses behavior, let's try avoid breaking this

bors r-

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am, missed the commet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was considering though would be to have goto_declaration instead to go to the field definition in that case while having goto_definiton stick to the local.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, good point about find references...

I guess, there are two different IDE actions: gotodef and findref. If gotodef is invoked, then I think it is clear that it's more helpful to show field definition. If findref is invoked, then it's more helpful to look for usages of local items.

However, the editor also implements a fallback behavior, where invoking gotodef on the def iteslf then invokes findref (i guess the editor just checkst that nav-target range intersects with current cursor position to detect this?).

If we implement gotodef in the more helpful way, than we break editor's fallback.

Seems like fundamentally we just have two different things at the same cursor position, and it's impossible to cover all cases 100% correctly without showing some kind of multiresolve dialog to the user 🤔

Hm, it seems that LSP actually allows us to return both definitions in this case?

https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition

If that's the case, I think the ideal behavior here is to return two locatins for gotodef, and, for findref, look only for local variables.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye if we can return multiple definitions for goto_def that seems like the correct solution then.

@bors
Copy link
Contributor

bors bot commented Jul 11, 2021

Canceled.

@Veykril
Copy link
Member Author

Veykril commented Jul 11, 2021

bors r+

@Veykril Veykril changed the title Explicitly check for reference locals or fields in Name classification internal: Explicitly check for reference locals or fields in Name classification Jul 11, 2021
@bors
Copy link
Contributor

bors bot commented Jul 11, 2021

@bors bors bot merged commit fe00358 into rust-lang:master Jul 11, 2021
@Veykril Veykril deleted the namedefs branch July 11, 2021 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Variables in destructing patterns are highlighted incorrectly
2 participants