Skip to content

Rust: extract generic parameters, arguments and resolve bound type variables #19237

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 10 commits into from
Apr 17, 2025
970 changes: 607 additions & 363 deletions rust/extractor/src/crate_graph.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Impl {
*/
class Lifetime extends Generated::Lifetime {
override string toStringImpl() {
result = "'" + this.getText()
result = this.getText()
or
not this.hasText() and result = "'_"
}
Expand Down
13 changes: 13 additions & 0 deletions rust/ql/test/extractor-tests/crate_graph/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ pub static X_B: X = X::B;

pub use std::fs::create_dir as mkdir;
pub use std::{fs::*, path::PathBuf};

pub struct LocalKey<T: 'static> {
inner: fn(Option<&mut Option<T>>) -> *const T,
}

pub struct Thing<T> {
x: T,
}
impl From<usize> for Thing<X> {
fn from(_x: usize) -> Self {
Thing { x: X::A }
}
}
18 changes: 15 additions & 3 deletions rust/ql/test/extractor-tests/crate_graph/modules.expected
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

#-----| fn fmt

#-----| fn from

#-----| fn length

#-----| impl ...::AsString for ...::X { ... }
#-----| -> fn as_string

#-----| impl ...::Display for ...::X { ... }
#-----| -> fn fmt

#-----| impl AsString for ...::X { ... }
#-----| -> fn as_string
#-----| impl ...::From::<...> for ...::Thing::<...> { ... }
#-----| -> fn from

lib.rs:
# 0| mod crate
Expand All @@ -27,8 +32,11 @@ lib.rs:
#-----| -> Static
#-----| -> enum X
#-----| -> fn length
#-----| -> impl ...::AsString for ...::X { ... }
#-----| -> impl ...::Display for ...::X { ... }
#-----| -> impl AsString for ...::X { ... }
#-----| -> impl ...::From::<...> for ...::Thing::<...> { ... }
#-----| -> struct LocalKey<T>
#-----| -> struct Thing<T>
#-----| -> struct X_List
#-----| -> trait AsString
#-----| -> use ...::DirBuilder
Expand Down Expand Up @@ -62,6 +70,10 @@ lib.rs:
#-----| -> use ...::symlink_metadata
#-----| -> use ...::write

#-----| struct LocalKey<T>

#-----| struct Thing<T>

#-----| struct X_List

#-----| trait AsString
Expand Down
29 changes: 28 additions & 1 deletion rust/ql/test/extractor-tests/crate_graph/modules.ql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ class RelevantNode extends Item {
this.getParentNode*() =
any(Crate m | m.getName() = "test" and m.getVersion() = "0.0.1").getModule()
}

string label() { result = this.toString() }
}

class HasGenericParams extends RelevantNode {
private GenericParamList params;

HasGenericParams() {
params = this.(Function).getGenericParamList() or
params = this.(Enum).getGenericParamList() or
params = this.(Struct).getGenericParamList() or
params = this.(Union).getGenericParamList() or
params = this.(Impl).getGenericParamList() or
params = this.(Enum).getGenericParamList() or
params = this.(Trait).getGenericParamList() or
params = this.(TraitAlias).getGenericParamList()
}

override string label() {
result =
super.toString() + "<" +
strictconcat(string part, int index |
part = params.getGenericParam(index).toString()
|
part, ", " order by index
) + ">"
}
}

predicate edges(RelevantNode container, RelevantNode element) {
Expand All @@ -25,7 +52,7 @@ query predicate nodes(RelevantNode node, string attr, string val) {
nodes(node) and
(
attr = "semmle.label" and
val = node.toString()
val = node.label()
or
attr = "semmle.order" and
val =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
| gen_break_expr.rs:7:13:7:17 | break | getNumberOfAttrs: | 0 | hasExpr: | no | hasLifetime: | no |
| gen_break_expr.rs:12:13:12:27 | break ''label 42 | getNumberOfAttrs: | 0 | hasExpr: | yes | hasLifetime: | yes |
| gen_break_expr.rs:17:13:17:27 | break ''label 42 | getNumberOfAttrs: | 0 | hasExpr: | yes | hasLifetime: | yes |
| gen_break_expr.rs:12:13:12:27 | break 'label 42 | getNumberOfAttrs: | 0 | hasExpr: | yes | hasLifetime: | yes |
| gen_break_expr.rs:17:13:17:27 | break 'label 42 | getNumberOfAttrs: | 0 | hasExpr: | yes | hasLifetime: | yes |
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
| gen_break_expr.rs:12:13:12:27 | break ''label 42 | gen_break_expr.rs:12:26:12:27 | 42 |
| gen_break_expr.rs:17:13:17:27 | break ''label 42 | gen_break_expr.rs:17:26:17:27 | 42 |
| gen_break_expr.rs:12:13:12:27 | break 'label 42 | gen_break_expr.rs:12:26:12:27 | 42 |
| gen_break_expr.rs:17:13:17:27 | break 'label 42 | gen_break_expr.rs:17:26:17:27 | 42 |
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
| gen_break_expr.rs:12:13:12:27 | break ''label 42 | gen_break_expr.rs:12:19:12:24 | ''label |
| gen_break_expr.rs:17:13:17:27 | break ''label 42 | gen_break_expr.rs:17:19:17:24 | ''label |
| gen_break_expr.rs:12:13:12:27 | break 'label 42 | gen_break_expr.rs:12:19:12:24 | 'label |
| gen_break_expr.rs:17:13:17:27 | break 'label 42 | gen_break_expr.rs:17:19:17:24 | 'label |
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| gen_continue_expr.rs:12:13:12:27 | continue 'label | gen_continue_expr.rs:12:22:12:27 | ''label |
| gen_continue_expr.rs:12:13:12:27 | continue 'label | gen_continue_expr.rs:12:22:12:27 | 'label |
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| gen_label.rs:5:5:5:11 | 'label | gen_label.rs:5:5:5:10 | ''label |
| gen_label.rs:5:5:5:11 | 'label | gen_label.rs:5:5:5:10 | 'label |
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| gen_self_param.rs:10:15:10:22 | SelfParam | gen_self_param.rs:10:16:10:17 | ''a |
| gen_self_param.rs:10:15:10:22 | SelfParam | gen_self_param.rs:10:16:10:17 | 'a |
2 changes: 1 addition & 1 deletion rust/ql/test/extractor-tests/utf8/ast.expected
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
| utf8_identifiers.rs:1:1:12:2 | SourceFile |
| utf8_identifiers.rs:1:4:1:6 | foo |
| utf8_identifiers.rs:1:7:4:1 | <...> |
| utf8_identifiers.rs:2:5:2:6 | ''\u03b2 |
| utf8_identifiers.rs:2:5:2:6 | '\u03b2 |
| utf8_identifiers.rs:2:5:2:6 | LifetimeParam |
| utf8_identifiers.rs:3:5:3:5 | \u03b3 |
| utf8_identifiers.rs:3:5:3:5 | \u03b3 |
Expand Down
Loading