Skip to content

Rust: Adjust the inferred type of string literals #19996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions rust/ql/lib/codeql/rust/internal/TypeInference.qll
Original file line number Diff line number Diff line change
Expand Up @@ -1045,14 +1045,15 @@ private Type inferTryExprType(TryExpr te, TypePath path) {
}

pragma[nomagic]
private StructType inferLiteralType(LiteralExpr le) {
private StructType getStrStruct() { result = TStruct(any(Builtins::Str s)) }

pragma[nomagic]
private Type inferLiteralType(LiteralExpr le, TypePath path) {
path.isEmpty() and
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()
Expand All @@ -1070,6 +1071,14 @@ private StructType inferLiteralType(LiteralExpr le) {
le instanceof BooleanLiteralExpr and
t instanceof Builtins::Bool
)
or
le instanceof StringLiteralExpr and
(
path.isEmpty() and result = TRefType()
or
path = TypePath::singleton(TRefTypeParameter()) and
result = getStrStruct()
)
}

pragma[nomagic]
Expand Down Expand Up @@ -1635,8 +1644,7 @@ private module Cached {
or
result = inferTryExprType(n, path)
or
result = inferLiteralType(n) and
path.isEmpty()
result = inferLiteralType(n, path)
or
result = inferAsyncBlockExprRootType(n) and
path.isEmpty()
Expand Down
10 changes: 5 additions & 5 deletions rust/ql/test/library-tests/type-inference/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ mod builtins {
let z = x + y; // $ type=z:i32 method=add
let z = x.abs(); // $ method=abs $ type=z:i32
let c = 'c'; // $ type=c:char
let hello = "Hello"; // $ type=hello:str
let hello = "Hello"; // $ type=hello:&T.str
let f = 123.0f64; // $ type=f:f64
let t = true; // $ type=t:bool
let f = false; // $ type=f:bool
Expand Down Expand Up @@ -2086,10 +2086,10 @@ mod loops {
let vals4: [u64; 3] = [1; 3]; // $ type=vals4:[T;...].u64
for u in vals4 {} // $ type=u:u64

let mut strings1 = ["foo", "bar", "baz"]; // $ type=strings1:[T;...].str
for s in &strings1 {} // $ type=s:&T.str
for s in &mut strings1 {} // $ type=s:&T.str
for s in strings1 {} // $ type=s:str
let mut strings1 = ["foo", "bar", "baz"]; // $ type=strings1:[T;...].&T.str
for s in &strings1 {} // $ type=s:&T.&T.str
for s in &mut strings1 {} // $ type=s:&T.&T.str
for s in strings1 {} // $ type=s:&T.str

let strings2 = // $ type=strings2:[T;...].String
[
Expand Down
Loading