Skip to content

Commit ebdf41b

Browse files
committed
Rust: implement Expr::getType for cases where rust-analyzer does not provide one
1 parent 0a0af0d commit ebdf41b

File tree

8 files changed

+73
-8
lines changed

8 files changed

+73
-8
lines changed

rust/ql/.generated.list

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/.gitattributes

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/elements/internal/BlockExprImpl.qll

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `BlockExpr`.
43
*
@@ -12,6 +11,7 @@ private import codeql.rust.elements.internal.generated.BlockExpr
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1515
/**
1616
* A block expression. For example:
1717
* ```rust
@@ -26,5 +26,16 @@ module Impl {
2626
* }
2727
* ```
2828
*/
29-
class BlockExpr extends Generated::BlockExpr { }
29+
class BlockExpr extends Generated::BlockExpr {
30+
override string getType() {
31+
result = super.getType()
32+
or
33+
not exists(super.getType()) and
34+
(
35+
result = this.getStmtList().getTailExpr().getType()
36+
or
37+
not exists(this.getStmtList().getTailExpr()) and result = "()"
38+
)
39+
}
40+
}
3041
}

rust/ql/lib/codeql/rust/elements/internal/FormatTemplateVariableAccessImpl.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private import codeql.rust.elements.internal.generated.Synth
1111
private import codeql.rust.elements.Format
1212
private import codeql.rust.elements.NamedFormatArgument
1313
private import codeql.Locations
14+
private import codeql.rust.elements.Variable
1415

1516
/**
1617
* INTERNAL: This module contains the customizable definition of `FormatTemplateVariableAccess` and should not
@@ -36,5 +37,9 @@ module Impl {
3637

3738
/** Gets the underlying `NamedFormatArgument` . */
3839
NamedFormatArgument getArgument() { result = argument }
40+
41+
override string getType() {
42+
result = any(Variable v | v.getAnAccess() = this).getPat().getType()
43+
}
3944
}
4045
}

rust/ql/lib/codeql/rust/elements/internal/IfExprImpl.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,13 @@ module Impl {
3939
or
4040
index = 3 and this.hasElse() and result = "else {...}"
4141
}
42+
43+
override string getType() {
44+
result = super.getType()
45+
or
46+
// For some reason rust-analyzer does not return a type for a chained `if`, so we use the parent's type instead.
47+
not exists(super.getType()) and
48+
result = any(IfExpr parent | parent.getElse() = this).getType()
49+
}
4250
}
4351
}

rust/ql/lib/codeql/rust/elements/internal/LiteralExprImpl.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
private import codeql.rust.elements.internal.generated.LiteralExpr
8+
private import codeql.rust.elements.FormatArgsExpr
9+
private import codeql.rust.elements.LiteralPat
810

911
/**
1012
* INTERNAL: This module contains the customizable definition of `LiteralExpr` and should not
@@ -41,5 +43,13 @@ module Impl {
4143
if v.length() > 30 then result = v.substring(0, 30) + "..." else result = v
4244
)
4345
}
46+
47+
override string getType() {
48+
result = super.getType()
49+
or
50+
exists(FormatArgsExpr f | f.getTemplate() = this and result = "&str")
51+
or
52+
result = any(LiteralPat p | p.getLiteral() = this).getType()
53+
}
4454
}
4555
}
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `MacroExpr`.
43
*
54
* INTERNAL: Do not use.
65
*/
76

87
private import codeql.rust.elements.internal.generated.MacroExpr
8+
private import codeql.rust.elements.MacroStmts
99

1010
/**
1111
* INTERNAL: This module contains the customizable definition of `MacroExpr` and should not
1212
* be referenced directly.
1313
*/
1414
module Impl {
15+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1516
/**
1617
* A MacroExpr. For example:
1718
* ```rust
1819
* todo!()
1920
* ```
2021
*/
21-
class MacroExpr extends Generated::MacroExpr { }
22+
class MacroExpr extends Generated::MacroExpr {
23+
override string getType() {
24+
result = super.getType()
25+
or
26+
not exists(super.getType()) and
27+
(
28+
result = this.getMacroCall().getExpanded().(Expr).getType() or
29+
result = typeOf(this.getMacroCall().getExpanded().(MacroStmts))
30+
)
31+
}
32+
}
33+
34+
string typeOf(MacroStmts stmts) {
35+
result = stmts.getExpr().getType()
36+
or
37+
not exists(stmts.getExpr()) and result = "()"
38+
}
2239
}

rust/ql/lib/codeql/rust/elements/internal/PathExprImpl.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
private import codeql.rust.elements.internal.generated.PathExpr
8+
private import codeql.rust.elements.CallExpr
89

910
/**
1011
* INTERNAL: This module contains the customizable definition of `PathExpr` and should not
@@ -25,5 +26,22 @@ module Impl {
2526
override string toString() { result = this.toAbbreviatedString() }
2627

2728
override string toAbbreviatedString() { result = this.getPath().toString() }
29+
30+
override string getType() {
31+
result = super.getType()
32+
or
33+
// Special case for `rustc_box`; these get translated to `box X` in the HIR layer.
34+
// ```rust
35+
// #[rustc_box]
36+
// alloc.boxed::Box::new(X)
37+
// ```
38+
not exists(super.getType()) and
39+
exists(CallExpr c, string tp |
40+
c.getAnAttr().getMeta().getPath().getPart().getNameRef().getText() = "rustc_box" and
41+
this = c.getExpr() and
42+
tp = c.getArgList().getArg(0).getType() and
43+
result = "fn new<T>(T) -> Box<T, Global>".replaceAll("T", tp)
44+
)
45+
}
2846
}
2947
}

0 commit comments

Comments
 (0)