Skip to content

Commit 240377f

Browse files
committed
Rust: Change inline expectation annotation for certain inferred types
1 parent 0c5c798 commit 240377f

File tree

6 files changed

+78
-75
lines changed

6 files changed

+78
-75
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private Type inferAnnotatedType(AstNode n, TypePath path) {
257257
}
258258

259259
/** Module for inferring certain type information. */
260-
private module CertainTypeInference {
260+
module CertainTypeInference {
261261
pragma[nomagic]
262262
private predicate callResolvesTo(CallExpr ce, Path p, Function f) {
263263
p = CallExprImpl::getFunctionPath(ce) and
@@ -286,7 +286,7 @@ private module CertainTypeInference {
286286
}
287287

288288
pragma[nomagic]
289-
Type inferCertainCallExprType(CallExpr ce, TypePath path) {
289+
private Type inferCertainCallExprType(CallExpr ce, TypePath path) {
290290
exists(Type ty, TypePath prefix, Path p | ty = getCertainCallExprType(ce, p, prefix) |
291291
exists(TypePath suffix, TypeParam tp |
292292
tp = ty.(TypeParamTypeParameter).getTypeParam() and

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

Whitespace-only changes.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod simple_closures {
55
// A simple closure without type annotations or invocations.
66
let my_closure = |a, b| a && b;
77

8-
let x: i64 = 1i64; // $ type=x:i64
8+
let x: i64 = 1i64; // $ certainType=x:i64
99
let add_one = |n| n + 1i64; // $ target=add
1010
let _y = add_one(x); // $ type=_y:i64
1111

@@ -27,7 +27,7 @@ mod simple_closures {
2727
// The return type of `id2` is inferred from the type of the call expression.
2828
let id2 = |b| b;
2929
let arg = Default::default(); // $ target=default type=arg:bool
30-
let _b2: bool = id2(arg); // $ type=_b2:bool
30+
let _b2: bool = id2(arg); // $ certainType=_b2:bool
3131
}
3232
}
3333

@@ -60,7 +60,7 @@ mod fn_once_trait {
6060
let _r = apply(f, true); // $ target=apply type=_r:i64
6161

6262
let f = |x| x + 1; // $ MISSING: type=x:i64 target=add
63-
let _r2 = apply_two(f); // $ target=apply_two type=_r2:i64
63+
let _r2 = apply_two(f); // $ target=apply_two certainType=_r2:i64
6464
}
6565
}
6666

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

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,20 +1140,20 @@ mod type_aliases {
11401140
println!("{:?}", p1);
11411141

11421142
// Type can be only inferred from the type alias
1143-
let p2: MyPair = PairOption::PairNone(); // $ type=p2:Fst.S1 type=p2:Snd.S2
1143+
let p2: MyPair = PairOption::PairNone(); // $ certainType=p2:Fst.S1 certainType=p2:Snd.S2
11441144
println!("{:?}", p2);
11451145

11461146
// First type from alias, second from constructor
1147-
let p3: AnotherPair<_> = PairOption::PairSnd(S3); // $ type=p3:Fst.S2
1147+
let p3: AnotherPair<_> = PairOption::PairSnd(S3); // $ certainType=p3:Fst.S2
11481148
println!("{:?}", p3);
11491149

11501150
// First type from alias definition, second from argument to alias
1151-
let p3: AnotherPair<S3> = PairOption::PairNone(); // $ type=p3:Fst.S2 type=p3:Snd.S3
1151+
let p3: AnotherPair<S3> = PairOption::PairNone(); // $ certainType=p3:Fst.S2 certainType=p3:Snd.S3
11521152
println!("{:?}", p3);
11531153

11541154
g(PairOption::PairSnd(PairOption::PairSnd(S3))); // $ target=g
11551155

1156-
let x: S7<S2>; // $ type=x:Result $ type=x:E.S1 $ type=x:T.S4 $ type=x:T.T41.S2 $ type=x:T.T42.S5 $ type=x:T.T42.T5.S2
1156+
let x: S7<S2>; // $ certainType=x:Result $ certainType=x:E.S1 $ certainType=x:T.S4 $ certainType=x:T.T41.S2 $ certainType=x:T.T42.S5 $ certainType=x:T.T42.T5.S2
11571157

11581158
let y = GenS(true).get_input(); // $ type=y:Result type=y:T.bool type=y:E.bool target=get_input
11591159
}
@@ -1199,7 +1199,7 @@ mod option_methods {
11991199
struct S;
12001200

12011201
pub fn f() {
1202-
let x1 = MyOption::<S>::new(); // $ type=x1:T.S target=new
1202+
let x1 = MyOption::<S>::new(); // $ certainType=x1:T.S target=new
12031203
println!("{:?}", x1);
12041204

12051205
let mut x2 = MyOption::new(); // $ target=new
@@ -1327,7 +1327,7 @@ mod method_call_type_conversion {
13271327
let t = x7.m1(); // $ target=m1 type=t:& type=t:&T.S2
13281328
println!("{:?}", x7);
13291329

1330-
let x9: String = "Hello".to_string(); // $ type=x9:String
1330+
let x9: String = "Hello".to_string(); // $ certainType=x9:String
13311331

13321332
// Implicit `String` -> `str` conversion happens via the `Deref` trait:
13331333
// https://doc.rust-lang.org/std/string/struct.String.html#deref.
@@ -1502,23 +1502,23 @@ mod try_expressions {
15021502

15031503
mod builtins {
15041504
pub fn f() {
1505-
let x: i32 = 1; // $ type=x:i32
1505+
let x: i32 = 1; // $ certainType=x:i32
15061506
let y = 2; // $ type=y:i32
15071507
let z = x + y; // $ type=z:i32 target=add
15081508
let z = x.abs(); // $ target=abs $ type=z:i32
1509-
let c = 'c'; // $ type=c:char
1510-
let hello = "Hello"; // $ type=hello:&T.str
1511-
let f = 123.0f64; // $ type=f:f64
1512-
let t = true; // $ type=t:bool
1513-
let f = false; // $ type=f:bool
1509+
let c = 'c'; // $ certainType=c:char
1510+
let hello = "Hello"; // $ certainType=hello:&T.str
1511+
let f = 123.0f64; // $ certainType=f:f64
1512+
let t = true; // $ certainType=t:bool
1513+
let f = false; // $ certainType=f:bool
15141514
}
15151515
}
15161516

15171517
// Tests for non-overloaded operators.
15181518
mod operators {
15191519
pub fn f() {
1520-
let x = true && false; // $ type=x:bool
1521-
let y = true || false; // $ type=y:bool
1520+
let x = true && false; // $ certainType=x:bool
1521+
let y = true || false; // $ certainType=y:bool
15221522

15231523
let mut a;
15241524
let cond = 34 == 33; // $ target=eq
@@ -2292,10 +2292,10 @@ mod loops {
22922292
let vals2 = [1u16; 3]; // $ type=vals2:[T;...].u16
22932293
for u in vals2 {} // $ type=u:u16
22942294

2295-
let vals3: [u32; 3] = [1, 2, 3]; // $ type=vals3:[T;...].u32
2295+
let vals3: [u32; 3] = [1, 2, 3]; // $ certainType=vals3:[T;...].u32
22962296
for u in vals3 {} // $ type=u:u32
22972297

2298-
let vals4: [u64; 3] = [1; 3]; // $ type=vals4:[T;...].u64
2298+
let vals4: [u64; 3] = [1; 3]; // $ certainType=vals4:[T;...].u64
22992299
for u in vals4 {} // $ type=u:u64
23002300

23012301
let mut strings1 = ["foo", "bar", "baz"]; // $ type=strings1:[T;...].&T.str
@@ -2330,9 +2330,9 @@ mod loops {
23302330

23312331
for i in 0..10 {} // $ type=i:i32
23322332
for u in [0u8..10] {} // $ type=u:Range type=u:Idx.u8
2333-
let range = 0..10; // $ type=range:Range type=range:Idx.i32
2333+
let range = 0..10; // $ certainType=range:Range type=range:Idx.i32
23342334
for i in range {} // $ type=i:i32
2335-
let range_full = ..; // $ type=range_full:RangeFull
2335+
let range_full = ..; // $ certainType=range_full:RangeFull
23362336
for i in &[1i64, 2i64, 3i64][range_full] {} // $ target=index MISSING: type=i:&T.i64
23372337

23382338
let range1 = // $ type=range1:Range type=range1:Idx.u16
@@ -2347,19 +2347,19 @@ mod loops {
23472347
let vals3 = vec![1, 2, 3]; // $ MISSING: type=vals3:Vec type=vals3:T.i32
23482348
for i in vals3 {} // $ MISSING: type=i:i32
23492349

2350-
let vals4a: Vec<u16> = [1u16, 2, 3].to_vec(); // $ type=vals4a:Vec type=vals4a:T.u16
2350+
let vals4a: Vec<u16> = [1u16, 2, 3].to_vec(); // $ certainType=vals4a:Vec certainType=vals4a:T.u16
23512351
for u in vals4a {} // $ type=u:u16
23522352

23532353
let vals4b = [1u16, 2, 3].to_vec(); // $ MISSING: type=vals4b:Vec type=vals4b:T.u16
23542354
for u in vals4b {} // $ MISSING: type=u:u16
23552355

2356-
let vals5 = Vec::from([1u32, 2, 3]); // $ type=vals5:Vec target=from type=vals5:T.u32
2356+
let vals5 = Vec::from([1u32, 2, 3]); // $ certainType=vals5:Vec target=from type=vals5:T.u32
23572357
for u in vals5 {} // $ type=u:u32
23582358

2359-
let vals6: Vec<&u64> = [1u64, 2, 3].iter().collect(); // $ type=vals6:Vec type=vals6:T.&T.u64
2359+
let vals6: Vec<&u64> = [1u64, 2, 3].iter().collect(); // $ certainType=vals6:Vec certainType=vals6:T.&T.u64
23602360
for u in vals6 {} // $ type=u:&T.u64
23612361

2362-
let mut vals7 = Vec::new(); // $ target=new type=vals7:Vec type=vals7:T.u8
2362+
let mut vals7 = Vec::new(); // $ target=new certainType=vals7:Vec type=vals7:T.u8
23632363
vals7.push(1u8); // $ target=push
23642364
for u in vals7 {} // $ type=u:u8
23652365

@@ -2380,11 +2380,11 @@ mod loops {
23802380

23812381
// while loops
23822382

2383-
let mut a: i64 = 0; // $ type=a:i64
2383+
let mut a: i64 = 0; // $ certainType=a:i64
23842384
#[rustfmt::skip]
2385-
let _ = while a < 10 // $ target=lt type=a:i64
2385+
let _ = while a < 10 // $ target=lt certainType=a:i64
23862386
{
2387-
a += 1; // $ type=a:i64 MISSING: target=add_assign
2387+
a += 1; // $ certainType=a:i64 MISSING: target=add_assign
23882388
};
23892389
}
23902390
}
@@ -2422,11 +2422,11 @@ mod explicit_type_args {
24222422
}
24232423

24242424
pub fn f() {
2425-
let x1: Option<S1<S2>> = S1::assoc_fun(); // $ type=x1:T.T.S2 target=assoc_fun
2426-
let x2 = S1::<S2>::assoc_fun(); // $ type=x2:T.T.S2 target=assoc_fun
2427-
let x3 = S3::assoc_fun(); // $ type=x3:T.T.S2 target=assoc_fun
2428-
let x4 = S1::<S2>::method(S1::default()); // $ target=method target=default type=x4:T.S2
2429-
let x5 = S3::method(S1::default()); // $ target=method target=default type=x5:T.S2
2425+
let x1: Option<S1<S2>> = S1::assoc_fun(); // $ certainType=x1:T.T.S2 target=assoc_fun
2426+
let x2 = S1::<S2>::assoc_fun(); // $ certainType=x2:T.T.S2 target=assoc_fun
2427+
let x3 = S3::assoc_fun(); // $ certainType=x3:T.T.S2 target=assoc_fun
2428+
let x4 = S1::<S2>::method(S1::default()); // $ target=method target=default certainType=x4:T.S2
2429+
let x5 = S3::method(S1::default()); // $ target=method target=default certainType=x5:T.S2
24302430
let x6 = S4::<S2>(Default::default()); // $ type=x6:T4.S2 target=default
24312431
let x7 = S4(S2); // $ type=x7:T4.S2
24322432
let x8 = S4(0); // $ type=x8:T4.i32
@@ -2441,7 +2441,7 @@ mod explicit_type_args {
24412441
{
24422442
field: S2::default(), // $ target=default
24432443
};
2444-
let x14 = foo::<i32>(Default::default()); // $ type=x14:i32 target=default target=foo
2444+
let x14 = foo::<i32>(Default::default()); // $ certainType=x14:i32 target=default target=foo
24452445
}
24462446
}
24472447

@@ -2457,8 +2457,8 @@ mod tuples {
24572457
}
24582458

24592459
pub fn f() {
2460-
let a = S1::get_pair(); // $ target=get_pair type=a:(T_2)
2461-
let mut b = S1::get_pair(); // $ target=get_pair type=b:(T_2)
2460+
let a = S1::get_pair(); // $ target=get_pair certainType=a:(T_2)
2461+
let mut b = S1::get_pair(); // $ target=get_pair certainType=b:(T_2)
24622462
let (c, d) = S1::get_pair(); // $ target=get_pair type=c:S1 type=d:S1
24632463
let (mut e, f) = S1::get_pair(); // $ target=get_pair type=e:S1 type=f:S1
24642464
let (mut g, mut h) = S1::get_pair(); // $ target=get_pair type=g:S1 type=h:S1
@@ -2593,11 +2593,11 @@ pub mod path_buf {
25932593
}
25942594

25952595
pub fn f() {
2596-
let path1 = Path::new(); // $ target=new type=path1:Path
2596+
let path1 = Path::new(); // $ target=new certainType=path1:Path
25972597
let path2 = path1.canonicalize(); // $ target=canonicalize
25982598
let path3 = path2.unwrap(); // $ target=unwrap type=path3:PathBuf
25992599

2600-
let pathbuf1 = PathBuf::new(); // $ target=new type=pathbuf1:PathBuf
2600+
let pathbuf1 = PathBuf::new(); // $ target=new certainType=pathbuf1:PathBuf
26012601
let pathbuf2 = pathbuf1.canonicalize(); // $ MISSING: target=canonicalize
26022602
let pathbuf3 = pathbuf2.unwrap(); // $ MISSING: target=unwrap type=pathbuf3:PathBuf
26032603
}

0 commit comments

Comments
 (0)