Skip to content

Commit 73f854f

Browse files
committed
Rust: Adjust the inferred type of string literals
1 parent 411aa6d commit 73f854f

File tree

3 files changed

+280
-143
lines changed

3 files changed

+280
-143
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,14 +1045,12 @@ private Type inferTryExprType(TryExpr te, TypePath path) {
10451045
}
10461046

10471047
pragma[nomagic]
1048-
private StructType inferLiteralType(LiteralExpr le) {
1048+
private Type inferLiteralType(LiteralExpr le, TypePath path) {
1049+
path.isEmpty() and
10491050
exists(Builtins::BuiltinType t | result = TStruct(t) |
10501051
le instanceof CharLiteralExpr and
10511052
t instanceof Builtins::Char
10521053
or
1053-
le instanceof StringLiteralExpr and
1054-
t instanceof Builtins::Str
1055-
or
10561054
le =
10571055
any(NumberLiteralExpr ne |
10581056
t.getName() = ne.getSuffix()
@@ -1070,6 +1068,14 @@ private StructType inferLiteralType(LiteralExpr le) {
10701068
le instanceof BooleanLiteralExpr and
10711069
t instanceof Builtins::Bool
10721070
)
1071+
or
1072+
le instanceof StringLiteralExpr and
1073+
(
1074+
path.isEmpty() and result = TRefType()
1075+
or
1076+
path = TypePath::singleton(TRefTypeParameter()) and
1077+
result = TStruct(any(Builtins::Str s))
1078+
)
10731079
}
10741080

10751081
pragma[nomagic]
@@ -1635,8 +1641,7 @@ private module Cached {
16351641
or
16361642
result = inferTryExprType(n, path)
16371643
or
1638-
result = inferLiteralType(n) and
1639-
path.isEmpty()
1644+
result = inferLiteralType(n, path)
16401645
or
16411646
result = inferAsyncBlockExprRootType(n) and
16421647
path.isEmpty()

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ mod builtins {
14131413
let z = x + y; // $ type=z:i32 method=add
14141414
let z = x.abs(); // $ method=abs $ type=z:i32
14151415
let c = 'c'; // $ type=c:char
1416-
let hello = "Hello"; // $ type=hello:str
1416+
let hello = "Hello"; // $ type=hello:&T.str
14171417
let f = 123.0f64; // $ type=f:f64
14181418
let t = true; // $ type=t:bool
14191419
let f = false; // $ type=f:bool
@@ -2086,10 +2086,10 @@ mod loops {
20862086
let vals4: [u64; 3] = [1; 3]; // $ type=vals4:[T;...].u64
20872087
for u in vals4 {} // $ type=u:u64
20882088

2089-
let mut strings1 = ["foo", "bar", "baz"]; // $ type=strings1:[T;...].str
2090-
for s in &strings1 {} // $ type=s:&T.str
2091-
for s in &mut strings1 {} // $ type=s:&T.str
2092-
for s in strings1 {} // $ type=s:str
2089+
let mut strings1 = ["foo", "bar", "baz"]; // $ type=strings1:[T;...].&T.str
2090+
for s in &strings1 {} // $ type=s:&T.&T.str
2091+
for s in &mut strings1 {} // $ type=s:&T.&T.str
2092+
for s in strings1 {} // $ type=s:&T.str
20932093

20942094
let strings2 = // $ type=strings2:[T;...].String
20952095
[

0 commit comments

Comments
 (0)