-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Jump-to-def for operations and indexing #20900
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,19 @@ abstract class Use extends Locatable { | |||||||
| * Gets the type of use. | ||||||||
| */ | ||||||||
| abstract string getUseType(); | ||||||||
|
|
||||||||
| /** | ||||||||
| * Holds if this element is at the specified location. | ||||||||
| * The location spans column `startcolumn` of line `startline` to | ||||||||
| * column `endcolumn` of line `endline` in file `filepath`. | ||||||||
| * For more information, see | ||||||||
| * [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/). | ||||||||
| */ | ||||||||
| predicate hasLocationInfo( | ||||||||
| string filepath, int startline, int startcolumn, int endline, int endcolumn | ||||||||
| ) { | ||||||||
| this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| cached | ||||||||
|
|
@@ -166,6 +179,43 @@ private class MethodUse extends Use instanceof NameRef { | |||||||
| override string getUseType() { result = "method" } | ||||||||
| } | ||||||||
|
|
||||||||
| private class OperationUse extends Use instanceof Operation { | ||||||||
| override Definition getDefinition() { result.asItemNode() = this.(Call).getStaticTarget() } | ||||||||
|
|
||||||||
| override string getUseType() { result = "method" } | ||||||||
|
|
||||||||
| override predicate hasLocationInfo( | ||||||||
| string filepath, int startline, int startcolumn, int endline, int endcolumn | ||||||||
| ) { | ||||||||
| // We don't have entities for the operator symbols, so approximate a location | ||||||||
| this = | ||||||||
| any(PrefixExpr pe | | ||||||||
| pe.getLocation().hasLocationInfo(filepath, startline, startcolumn, _, _) and | ||||||||
| pe.getExpr().getLocation().hasLocationInfo(_, endline, endcolumn + 2, _, _) | ||||||||
| ) | ||||||||
| or | ||||||||
| this = | ||||||||
| any(BinaryExpr be | | ||||||||
| be.getLhs().getLocation().hasLocationInfo(filepath, _, _, startline, startcolumn - 2) and | ||||||||
| be.getRhs().getLocation().hasLocationInfo(filepath, endline, endcolumn + 2, _, _) | ||||||||
| ) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| private class IndexExprUse extends Use instanceof IndexExpr { | ||||||||
| override Definition getDefinition() { result.asItemNode() = this.(Call).getStaticTarget() } | ||||||||
|
|
||||||||
| override string getUseType() { result = "method" } | ||||||||
|
|
||||||||
| override predicate hasLocationInfo( | ||||||||
| string filepath, int startline, int startcolumn, int endline, int endcolumn | ||||||||
| ) { | ||||||||
| // We don't have entities for the brackets, so approximate a location | ||||||||
| super.getIndex().getLocation().hasLocationInfo(filepath, _, _, startline, startcolumn - 2) and | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
? |
||||||||
| this.getLocation().hasLocationInfo(_, _, _, endline, endcolumn) | ||||||||
|
Comment on lines
+237
to
+238
|
||||||||
| super.getIndex().getLocation().hasLocationInfo(filepath, _, _, startline, startcolumn - 2) and | |
| this.getLocation().hasLocationInfo(_, _, _, endline, endcolumn) | |
| this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,7 +68,10 @@ | |
| | main.rs:66:14:66:14 | S | main.rs:3:1:4:9 | struct S | path | | ||
| | main.rs:66:18:66:20 | new | main.rs:46:9:48:9 | fn new | path | | ||
| | main.rs:66:22:66:22 | S | main.rs:3:1:4:9 | struct S | path | | ||
| | main.rs:67:5:67:4 | - ... | main.rs:11:5:13:5 | fn neg | method | | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what I did initially, but that means putting the cursor between
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's odd - I'm not sure what you mean about "putting the cursor between" two characters - surely the granularity here is which single character you're pointing at?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I still don't know what you mean here. Surely your mouse is either over the |
||
| | main.rs:67:6:67:6 | S | main.rs:3:1:4:9 | struct S | path | | ||
| | main.rs:68:5:68:5 | S | main.rs:3:1:4:9 | struct S | path | | ||
| | main.rs:68:7:68:7 | ... + ... | main.rs:19:5:21:5 | fn add | method | | ||
| | main.rs:68:9:68:9 | S | main.rs:3:1:4:9 | struct S | path | | ||
| | main.rs:69:5:69:5 | S | main.rs:3:1:4:9 | struct S | path | | ||
| | main.rs:69:9:69:8 | S[0] | main.rs:27:5:29:5 | fn index | method | | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The location calculation appears to have the start and end columns inverted. For a prefix expression like
-Son line 67, the expected output shows67:5:67:4(start column 5, end column 4), where the end column is before the start column. This will likely cause issues with jump-to-definition functionality.Looking at the logic:
pe.getLocation().hasLocationInfo(filepath, startline, startcolumn, _, _)gets the prefix expression's start locationpe.getExpr().getLocation().hasLocationInfo(_, endline, endcolumn + 2, _, _)gets the operand's end locationThe issue is that for
-S, the prefix expression location is the entire-S, and the operandScomes after the-. So using the prefix expression's start column and the operand's end column (which comes before it) results in inverted columns.Consider calculating the operator location more directly, or ensure endcolumn >= startcolumn.