Skip to content

Commit 8fe737c

Browse files
committed
Rust: Use defaults for type parameters
1 parent 38aacb0 commit 8fe737c

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ abstract class Type extends TType {
4242
/** Gets the `i`th type parameter of this type, if any. */
4343
abstract TypeParameter getTypeParameter(int i);
4444

45+
/** Gets the default type for the `i`th type parameter, if any. */
46+
TypeMention getTypeParameterDefault(int i) { none() }
47+
4548
/** Gets a type parameter of this type. */
4649
final TypeParameter getATypeParameter() { result = this.getTypeParameter(_) }
4750

@@ -87,6 +90,10 @@ class StructType extends StructOrEnumType, TStruct {
8790
result = TTypeParamTypeParameter(struct.getGenericParamList().getTypeParam(i))
8891
}
8992

93+
override TypeMention getTypeParameterDefault(int i) {
94+
result = struct.getGenericParamList().getTypeParam(i).getDefaultType()
95+
}
96+
9097
override string toString() { result = struct.getName().getText() }
9198

9299
override Location getLocation() { result = struct.getLocation() }
@@ -108,6 +115,10 @@ class EnumType extends StructOrEnumType, TEnum {
108115
result = TTypeParamTypeParameter(enum.getGenericParamList().getTypeParam(i))
109116
}
110117

118+
override TypeMention getTypeParameterDefault(int i) {
119+
result = enum.getGenericParamList().getTypeParam(i).getDefaultType()
120+
}
121+
111122
override string toString() { result = enum.getName().getText() }
112123

113124
override Location getLocation() { result = enum.getLocation() }
@@ -133,6 +144,10 @@ class TraitType extends Type, TTrait {
133144
any(AssociatedTypeTypeParameter param | param.getTrait() = trait and param.getIndex() = i)
134145
}
135146

147+
override TypeMention getTypeParameterDefault(int i) {
148+
result = trait.getGenericParamList().getTypeParam(i).getDefaultType()
149+
}
150+
136151
override string toString() { result = trait.toString() }
137152

138153
override Location getLocation() { result = trait.getLocation() }

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class PathTypeReprMention extends TypeMention instanceof PathTypeRepr {
8888
override TypeMention getTypeArgument(int i) {
8989
result = path.getSegment().getGenericArgList().getTypeArg(i)
9090
or
91+
// If a type argument is not given in the path, then we use the default for
92+
// the type parameter if one exists for the type.
93+
not exists(path.getSegment().getGenericArgList().getTypeArg(i)) and
94+
result = this.resolveType().getTypeParameterDefault(i)
95+
or
9196
// `Self` paths inside `impl` blocks have implicit type arguments that are
9297
// the type parameters of the `impl` block. For example, in
9398
//

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod field_access {
2828
}
2929

3030
fn default_field_access(x: GenericThing) {
31-
let a = x.a; // $ fieldof=GenericThing MISSING: type=a:bool
31+
let a = x.a; // $ fieldof=GenericThing type=a:bool
3232
println!("{:?}", a);
3333
}
3434

@@ -499,15 +499,15 @@ mod type_parameter_bounds {
499499

500500
fn call_trait_per_bound_with_type_3<T: Pair>(x: T, y: T) {
501501
// The type in the type parameter bound determines the return type.
502-
let s1 = x.fst(); // $ method=fst MISSING: type=s1:bool
503-
let s2 = y.snd(); // $ method=snd MISSING: type=s2:i64
502+
let s1 = x.fst(); // $ method=fst type=s1:bool
503+
let s2 = y.snd(); // $ method=snd type=s2:i64
504504
println!("{:?}, {:?}", s1, s2);
505505
}
506506

507507
fn call_trait_per_bound_with_type_4<T: Pair<u8>>(x: T, y: T) {
508508
// The type in the type parameter bound determines the return type.
509509
let s1 = x.fst(); // $ method=fst type=s1:u8
510-
let s2 = y.snd(); // $ method=snd MISSING: type=s2:i64
510+
let s2 = y.snd(); // $ method=snd type=s2:i64
511511
println!("{:?}, {:?}", s1, s2);
512512
}
513513
}

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ inferType
99
| main.rs:27:26:27:26 | x | | main.rs:5:5:8:5 | MyThing |
1010
| main.rs:27:26:27:28 | x.a | | main.rs:2:5:3:13 | S |
1111
| main.rs:30:29:30:29 | x | | main.rs:16:5:19:5 | GenericThing |
12+
| main.rs:30:29:30:29 | x | A | {EXTERNAL LOCATION} | bool |
13+
| main.rs:31:13:31:13 | a | | {EXTERNAL LOCATION} | bool |
1214
| main.rs:31:17:31:17 | x | | main.rs:16:5:19:5 | GenericThing |
15+
| main.rs:31:17:31:17 | x | A | {EXTERNAL LOCATION} | bool |
16+
| main.rs:31:17:31:19 | x.a | | {EXTERNAL LOCATION} | bool |
1317
| main.rs:32:18:32:23 | "{:?}\\n" | | {EXTERNAL LOCATION} | str |
18+
| main.rs:32:26:32:26 | a | | {EXTERNAL LOCATION} | bool |
1419
| main.rs:37:13:37:13 | x | | main.rs:16:5:19:5 | GenericThing |
1520
| main.rs:37:13:37:13 | x | A | main.rs:2:5:3:13 | S |
1621
| main.rs:37:17:37:42 | GenericThing::<...> {...} | | main.rs:16:5:19:5 | GenericThing |
@@ -583,17 +588,26 @@ inferType
583588
| main.rs:497:36:497:37 | s2 | | main.rs:493:41:493:49 | T2 |
584589
| main.rs:500:50:500:50 | x | | main.rs:500:41:500:47 | T |
585590
| main.rs:500:56:500:56 | y | | main.rs:500:41:500:47 | T |
591+
| main.rs:502:13:502:14 | s1 | | {EXTERNAL LOCATION} | bool |
586592
| main.rs:502:18:502:18 | x | | main.rs:500:41:500:47 | T |
593+
| main.rs:502:18:502:24 | x.fst() | | {EXTERNAL LOCATION} | bool |
594+
| main.rs:503:13:503:14 | s2 | | {EXTERNAL LOCATION} | i64 |
587595
| main.rs:503:18:503:18 | y | | main.rs:500:41:500:47 | T |
596+
| main.rs:503:18:503:24 | y.snd() | | {EXTERNAL LOCATION} | i64 |
588597
| main.rs:504:18:504:29 | "{:?}, {:?}\\n" | | {EXTERNAL LOCATION} | str |
598+
| main.rs:504:32:504:33 | s1 | | {EXTERNAL LOCATION} | bool |
599+
| main.rs:504:36:504:37 | s2 | | {EXTERNAL LOCATION} | i64 |
589600
| main.rs:507:54:507:54 | x | | main.rs:507:41:507:51 | T |
590601
| main.rs:507:60:507:60 | y | | main.rs:507:41:507:51 | T |
591602
| main.rs:509:13:509:14 | s1 | | {EXTERNAL LOCATION} | u8 |
592603
| main.rs:509:18:509:18 | x | | main.rs:507:41:507:51 | T |
593604
| main.rs:509:18:509:24 | x.fst() | | {EXTERNAL LOCATION} | u8 |
605+
| main.rs:510:13:510:14 | s2 | | {EXTERNAL LOCATION} | i64 |
594606
| main.rs:510:18:510:18 | y | | main.rs:507:41:507:51 | T |
607+
| main.rs:510:18:510:24 | y.snd() | | {EXTERNAL LOCATION} | i64 |
595608
| main.rs:511:18:511:29 | "{:?}, {:?}\\n" | | {EXTERNAL LOCATION} | str |
596609
| main.rs:511:32:511:33 | s1 | | {EXTERNAL LOCATION} | u8 |
610+
| main.rs:511:36:511:37 | s2 | | {EXTERNAL LOCATION} | i64 |
597611
| main.rs:527:15:527:18 | SelfParam | | main.rs:526:5:535:5 | Self [trait MyTrait] |
598612
| main.rs:529:15:529:18 | SelfParam | | main.rs:526:5:535:5 | Self [trait MyTrait] |
599613
| main.rs:532:9:534:9 | { ... } | | main.rs:526:19:526:19 | A |
@@ -2541,6 +2555,7 @@ inferType
25412555
| main.rs:1798:13:1798:38 | MyVec {...} | | main.rs:1791:5:1794:5 | MyVec |
25422556
| main.rs:1798:13:1798:38 | MyVec {...} | T | main.rs:1796:10:1796:10 | T |
25432557
| main.rs:1798:27:1798:36 | ...::new(...) | | {EXTERNAL LOCATION} | Vec |
2558+
| main.rs:1798:27:1798:36 | ...::new(...) | A | {EXTERNAL LOCATION} | Global |
25442559
| main.rs:1798:27:1798:36 | ...::new(...) | T | main.rs:1796:10:1796:10 | T |
25452560
| main.rs:1801:17:1801:25 | SelfParam | | file://:0:0:0:0 | & |
25462561
| main.rs:1801:17:1801:25 | SelfParam | &T | main.rs:1791:5:1794:5 | MyVec |
@@ -2550,6 +2565,7 @@ inferType
25502565
| main.rs:1802:13:1802:16 | self | &T | main.rs:1791:5:1794:5 | MyVec |
25512566
| main.rs:1802:13:1802:16 | self | &T.T | main.rs:1796:10:1796:10 | T |
25522567
| main.rs:1802:13:1802:21 | self.data | | {EXTERNAL LOCATION} | Vec |
2568+
| main.rs:1802:13:1802:21 | self.data | A | {EXTERNAL LOCATION} | Global |
25532569
| main.rs:1802:13:1802:21 | self.data | T | main.rs:1796:10:1796:10 | T |
25542570
| main.rs:1802:28:1802:32 | value | | main.rs:1796:10:1796:10 | T |
25552571
| main.rs:1810:18:1810:22 | SelfParam | | file://:0:0:0:0 | & |
@@ -2564,6 +2580,7 @@ inferType
25642580
| main.rs:1811:14:1811:17 | self | &T | main.rs:1791:5:1794:5 | MyVec |
25652581
| main.rs:1811:14:1811:17 | self | &T.T | main.rs:1806:10:1806:10 | T |
25662582
| main.rs:1811:14:1811:22 | self.data | | {EXTERNAL LOCATION} | Vec |
2583+
| main.rs:1811:14:1811:22 | self.data | A | {EXTERNAL LOCATION} | Global |
25672584
| main.rs:1811:14:1811:22 | self.data | T | main.rs:1806:10:1806:10 | T |
25682585
| main.rs:1811:14:1811:29 | ...[index] | | main.rs:1806:10:1806:10 | T |
25692586
| main.rs:1811:24:1811:28 | index | | {EXTERNAL LOCATION} | usize |

0 commit comments

Comments
 (0)