Skip to content

Commit 156f867

Browse files
authored
Merge pull request #19996 from hvitved/rust/type-inference-str-literal
Rust: Adjust the inferred type of string literals
2 parents 4f1ca21 + 22b833f commit 156f867

File tree

5 files changed

+286
-148
lines changed

5 files changed

+286
-148
lines changed

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

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

10471047
pragma[nomagic]
1048-
private StructType inferLiteralType(LiteralExpr le) {
1048+
private StructType getStrStruct() { result = TStruct(any(Builtins::Str s)) }
1049+
1050+
pragma[nomagic]
1051+
private Type inferLiteralType(LiteralExpr le, TypePath path) {
1052+
path.isEmpty() and
10491053
exists(Builtins::BuiltinType t | result = TStruct(t) |
10501054
le instanceof CharLiteralExpr and
10511055
t instanceof Builtins::Char
10521056
or
1053-
le instanceof StringLiteralExpr and
1054-
t instanceof Builtins::Str
1055-
or
10561057
le =
10571058
any(NumberLiteralExpr ne |
10581059
t.getName() = ne.getSuffix()
@@ -1070,6 +1071,14 @@ private StructType inferLiteralType(LiteralExpr le) {
10701071
le instanceof BooleanLiteralExpr and
10711072
t instanceof Builtins::Bool
10721073
)
1074+
or
1075+
le instanceof StringLiteralExpr and
1076+
(
1077+
path.isEmpty() and result = TRefType()
1078+
or
1079+
path = TypePath::singleton(TRefTypeParameter()) and
1080+
result = getStrStruct()
1081+
)
10731082
}
10741083

10751084
pragma[nomagic]
@@ -1635,8 +1644,7 @@ private module Cached {
16351644
or
16361645
result = inferTryExprType(n, path)
16371646
or
1638-
result = inferLiteralType(n) and
1639-
path.isEmpty()
1647+
result = inferLiteralType(n, path)
16401648
or
16411649
result = inferAsyncBlockExprRootType(n) and
16421650
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)