Skip to content

Commit cf0b3b5

Browse files
authored
Merge pull request github#18632 from hvitved/rust/type-inference
Rust: Implement basic type inference in QL
2 parents a3ef137 + c3739d4 commit cf0b3b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+4295
-200
lines changed

rust/ql/.generated.list

Lines changed: 0 additions & 1 deletion
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 & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @name Path resolution inconsistencies
3+
* @description Lists the path resolution inconsistencies in the database. This query is intended for internal use.
4+
* @kind table
5+
* @id rust/diagnostics/path-resolution-consistency
6+
*/
7+
8+
import codeql.rust.internal.PathResolutionConsistency
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @name Type inference inconsistencies
3+
* @description Lists the type inference inconsistencies in the database. This query is intended for internal use.
4+
* @kind table
5+
* @id rust/diagnostics/type-inference-consistency
6+
*/
7+
8+
import codeql.rust.internal.TypeInferenceConsistency

rust/ql/integration-tests/hello-project/summary.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| Files extracted - without errors % | 80 |
99
| Inconsistencies - AST | 0 |
1010
| Inconsistencies - CFG | 0 |
11+
| Inconsistencies - Path resolution | 0 |
1112
| Inconsistencies - data flow | 0 |
1213
| Lines of code extracted | 6 |
1314
| Lines of user code extracted | 6 |

rust/ql/integration-tests/hello-workspace/summary.cargo.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| Files extracted - without errors % | 100 |
99
| Inconsistencies - AST | 0 |
1010
| Inconsistencies - CFG | 0 |
11+
| Inconsistencies - Path resolution | 0 |
1112
| Inconsistencies - data flow | 0 |
1213
| Lines of code extracted | 9 |
1314
| Lines of user code extracted | 9 |

rust/ql/integration-tests/hello-workspace/summary.rust-project.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| Files extracted - without errors % | 100 |
99
| Inconsistencies - AST | 0 |
1010
| Inconsistencies - CFG | 0 |
11+
| Inconsistencies - Path resolution | 0 |
1112
| Inconsistencies - data flow | 0 |
1213
| Lines of code extracted | 9 |
1314
| Lines of user code extracted | 9 |

rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,8 @@ final class RecordExprCfgNode extends Nodes::RecordExprCfgNode {
239239
pragma[nomagic]
240240
ExprCfgNode getFieldExpr(string field) {
241241
exists(RecordExprField ref |
242-
ref = node.getRecordExprFieldList().getAField() and
243-
any(ChildMapping mapping).hasCfgChild(node, ref.getExpr(), this, result) and
244-
field = ref.getFieldName()
242+
ref = node.getFieldExpr(field) and
243+
any(ChildMapping mapping).hasCfgChild(node, ref.getExpr(), this, result)
245244
)
246245
}
247246
}

rust/ql/lib/codeql/rust/dataflow/internal/DataFlowImpl.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private import codeql.dataflow.internal.DataFlowImpl
1010
private import rust
1111
private import SsaImpl as SsaImpl
1212
private import codeql.rust.controlflow.internal.Scope as Scope
13-
private import codeql.rust.elements.internal.PathResolution
13+
private import codeql.rust.internal.PathResolution
1414
private import codeql.rust.controlflow.ControlFlowGraph
1515
private import codeql.rust.controlflow.CfgNodes
1616
private import codeql.rust.dataflow.Ssa
@@ -847,7 +847,7 @@ class TupleFieldContent extends FieldContent, TTupleFieldContent {
847847

848848
predicate isStructField(Struct s, int pos) { field.isStructField(s, pos) }
849849

850-
override FieldExprCfgNode getAnAccess() { none() } // TODO
850+
override FieldExprCfgNode getAnAccess() { field = result.getFieldExpr().getTupleField() }
851851

852852
final override string toString() {
853853
exists(Variant v, int pos, string vname |
@@ -878,7 +878,7 @@ class RecordFieldContent extends FieldContent, TRecordFieldContent {
878878

879879
predicate isStructField(Struct s, string name) { field.isStructField(s, name) }
880880

881-
override FieldExprCfgNode getAnAccess() { none() } // TODO
881+
override FieldExprCfgNode getAnAccess() { field = result.getFieldExpr().getRecordField() }
882882

883883
final override string toString() {
884884
exists(Variant v, string name, string vname |

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ private import codeql.rust.elements.Resolvable
1212
* be referenced directly.
1313
*/
1414
module Impl {
15-
private import codeql.rust.elements.internal.CallableImpl::Impl
16-
private import codeql.rust.elements.internal.MethodCallExprImpl::Impl
17-
private import codeql.rust.elements.internal.CallExprImpl::Impl
18-
private import codeql.rust.elements.internal.PathExprImpl::Impl
19-
private import codeql.rust.elements.internal.PathResolution
15+
private import rust
2016

2117
pragma[nomagic]
2218
Resolvable getCallResolvable(CallExprBase call) {
@@ -30,14 +26,7 @@ module Impl {
3026
* A function or method call expression. See `CallExpr` and `MethodCallExpr` for further details.
3127
*/
3228
class CallExprBase extends Generated::CallExprBase {
33-
/**
34-
* Gets the target callable of this call, if a unique such target can
35-
* be statically resolved.
36-
*/
37-
Callable getStaticTarget() {
38-
getCallResolvable(this).resolvesAsItem(result)
39-
or
40-
result = resolvePath(this.(CallExpr).getFunction().(PathExpr).getPath())
41-
}
29+
/** Gets the static target of this call, if any. */
30+
Callable getStaticTarget() { none() } // overridden by subclasses, but cannot be made abstract
4231
}
4332
}

0 commit comments

Comments
 (0)