Address review comments

This commit is contained in:
Tom Hvitved
2025-05-14 20:50:40 +02:00
parent a02bf182c5
commit 9d37597461
5 changed files with 53 additions and 56 deletions

View File

@@ -76,7 +76,14 @@ module Impl {
/**
* A number literal.
*/
abstract class NumberLiteralExpr extends LiteralExpr { }
abstract class NumberLiteralExpr extends LiteralExpr {
/**
* Get the suffix of this number literal, if any.
*
* For example, `42u8` has the suffix `u8`.
*/
abstract string getSuffix();
}
// https://doc.rust-lang.org/reference/tokens.html#integer-literals
private module IntegerLiteralRegexs {
@@ -126,12 +133,7 @@ module Impl {
class IntegerLiteralExpr extends NumberLiteralExpr {
IntegerLiteralExpr() { this.getTextValue().regexpMatch(IntegerLiteralRegexs::integerLiteral()) }
/**
* Get the suffix of this integer literal, if any.
*
* For example, `42u8` has the suffix `u8`.
*/
string getSuffix() {
override string getSuffix() {
exists(string s, string reg |
s = this.getTextValue() and
reg = IntegerLiteralRegexs::integerLiteral() and
@@ -193,12 +195,7 @@ module Impl {
not this instanceof IntegerLiteralExpr
}
/**
* Get the suffix of this floating-point literal, if any.
*
* For example, `42.0f32` has the suffix `f32`.
*/
string getSuffix() {
override string getSuffix() {
exists(string s, string reg |
reg =
IntegerLiteralRegexs::paren(FloatLiteralRegexs::floatLiteralSuffix1()) + "|" +

View File

@@ -97,13 +97,13 @@ class U128 extends BuiltinType {
}
/** The builtin `usize` type. */
class USize extends BuiltinType {
USize() { this.getName() = "usize" }
class Usize extends BuiltinType {
Usize() { this.getName() = "usize" }
}
/** The builtin `isize` type. */
class ISize extends BuiltinType {
ISize() { this.getName() = "isize" }
class Isize extends BuiltinType {
Isize() { this.getName() = "isize" }
}
/** The builtin `f32` type. */

View File

@@ -887,37 +887,32 @@ private Type inferTryExprType(TryExpr te, TypePath path) {
private import codeql.rust.frameworks.stdlib.Bultins as Builtins
pragma[nomagic]
StructType getBuiltinType(string name) {
result = TStruct(any(Builtins::BuiltinType t | name = t.getName()))
}
pragma[nomagic]
private StructType inferLiteralType(LiteralExpr le) {
le instanceof CharLiteralExpr and
result = TStruct(any(Builtins::Char t))
or
le instanceof StringLiteralExpr and
result = TStruct(any(Builtins::Str t))
or
le =
any(IntegerLiteralExpr n |
not exists(n.getSuffix()) and
result = getBuiltinType("i32")
or
result = getBuiltinType(n.getSuffix())
)
or
le =
any(FloatLiteralExpr n |
not exists(n.getSuffix()) and
result = getBuiltinType("f32")
or
result = getBuiltinType(n.getSuffix())
)
or
le instanceof BooleanLiteralExpr and
result = TStruct(any(Builtins::Bool t))
exists(Builtins::BuiltinType t | result = TStruct(t) |
le instanceof CharLiteralExpr and
t instanceof Builtins::Char
or
le instanceof StringLiteralExpr and
t instanceof Builtins::Str
or
le =
any(NumberLiteralExpr ne |
t.getName() = ne.getSuffix()
or
not exists(ne.getSuffix()) and
(
ne instanceof IntegerLiteralExpr and
t instanceof Builtins::I32
or
ne instanceof FloatLiteralExpr and
t instanceof Builtins::F64
)
)
or
le instanceof BooleanLiteralExpr and
t instanceof Builtins::Bool
)
}
cached

View File

@@ -984,11 +984,11 @@ mod builtins {
let y = 2; // $ type=y:i32
let z = x + y; // $ MISSING: type=z:i32
let z = x.abs(); // $ method=abs $ type=z:i32
'c';
"Hello";
123.0f64;
true;
false;
let c = 'c'; // $ type=c:char
let hello = "Hello"; // $ type=hello:str
let f = 123.0f64; // $ type=f:f64
let t = true; // $ type=t:bool
let f = false; // $ type=f:bool
}
}

View File

@@ -1206,11 +1206,16 @@ inferType
| main.rs:986:13:986:13 | z | | file:///BUILTINS/types.rs:12:1:12:15 | i32 |
| main.rs:986:17:986:17 | x | | file:///BUILTINS/types.rs:12:1:12:15 | i32 |
| main.rs:986:17:986:23 | x.abs() | | file:///BUILTINS/types.rs:12:1:12:15 | i32 |
| main.rs:987:9:987:11 | 'c' | | file:///BUILTINS/types.rs:6:1:7:16 | char |
| main.rs:988:9:988:15 | "Hello" | | file:///BUILTINS/types.rs:8:1:8:15 | str |
| main.rs:989:9:989:16 | 123.0f64 | | file:///BUILTINS/types.rs:25:1:25:15 | f64 |
| main.rs:990:9:990:12 | true | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
| main.rs:991:9:991:13 | false | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
| main.rs:987:13:987:13 | c | | file:///BUILTINS/types.rs:6:1:7:16 | char |
| main.rs:987:17:987:19 | 'c' | | file:///BUILTINS/types.rs:6:1:7:16 | char |
| main.rs:988:13:988:17 | hello | | file:///BUILTINS/types.rs:8:1:8:15 | str |
| main.rs:988:21:988:27 | "Hello" | | file:///BUILTINS/types.rs:8:1:8:15 | str |
| main.rs:989:13:989:13 | f | | file:///BUILTINS/types.rs:25:1:25:15 | f64 |
| main.rs:989:17:989:24 | 123.0f64 | | file:///BUILTINS/types.rs:25:1:25:15 | f64 |
| main.rs:990:13:990:13 | t | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
| main.rs:990:17:990:20 | true | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
| main.rs:991:13:991:13 | f | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
| main.rs:991:17:991:21 | false | | file:///BUILTINS/types.rs:3:1:5:16 | bool |
| main.rs:997:5:997:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
| main.rs:998:5:998:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
| main.rs:998:20:998:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |