Skip to content

Commit 08f025f

Browse files
committed
Rust: Infer range expressions certainly and support full ranges
1 parent 4c10f07 commit 08f025f

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

rust/ql/lib/codeql/rust/elements/RangeExprExt.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ final class RangeFromToExpr extends RangeExpr {
4646
}
4747
}
4848

49+
/**
50+
* A range-full expression. For example:
51+
* ```rust
52+
* let x = ..;
53+
* ```
54+
*/
55+
final class RangeFullExpr extends RangeExpr {
56+
RangeFullExpr() {
57+
this.getOperatorName() = ".." and
58+
not this.hasStart() and
59+
not this.hasEnd()
60+
}
61+
}
62+
4963
/**
5064
* A range-inclusive expression. For example:
5165
* ```rust

rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ class RangeToStruct extends Struct {
9494
StructField getEnd() { result = this.getStructField("end") }
9595
}
9696

97+
/**
98+
* The [`RangeFull` struct][1].
99+
*
100+
* [1]: https://doc.rust-lang.org/core/ops/struct.RangeFull.html
101+
*/
102+
class RangeFullStruct extends Struct {
103+
pragma[nomagic]
104+
RangeFullStruct() { this.getCanonicalPath() = "core::ops::range::RangeFull" }
105+
}
106+
97107
/**
98108
* The [`RangeInclusive` struct][1].
99109
*

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ private module CertainTypeInference {
385385
or
386386
result = inferLogicalOperationType(n, path)
387387
or
388+
result = inferRangeExprType(n) and
389+
path.isEmpty()
390+
or
388391
result = inferTupleRootType(n) and
389392
path.isEmpty()
390393
or
@@ -463,6 +466,9 @@ private Struct getRangeType(RangeExpr re) {
463466
re instanceof RangeToExpr and
464467
result instanceof RangeToStruct
465468
or
469+
re instanceof RangeFullExpr and
470+
result instanceof RangeFullStruct
471+
or
466472
re instanceof RangeFromToExpr and
467473
result instanceof RangeStruct
468474
or
@@ -2402,9 +2408,6 @@ private module Cached {
24022408
or
24032409
result = inferAwaitExprType(n, path)
24042410
or
2405-
result = inferRangeExprType(n) and
2406-
path.isEmpty()
2407-
or
24082411
result = inferIndexExprType(n, path)
24092412
or
24102413
result = inferForLoopExprType(n, path)

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,7 @@ mod loops {
23322332
for u in [0u8..10] {} // $ type=u:Range type=u:Idx.u8
23332333
let range = 0..10; // $ type=range:Range type=range:Idx.i32
23342334
for i in range {} // $ type=i:i32
2335-
let range_full = ..; // $ MISSING: type=range_full:RangeFull
2335+
let range_full = ..; // $ type=range_full:RangeFull
23362336
for i in &[1i64, 2i64, 3i64][range_full] {} // $ target=index MISSING: type=i:&T.i64
23372337

23382338
let range1 = // $ type=range1:Range type=range1:Idx.u16

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,13 +4367,16 @@ inferType
43674367
| main.rs:2334:13:2334:13 | i | | {EXTERNAL LOCATION} | i32 |
43684368
| main.rs:2334:18:2334:22 | range | | {EXTERNAL LOCATION} | Range |
43694369
| main.rs:2334:18:2334:22 | range | Idx | {EXTERNAL LOCATION} | i32 |
4370+
| main.rs:2335:13:2335:22 | range_full | | {EXTERNAL LOCATION} | RangeFull |
4371+
| main.rs:2335:26:2335:27 | .. | | {EXTERNAL LOCATION} | RangeFull |
43704372
| main.rs:2336:13:2336:13 | i | | {EXTERNAL LOCATION} | Item |
43714373
| main.rs:2336:18:2336:48 | &... | | file://:0:0:0:0 | & |
43724374
| main.rs:2336:19:2336:36 | [...] | | file://:0:0:0:0 | [] |
43734375
| main.rs:2336:19:2336:36 | [...] | [T;...] | {EXTERNAL LOCATION} | i64 |
43744376
| main.rs:2336:20:2336:23 | 1i64 | | {EXTERNAL LOCATION} | i64 |
43754377
| main.rs:2336:26:2336:29 | 2i64 | | {EXTERNAL LOCATION} | i64 |
43764378
| main.rs:2336:32:2336:35 | 3i64 | | {EXTERNAL LOCATION} | i64 |
4379+
| main.rs:2336:38:2336:47 | range_full | | {EXTERNAL LOCATION} | RangeFull |
43774380
| main.rs:2338:13:2338:18 | range1 | | {EXTERNAL LOCATION} | Range |
43784381
| main.rs:2338:13:2338:18 | range1 | Idx | {EXTERNAL LOCATION} | u16 |
43794382
| main.rs:2339:9:2342:9 | ...::Range {...} | | {EXTERNAL LOCATION} | Range |

0 commit comments

Comments
 (0)