Skip to content

Commit ec15085

Browse files
committed
Address review comments
1 parent bf0dc3c commit ec15085

File tree

9 files changed

+1546
-1654
lines changed

9 files changed

+1546
-1654
lines changed

rust/ql/lib/codeql/rust/dataflow/internal/Content.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ abstract class FieldContent extends Content {
2929
class TupleFieldContent extends FieldContent, TTupleFieldContent {
3030
private TupleField field;
3131

32-
TupleFieldContent() { this = TTupleFieldContent(field) }
32+
TupleFieldContent() {
33+
this = TTupleFieldContent(field) and
34+
// tuples are handled using the special `TupleContent` type
35+
not field = any(TupleType tt).getATupleField()
36+
}
3337

3438
/** Holds if this field belongs to an enum variant. */
3539
predicate isVariantField(Variant v, int pos) { field.isVariantField(v, pos) }
3640

3741
/** Holds if this field belongs to a struct. */
3842
predicate isStructField(Struct s, int pos) { field.isStructField(s, pos) }
3943

40-
override FieldExprCfgNode getAnAccess() {
41-
field = result.getFieldExpr().getTupleField() and
42-
// tuples are handled using the special `TupleContent` type
43-
not field = any(TupleType tt).getATupleField()
44-
}
44+
override FieldExprCfgNode getAnAccess() { field = result.getFieldExpr().getTupleField() }
4545

4646
final override string toString() {
4747
exists(Variant v, int pos, string vname |

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,8 +2172,7 @@ private predicate builtin(string name, ItemNode i) {
21722172
exists(BuiltinSourceFile builtins |
21732173
builtins.getFile().getBaseName() = "types.rs" and
21742174
i = builtins.getASuccessor(name) and
2175-
not name = ["Slice", "Array", "Ref", "Ptr"] and
2176-
not name.matches("Tuple%")
2175+
i.isPublic()
21772176
)
21782177
}
21792178

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

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ class UnionType extends Type, TUnion {
208208
/**
209209
* An array type.
210210
*
211-
* Array types like `[i64; 5]` are modeled as normal generic types
212-
* with a single type argument.
211+
* Array types like `[i64; 5]` are modeled as normal generic types.
213212
*/
214213
class ArrayType extends StructType {
215214
ArrayType() { this.getStruct() instanceof Builtins::ArrayType }
@@ -401,32 +400,7 @@ class TypeParamTypeParameter extends TypeParameter, TTypeParamTypeParameter {
401400

402401
TypeParam getTypeParam() { result = typeParam }
403402

404-
override string toString() {
405-
this = any(SliceType st).getATypeParameter() and
406-
result = "[T]"
407-
or
408-
this = any(ArrayType at).getATypeParameter() and
409-
result = "[T;...]"
410-
or
411-
this = any(RefType rt).getATypeParameter() and
412-
result = "&T"
413-
or
414-
this = any(PtrType pt).getATypeParameter() and
415-
result = "*T"
416-
or
417-
exists(TupleType tt, int arity, int i |
418-
this = tt.getPositionalTypeParameter(i) and
419-
arity = tt.getArity() and
420-
result = i + "(" + arity + ")"
421-
)
422-
or
423-
not this = any(SliceType st).getATypeParameter() and
424-
not this = any(ArrayType at).getATypeParameter() and
425-
not this = any(RefType rt).getATypeParameter() and
426-
not this = any(PtrType pt).getATypeParameter() and
427-
not this = any(TupleType tt).getATypeParameter() and
428-
result = typeParam.toString()
429-
}
403+
override string toString() { result = typeParam.toString() }
430404

431405
override Location getLocation() { result = typeParam.getLocation() }
432406
}

rust/ql/test/library-tests/elements/builtintypes/BuiltinTypes.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
| struct Tuple10 | |
1616
| struct Tuple11 | |
1717
| struct Tuple12 | |
18-
| struct Tuple13 | |
19-
| struct Tuple14 | |
20-
| struct Tuple15 | |
21-
| struct Tuple16 | |
2218
| struct bool | |
2319
| struct char | |
2420
| struct f32 | FloatingPointType, NumericType |

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<T> S<T> {
3838
fn explicit_monomorphic_dereference() {
3939
// Dereference with method call
4040
let a1 = MyIntPointer { value: 34i64 };
41-
let _b1 = a1.deref(); // $ target=MyIntPointer::deref type=_b1:&T.i64
41+
let _b1 = a1.deref(); // $ target=MyIntPointer::deref type=_b1:TRef.i64
4242

4343
// Dereference with overloaded dereference operator
4444
let a2 = MyIntPointer { value: 34i64 };
@@ -52,7 +52,7 @@ fn explicit_monomorphic_dereference() {
5252
fn explicit_polymorphic_dereference() {
5353
// Explicit dereference with type parameter
5454
let c1 = MySmartPointer { value: 'a' };
55-
let _d1 = c1.deref(); // $ target=MySmartPointer::deref type=_d1:&T.char
55+
let _d1 = c1.deref(); // $ target=MySmartPointer::deref type=_d1:TRef.char
5656

5757
// Explicit dereference with type parameter
5858
let c2 = MySmartPointer { value: 'a' };
@@ -66,7 +66,7 @@ fn explicit_polymorphic_dereference() {
6666
fn explicit_ref_dereference() {
6767
// Explicit dereference with type parameter
6868
let e1 = &'a';
69-
let _f1 = e1.deref(); // $ target=deref type=_f1:&T.char
69+
let _f1 = e1.deref(); // $ target=deref type=_f1:TRef.char
7070

7171
// Explicit dereference with type parameter
7272
let e2 = &'a';
@@ -80,7 +80,7 @@ fn explicit_ref_dereference() {
8080
fn explicit_box_dereference() {
8181
// Explicit dereference with type parameter
8282
let g1: Box<char> = Box::new('a'); // $ target=new
83-
let _h1 = g1.deref(); // $ target=deref type=_h1:&T.char
83+
let _h1 = g1.deref(); // $ target=deref type=_h1:TRef.char
8484

8585
// Explicit dereference with type parameter
8686
let g2: Box<char> = Box::new('a'); // $ target=new
@@ -101,7 +101,7 @@ fn implicit_dereference() {
101101
let _y = x.is_positive(); // $ MISSING: target=is_positive type=_y:bool
102102

103103
let z = MySmartPointer { value: S(0i64) };
104-
let z_ = z.foo(); // $ MISSING: target=foo type=z_:&T.i64
104+
let z_ = z.foo(); // $ MISSING: target=foo type=z_:TRef.i64
105105
}
106106

107107
mod implicit_deref_coercion_cycle {

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

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ mod method_call_type_conversion {
15421542
let x7 = S(&S2);
15431543
// Non-implicit dereference with nested borrow in order to test that the
15441544
// implicit dereference handling doesn't affect nested borrows.
1545-
let t = x7.m1(); // $ target=m1 type=t:& type=t:&T.S2
1545+
let t = x7.m1(); // $ target=m1 type=t:& type=t:TRef.S2
15461546
println!("{:?}", x7);
15471547

15481548
let x9: String = "Hello".to_string(); // $ certainType=x9:String target=to_string
@@ -1732,7 +1732,7 @@ mod builtins {
17321732
let z = x + y; // $ type=z:i32 target=add
17331733
let z = x.abs(); // $ target=abs $ type=z:i32
17341734
let c = 'c'; // $ certainType=c:char
1735-
let hello = "Hello"; // $ certainType=hello:&T.str
1735+
let hello = "Hello"; // $ certainType=hello:TRef.str
17361736
let f = 123.0f64; // $ certainType=f:f64
17371737
let t = true; // $ certainType=t:bool
17381738
let f = false; // $ certainType=f:bool
@@ -1753,8 +1753,8 @@ mod builtins {
17531753
}
17541754
}
17551755

1756-
let x = [1, 2, 3].my_method(); // $ target=my_method type=x:&T.i32
1757-
let x = <[_; 3]>::my_method(&[1, 2, 3]); // $ target=my_method type=x:&T.i32
1756+
let x = [1, 2, 3].my_method(); // $ target=my_method type=x:TRef.i32
1757+
let x = <[_; 3]>::my_method(&[1, 2, 3]); // $ target=my_method type=x:TRef.i32
17581758
let x = <[i32; 3]>::my_func(); // $ target=my_func type=x:i32
17591759

17601760
impl<T: Default> MyTrait<T> for [T] {
@@ -1768,8 +1768,8 @@ mod builtins {
17681768
}
17691769

17701770
let s: &[i32] = &[1, 2, 3];
1771-
let x = s.my_method(); // $ target=my_method type=x:&T.i32
1772-
let x = <[_]>::my_method(s); // $ target=my_method type=x:&T.i32
1771+
let x = s.my_method(); // $ target=my_method type=x:TRef.i32
1772+
let x = <[_]>::my_method(s); // $ target=my_method type=x:TRef.i32
17731773
let x = <[i32]>::my_func(); // $ target=my_func type=x:i32
17741774

17751775
impl<T: Default> MyTrait<T> for (T, i32) {
@@ -1783,8 +1783,8 @@ mod builtins {
17831783
}
17841784

17851785
let p = (42, 7);
1786-
let x = p.my_method(); // $ target=my_method type=x:&T.i32
1787-
let x = <(_, _)>::my_method(&p); // $ target=my_method type=x:&T.i32
1786+
let x = p.my_method(); // $ target=my_method type=x:TRef.i32
1787+
let x = <(_, _)>::my_method(&p); // $ target=my_method type=x:TRef.i32
17881788
let x = <(i32, i32)>::my_func(); // $ target=my_func type=x:i32
17891789

17901790
impl<T: Default> MyTrait<T> for &T {
@@ -1798,8 +1798,8 @@ mod builtins {
17981798
}
17991799

18001800
let r = &42;
1801-
let x = r.my_method(); // $ target=my_method type=x:&T.i32
1802-
let x = <&_>::my_method(&r); // $ target=my_method type=x:&T.i32
1801+
let x = r.my_method(); // $ target=my_method type=x:TRef.i32
1802+
let x = <&_>::my_method(&r); // $ target=my_method type=x:TRef.i32
18031803
let x = <&i32>::my_func(); // $ target=my_func type=x:i32
18041804

18051805
impl<T: Default> MyTrait<T> for *mut T {
@@ -1814,8 +1814,8 @@ mod builtins {
18141814

18151815
let mut v = 42;
18161816
let p: *mut i32 = &mut v;
1817-
let x = unsafe { p.my_method() }; // $ target=my_method type=x:&T.i32
1818-
let x = unsafe { <*mut _>::my_method(&p) }; // $ target=my_method type=x:&T.i32
1817+
let x = unsafe { p.my_method() }; // $ target=my_method type=x:TRef.i32
1818+
let x = unsafe { <*mut _>::my_method(&p) }; // $ target=my_method type=x:TRef.i32
18191819
let x = <*mut i32>::my_func(); // $ target=my_func type=x:i32
18201820
}
18211821
}
@@ -2612,40 +2612,40 @@ mod loops {
26122612
for i in [1, 2, 3].map(|x| x + 1) {} // $ target=map MISSING: type=i:i32
26132613
for i in [1, 2, 3].into_iter() {} // $ target=into_iter type=i:i32
26142614

2615-
let vals1 = [1u8, 2, 3]; // $ type=vals1:[T;...].u8
2615+
let vals1 = [1u8, 2, 3]; // $ type=vals1:TArray.u8
26162616
for u in vals1 {} // $ type=u:u8
26172617

2618-
let vals2 = [1u16; 3]; // $ type=vals2:[T;...].u16
2618+
let vals2 = [1u16; 3]; // $ type=vals2:TArray.u16
26192619
for u in vals2 {} // $ type=u:u16
26202620

2621-
let vals3: [u32; 3] = [1, 2, 3]; // $ certainType=vals3:[T;...].u32
2621+
let vals3: [u32; 3] = [1, 2, 3]; // $ certainType=vals3:TArray.u32
26222622
for u in vals3 {} // $ type=u:u32
26232623

2624-
let vals4: [u64; 3] = [1; 3]; // $ certainType=vals4:[T;...].u64
2624+
let vals4: [u64; 3] = [1; 3]; // $ certainType=vals4:TArray.u64
26252625
for u in vals4 {} // $ type=u:u64
26262626

2627-
let mut strings1 = ["foo", "bar", "baz"]; // $ type=strings1:[T;...].&T.str
2628-
for s in &strings1 {} // $ type=s:&T.&T.str
2629-
for s in &mut strings1 {} // $ type=s:&T.&T.str
2630-
for s in strings1 {} // $ type=s:&T.str
2627+
let mut strings1 = ["foo", "bar", "baz"]; // $ type=strings1:TArray.TRef.str
2628+
for s in &strings1 {} // $ type=s:TRef.TRef.str
2629+
for s in &mut strings1 {} // $ type=s:TRef.TRef.str
2630+
for s in strings1 {} // $ type=s:TRef.str
26312631

2632-
let strings2 = // $ type=strings2:[T;...].String
2632+
let strings2 = // $ type=strings2:TArray.String
26332633
[
26342634
String::from("foo"), // $ target=from
26352635
String::from("bar"), // $ target=from
26362636
String::from("baz"), // $ target=from
26372637
];
26382638
for s in strings2 {} // $ type=s:String
26392639

2640-
let strings3 = // $ type=strings3:&T.[T;...].String
2640+
let strings3 = // $ type=strings3:TRef.TArray.String
26412641
&[
26422642
String::from("foo"), // $ target=from
26432643
String::from("bar"), // $ target=from
26442644
String::from("baz"), // $ target=from
26452645
];
2646-
for s in strings3 {} // $ type=s:&T.String
2646+
for s in strings3 {} // $ type=s:TRef.String
26472647

2648-
let callables = [MyCallable::new(), MyCallable::new(), MyCallable::new()]; // $ target=new $ type=callables:[T;...].MyCallable
2648+
let callables = [MyCallable::new(), MyCallable::new(), MyCallable::new()]; // $ target=new $ type=callables:TArray.MyCallable
26492649
for c // $ type=c:MyCallable
26502650
in callables
26512651
{
@@ -2659,7 +2659,7 @@ mod loops {
26592659
let range = 0..10; // $ certainType=range:Range type=range:Idx.i32
26602660
for i in range {} // $ type=i:i32
26612661
let range_full = ..; // $ certainType=range_full:RangeFull
2662-
for i in &[1i64, 2i64, 3i64][range_full] {} // $ target=index MISSING: type=i:&T.i64
2662+
for i in &[1i64, 2i64, 3i64][range_full] {} // $ target=index MISSING: type=i:TRef.i64
26632663

26642664
let range1 = // $ certainType=range1:Range type=range1:Idx.u16
26652665
std::ops::Range {
@@ -2682,8 +2682,8 @@ mod loops {
26822682
let vals5 = Vec::from([1u32, 2, 3]); // $ certainType=vals5:Vec target=from type=vals5:T.u32
26832683
for u in vals5 {} // $ type=u:u32
26842684

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

26882688
let mut vals7 = Vec::new(); // $ target=new certainType=vals7:Vec type=vals7:T.u8
26892689
vals7.push(1u8); // $ target=push
@@ -2696,13 +2696,13 @@ mod loops {
26962696
}
26972697
};
26982698

2699-
let mut map1 = std::collections::HashMap::new(); // $ target=new type=map1:K.i32 type=map1:V.Box $ MISSING: type=map1:Hashmap type1=map1:V.T.&T.str
2699+
let mut map1 = std::collections::HashMap::new(); // $ target=new type=map1:K.i32 type=map1:V.Box $ MISSING: type=map1:Hashmap type1=map1:V.T.TRef.str
27002700
map1.insert(1, Box::new("one")); // $ target=insert target=new
27012701
map1.insert(2, Box::new("two")); // $ target=insert target=new
2702-
for key in map1.keys() {} // $ target=keys type=key:&T.i32
2703-
for value in map1.values() {} // $ target=values type=value:&T.Box type=value:&T.T.&T.str
2704-
for (key, value) in map1.iter() {} // $ target=iter type=key:&T.i32 type=value:&T.Box type=value:&T.T.&T.str
2705-
for (key, value) in &map1 {} // $ type=key:&T.i32 type=value:&T.Box type=value:&T.T.&T.str
2702+
for key in map1.keys() {} // $ target=keys type=key:TRef.i32
2703+
for value in map1.values() {} // $ target=values type=value:TRef.Box type=value:TRef.T.TRef.str
2704+
for (key, value) in map1.iter() {} // $ target=iter type=key:TRef.i32 type=value:TRef.Box type=value:TRef.T.TRef.str
2705+
for (key, value) in &map1 {} // $ type=key:TRef.i32 type=value:TRef.Box type=value:TRef.T.TRef.str
27062706

27072707
// while loops
27082708

@@ -2804,11 +2804,11 @@ mod tuples {
28042804
// `a` and `b` to be inferred.
28052805
let a = Default::default(); // $ target=default type=a:i64
28062806
let b = Default::default(); // $ target=default type=b:bool
2807-
let pair = (a, b); // $ type=pair:0(2).i64 type=pair:1(2).bool
2807+
let pair = (a, b); // $ type=pair:T0.i64 type=pair:T1.bool
28082808
let i: i64 = pair.0; // $ fieldof=Tuple2
28092809
let j: bool = pair.1; // $ fieldof=Tuple2
28102810

2811-
let pair = [1, 1].into(); // $ type=pair:(T_2) type=pair:0(2).i32 type=pair:1(2).i32 MISSING: target=into
2811+
let pair = [1, 1].into(); // $ type=pair:(T_2) type=pair:T0.i32 type=pair:T1.i32 MISSING: target=into
28122812
match pair {
28132813
(0, 0) => print!("unexpected"),
28142814
_ => print!("expected"),

0 commit comments

Comments
 (0)