-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Add ConstAccess class with getConst() predicate #20736
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
+105
−4
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /** | ||
| * This module provides the public class `ConstAccess`. | ||
| */ | ||
|
|
||
| private import internal.ConstImpl | ||
|
|
||
| final class ConstAccess = Impl::ConstAccess; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
rust/ql/test/library-tests/const_access/const_access.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| testFailures | ||
| constAccess | ||
| | main.rs:17:13:17:24 | GLOBAL_CONST | main.rs:1:1:1:29 | Const | | ||
| | main.rs:19:13:19:24 | STRING_CONST | main.rs:2:1:2:35 | Const | | ||
| | main.rs:21:13:21:33 | ...::ASSOC_CONST | main.rs:9:5:9:33 | Const | | ||
| | main.rs:23:13:23:35 | ...::MODULE_CONST | main.rs:13:5:13:38 | Const | | ||
| | main.rs:25:8:25:19 | GLOBAL_CONST | main.rs:1:1:1:29 | Const | | ||
| | main.rs:29:16:29:36 | ...::ASSOC_CONST | main.rs:9:5:9:33 | Const | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import rust | ||
| import utils.test.InlineExpectationsTest | ||
| import TestUtils | ||
|
|
||
| query predicate constAccess(ConstAccess ca, Const c) { toBeTested(ca) and c = ca.getConst() } | ||
|
|
||
| module ConstAccessTest implements TestSig { | ||
| string getARelevantTag() { result = "const_access" } | ||
|
|
||
| predicate hasActualResult(Location location, string element, string tag, string value) { | ||
| exists(ConstAccess ca | | ||
| toBeTested(ca) and | ||
| location = ca.getLocation() and | ||
| element = ca.toString() and | ||
| tag = "const_access" and | ||
| value = ca.getConst().getName().getText() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| import MakeTest<ConstAccessTest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| const GLOBAL_CONST: i32 = 42; | ||
| const STRING_CONST: &str = "hello"; | ||
|
|
||
| struct MyStruct { | ||
| value: i32, | ||
| } | ||
|
|
||
| impl MyStruct { | ||
| const ASSOC_CONST: i32 = 100; | ||
| } | ||
|
|
||
| mod my_module { | ||
| pub const MODULE_CONST: i32 = 200; | ||
| } | ||
|
|
||
| fn use_consts() { | ||
| let x = GLOBAL_CONST; // $ const_access=GLOBAL_CONST | ||
|
|
||
| let s = STRING_CONST; // $ const_access=STRING_CONST | ||
|
|
||
| let y = MyStruct::ASSOC_CONST; // $ const_access=ASSOC_CONST | ||
|
|
||
| let z = my_module::MODULE_CONST; // $ const_access=MODULE_CONST | ||
|
|
||
| if GLOBAL_CONST > 0 { // $ const_access=GLOBAL_CONST | ||
| println!("positive"); | ||
| } | ||
|
|
||
| let arr = [MyStruct::ASSOC_CONST; 5]; // $ const_access=ASSOC_CONST | ||
| } | ||
|
|
||
| fn main() { | ||
| use_consts(); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would have expected
ConstAccessto extendVariableAccess(and re-use thegetVariable()method).Uh oh!
There was an error while loading. Please reload this page.
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.
I think keeping them separate is easier, and also makes more sense IMO (a "constant variable" is a bit of an oxymoron).
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.
That's true.
I was thinking more about how typical QL involving variables (e.g. looking for initializers) is likely to want to look at constants just the same.
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.
I think in most other languages we model a constant as a variable with a
constflag or specifier or some such.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.
We had a discussion about this, conclusion is that we can address this later if there turns out to be a need.