Skip to content

Commit 9d37597

Browse files
committed
Address review comments
1 parent a02bf18 commit 9d37597

File tree

5 files changed

+53
-56
lines changed

5 files changed

+53
-56
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ module Impl {
7676
/**
7777
* A number literal.
7878
*/
79-
abstract class NumberLiteralExpr extends LiteralExpr { }
79+
abstract class NumberLiteralExpr extends LiteralExpr {
80+
/**
81+
* Get the suffix of this number literal, if any.
82+
*
83+
* For example, `42u8` has the suffix `u8`.
84+
*/
85+
abstract string getSuffix();
86+
}
8087

8188
// https://doc.rust-lang.org/reference/tokens.html#integer-literals
8289
private module IntegerLiteralRegexs {
@@ -126,12 +133,7 @@ module Impl {
126133
class IntegerLiteralExpr extends NumberLiteralExpr {
127134
IntegerLiteralExpr() { this.getTextValue().regexpMatch(IntegerLiteralRegexs::integerLiteral()) }
128135

129-
/**
130-
* Get the suffix of this integer literal, if any.
131-
*
132-
* For example, `42u8` has the suffix `u8`.
133-
*/
134-
string getSuffix() {
136+
override string getSuffix() {
135137
exists(string s, string reg |
136138
s = this.getTextValue() and
137139
reg = IntegerLiteralRegexs::integerLiteral() and
@@ -193,12 +195,7 @@ module Impl {
193195
not this instanceof IntegerLiteralExpr
194196
}
195197

196-
/**
197-
* Get the suffix of this floating-point literal, if any.
198-
*
199-
* For example, `42.0f32` has the suffix `f32`.
200-
*/
201-
string getSuffix() {
198+
override string getSuffix() {
202199
exists(string s, string reg |
203200
reg =
204201
IntegerLiteralRegexs::paren(FloatLiteralRegexs::floatLiteralSuffix1()) + "|" +

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ class U128 extends BuiltinType {
9797
}
9898

9999
/** The builtin `usize` type. */
100-
class USize extends BuiltinType {
101-
USize() { this.getName() = "usize" }
100+
class Usize extends BuiltinType {
101+
Usize() { this.getName() = "usize" }
102102
}
103103

104104
/** The builtin `isize` type. */
105-
class ISize extends BuiltinType {
106-
ISize() { this.getName() = "isize" }
105+
class Isize extends BuiltinType {
106+
Isize() { this.getName() = "isize" }
107107
}
108108

109109
/** The builtin `f32` type. */

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

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -887,37 +887,32 @@ private Type inferTryExprType(TryExpr te, TypePath path) {
887887

888888
private import codeql.rust.frameworks.stdlib.Bultins as Builtins
889889

890-
pragma[nomagic]
891-
StructType getBuiltinType(string name) {
892-
result = TStruct(any(Builtins::BuiltinType t | name = t.getName()))
893-
}
894-
895890
pragma[nomagic]
896891
private StructType inferLiteralType(LiteralExpr le) {
897-
le instanceof CharLiteralExpr and
898-
result = TStruct(any(Builtins::Char t))
899-
or
900-
le instanceof StringLiteralExpr and
901-
result = TStruct(any(Builtins::Str t))
902-
or
903-
le =
904-
any(IntegerLiteralExpr n |
905-
not exists(n.getSuffix()) and
906-
result = getBuiltinType("i32")
907-
or
908-
result = getBuiltinType(n.getSuffix())
909-
)
910-
or
911-
le =
912-
any(FloatLiteralExpr n |
913-
not exists(n.getSuffix()) and
914-
result = getBuiltinType("f32")
915-
or
916-
result = getBuiltinType(n.getSuffix())
917-
)
918-
or
919-
le instanceof BooleanLiteralExpr and
920-
result = TStruct(any(Builtins::Bool t))
892+
exists(Builtins::BuiltinType t | result = TStruct(t) |
893+
le instanceof CharLiteralExpr and
894+
t instanceof Builtins::Char
895+
or
896+
le instanceof StringLiteralExpr and
897+
t instanceof Builtins::Str
898+
or
899+
le =
900+
any(NumberLiteralExpr ne |
901+
t.getName() = ne.getSuffix()
902+
or
903+
not exists(ne.getSuffix()) and
904+
(
905+
ne instanceof IntegerLiteralExpr and
906+
t instanceof Builtins::I32
907+
or
908+
ne instanceof FloatLiteralExpr and
909+
t instanceof Builtins::F64
910+
)
911+
)
912+
or
913+
le instanceof BooleanLiteralExpr and
914+
t instanceof Builtins::Bool
915+
)
921916
}
922917

923918
cached

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -984,11 +984,11 @@ mod builtins {
984984
let y = 2; // $ type=y:i32
985985
let z = x + y; // $ MISSING: type=z:i32
986986
let z = x.abs(); // $ method=abs $ type=z:i32
987-
'c';
988-
"Hello";
989-
123.0f64;
990-
true;
991-
false;
987+
let c = 'c'; // $ type=c:char
988+
let hello = "Hello"; // $ type=hello:str
989+
let f = 123.0f64; // $ type=f:f64
990+
let t = true; // $ type=t:bool
991+
let f = false; // $ type=f:bool
992992
}
993993
}
994994

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,11 +1206,16 @@ inferType
12061206
| main.rs:986:13:986:13 | z | | file:///BUILTINS/types.rs:12:1:12:15 | i32 |
12071207
| main.rs:986:17:986:17 | x | | file:///BUILTINS/types.rs:12:1:12:15 | i32 |
12081208
| main.rs:986:17:986:23 | x.abs() | | file:///BUILTINS/types.rs:12:1:12:15 | i32 |
1209-
| main.rs:987:9:987:11 | 'c' | | file:///BUILTINS/types.rs:6:1:7:16 | char |
1210-
| main.rs:988:9:988:15 | "Hello" | | file:///BUILTINS/types.rs:8:1:8:15 | str |
1211-
| main.rs:989:9:989:16 | 123.0f64 | | file:///BUILTINS/types.rs:25:1:25:15 | f64 |
1212-
| main.rs:990:9:990:12 | true | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
1213-
| main.rs:991:9:991:13 | false | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
1209+
| main.rs:987:13:987:13 | c | | file:///BUILTINS/types.rs:6:1:7:16 | char |
1210+
| main.rs:987:17:987:19 | 'c' | | file:///BUILTINS/types.rs:6:1:7:16 | char |
1211+
| main.rs:988:13:988:17 | hello | | file:///BUILTINS/types.rs:8:1:8:15 | str |
1212+
| main.rs:988:21:988:27 | "Hello" | | file:///BUILTINS/types.rs:8:1:8:15 | str |
1213+
| main.rs:989:13:989:13 | f | | file:///BUILTINS/types.rs:25:1:25:15 | f64 |
1214+
| main.rs:989:17:989:24 | 123.0f64 | | file:///BUILTINS/types.rs:25:1:25:15 | f64 |
1215+
| main.rs:990:13:990:13 | t | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
1216+
| main.rs:990:17:990:20 | true | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
1217+
| main.rs:991:13:991:13 | f | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
1218+
| main.rs:991:17:991:21 | false | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
12141219
| main.rs:997:5:997:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
12151220
| main.rs:998:5:998:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
12161221
| main.rs:998:20:998:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |

0 commit comments

Comments
 (0)