Skip to content

Commit 1770f56

Browse files
authored
Merge pull request #19367 from hvitved/rust/type-inference-try-expr
Rust: Type inference for `?` expressions
2 parents 20f7781 + a3c26b4 commit 1770f56

File tree

4 files changed

+184
-4
lines changed

4 files changed

+184
-4
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ private import Type
66
private import Type as T
77
private import TypeMention
88
private import codeql.typeinference.internal.TypeInference
9+
private import codeql.rust.frameworks.stdlib.Stdlib
910

1011
class Type = T::Type;
1112

@@ -873,6 +874,17 @@ private Type inferRefExprType(Expr e, TypePath path) {
873874
)
874875
}
875876

877+
pragma[nomagic]
878+
private Type inferTryExprType(TryExpr te, TypePath path) {
879+
exists(TypeParam tp |
880+
result = inferType(te.getExpr(), TypePath::cons(TTypeParamTypeParameter(tp), path))
881+
|
882+
tp = any(ResultEnum r).getGenericParamList().getGenericParam(0)
883+
or
884+
tp = any(OptionEnum o).getGenericParamList().getGenericParam(0)
885+
)
886+
}
887+
876888
cached
877889
private module Cached {
878890
private import codeql.rust.internal.CachedStages
@@ -1012,6 +1024,8 @@ private module Cached {
10121024
result = inferFieldExprType(n, path)
10131025
or
10141026
result = inferRefExprType(n, path)
1027+
or
1028+
result = inferTryExprType(n, path)
10151029
}
10161030
}
10171031

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
multipleMethodCallTargets
2+
| main.rs:11:5:18:5 | conn.execute(...) | file://:0:0:0:0 | fn execute |
3+
| main.rs:11:5:18:5 | conn.execute(...) | file://:0:0:0:0 | fn execute |
4+
| main.rs:22:5:22:37 | conn.execute(...) | file://:0:0:0:0 | fn execute |
5+
| main.rs:22:5:22:37 | conn.execute(...) | file://:0:0:0:0 | fn execute |
6+
| main.rs:23:5:23:38 | conn.batch_execute(...) | file://:0:0:0:0 | fn batch_execute |
7+
| main.rs:23:5:23:38 | conn.batch_execute(...) | file://:0:0:0:0 | fn batch_execute |
8+
| main.rs:25:5:25:32 | conn.prepare(...) | file://:0:0:0:0 | fn prepare |
9+
| main.rs:25:5:25:32 | conn.prepare(...) | file://:0:0:0:0 | fn prepare |
10+
| main.rs:28:5:28:35 | conn.query(...) | file://:0:0:0:0 | fn query |
11+
| main.rs:28:5:28:35 | conn.query(...) | file://:0:0:0:0 | fn query |
12+
| main.rs:29:5:29:39 | conn.query_one(...) | file://:0:0:0:0 | fn query_one |
13+
| main.rs:29:5:29:39 | conn.query_one(...) | file://:0:0:0:0 | fn query_one |
14+
| main.rs:30:5:30:39 | conn.query_opt(...) | file://:0:0:0:0 | fn query_opt |
15+
| main.rs:30:5:30:39 | conn.query_opt(...) | file://:0:0:0:0 | fn query_opt |
16+
| main.rs:35:17:35:67 | conn.query(...) | file://:0:0:0:0 | fn query |
17+
| main.rs:35:17:35:67 | conn.query(...) | file://:0:0:0:0 | fn query |

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

+60
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,65 @@ mod borrowed_typed {
919919
}
920920
}
921921

922+
mod try_expressions {
923+
use std::fmt::Debug;
924+
925+
#[derive(Debug)]
926+
struct S1;
927+
928+
#[derive(Debug)]
929+
struct S2;
930+
931+
// Simple function using ? operator with same error types
932+
fn try_same_error() -> Result<S1, S1> {
933+
let x = Result::Ok(S1)?; // $ type=x:S1
934+
Result::Ok(S1)
935+
}
936+
937+
// Function using ? operator with different error types that need conversion
938+
fn try_convert_error() -> Result<S1, S2> {
939+
let x = Result::Ok(S1);
940+
let y = x?; // $ type=y:S1
941+
Result::Ok(S1)
942+
}
943+
944+
// Chained ? operations
945+
fn try_chained() -> Result<S1, S2> {
946+
let x = Result::Ok(Result::Ok(S1));
947+
// First ? returns Result<S1, S2>, second ? returns S1
948+
let y = x?.map(|s| s)?; // $ method=map
949+
Result::Ok(S1)
950+
}
951+
952+
// Function that uses ? with closures and complex error cases
953+
fn try_complex<T: Debug>(input: Result<T, S1>) -> Result<T, S1> {
954+
let value = input?;
955+
let mapped = Result::Ok(value).and_then(|v| {
956+
println!("{:?}", v);
957+
Result::Ok::<_, S1>(v)
958+
})?; // $ method=and_then
959+
Result::Err(S1)
960+
}
961+
962+
pub fn f() {
963+
if let Result::Ok(result) = try_same_error() {
964+
println!("{:?}", result);
965+
}
966+
967+
if let Result::Ok(result) = try_convert_error() {
968+
println!("{:?}", result);
969+
}
970+
971+
if let Result::Ok(result) = try_chained() {
972+
println!("{:?}", result);
973+
}
974+
975+
if let Result::Ok(result) = try_complex(Result::Ok(S1)) {
976+
println!("{:?}", result);
977+
}
978+
}
979+
}
980+
922981
fn main() {
923982
field_access::f();
924983
method_impl::f();
@@ -935,4 +994,5 @@ fn main() {
935994
trait_implicit_self_borrow::f();
936995
implicit_self_borrow::f();
937996
borrowed_typed::f();
997+
try_expressions::f();
938998
}

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

+93-4
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,96 @@ inferType
10051005
| main.rs:918:15:918:16 | &x | | file://:0:0:0:0 | & |
10061006
| main.rs:918:15:918:16 | &x | &T | main.rs:894:5:894:13 | S |
10071007
| main.rs:918:16:918:16 | x | | main.rs:894:5:894:13 | S |
1008-
| main.rs:924:5:924:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
1009-
| main.rs:925:5:925:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
1010-
| main.rs:925:20:925:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
1011-
| main.rs:925:41:925:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
1008+
| main.rs:932:43:935:5 | { ... } | | file://:0:0:0:0 | Result |
1009+
| main.rs:932:43:935:5 | { ... } | E | main.rs:925:5:926:14 | S1 |
1010+
| main.rs:932:43:935:5 | { ... } | T | main.rs:925:5:926:14 | S1 |
1011+
| main.rs:933:13:933:13 | x | | main.rs:925:5:926:14 | S1 |
1012+
| main.rs:933:17:933:30 | ...::Ok(...) | | file://:0:0:0:0 | Result |
1013+
| main.rs:933:17:933:30 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 |
1014+
| main.rs:933:17:933:31 | TryExpr | | main.rs:925:5:926:14 | S1 |
1015+
| main.rs:933:28:933:29 | S1 | | main.rs:925:5:926:14 | S1 |
1016+
| main.rs:934:9:934:22 | ...::Ok(...) | | file://:0:0:0:0 | Result |
1017+
| main.rs:934:9:934:22 | ...::Ok(...) | E | main.rs:925:5:926:14 | S1 |
1018+
| main.rs:934:9:934:22 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 |
1019+
| main.rs:934:20:934:21 | S1 | | main.rs:925:5:926:14 | S1 |
1020+
| main.rs:938:46:942:5 | { ... } | | file://:0:0:0:0 | Result |
1021+
| main.rs:938:46:942:5 | { ... } | E | main.rs:928:5:929:14 | S2 |
1022+
| main.rs:938:46:942:5 | { ... } | T | main.rs:925:5:926:14 | S1 |
1023+
| main.rs:939:13:939:13 | x | | file://:0:0:0:0 | Result |
1024+
| main.rs:939:13:939:13 | x | T | main.rs:925:5:926:14 | S1 |
1025+
| main.rs:939:17:939:30 | ...::Ok(...) | | file://:0:0:0:0 | Result |
1026+
| main.rs:939:17:939:30 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 |
1027+
| main.rs:939:28:939:29 | S1 | | main.rs:925:5:926:14 | S1 |
1028+
| main.rs:940:13:940:13 | y | | main.rs:925:5:926:14 | S1 |
1029+
| main.rs:940:17:940:17 | x | | file://:0:0:0:0 | Result |
1030+
| main.rs:940:17:940:17 | x | T | main.rs:925:5:926:14 | S1 |
1031+
| main.rs:940:17:940:18 | TryExpr | | main.rs:925:5:926:14 | S1 |
1032+
| main.rs:941:9:941:22 | ...::Ok(...) | | file://:0:0:0:0 | Result |
1033+
| main.rs:941:9:941:22 | ...::Ok(...) | E | main.rs:928:5:929:14 | S2 |
1034+
| main.rs:941:9:941:22 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 |
1035+
| main.rs:941:20:941:21 | S1 | | main.rs:925:5:926:14 | S1 |
1036+
| main.rs:945:40:950:5 | { ... } | | file://:0:0:0:0 | Result |
1037+
| main.rs:945:40:950:5 | { ... } | E | main.rs:928:5:929:14 | S2 |
1038+
| main.rs:945:40:950:5 | { ... } | T | main.rs:925:5:926:14 | S1 |
1039+
| main.rs:946:13:946:13 | x | | file://:0:0:0:0 | Result |
1040+
| main.rs:946:13:946:13 | x | T | file://:0:0:0:0 | Result |
1041+
| main.rs:946:13:946:13 | x | T.T | main.rs:925:5:926:14 | S1 |
1042+
| main.rs:946:17:946:42 | ...::Ok(...) | | file://:0:0:0:0 | Result |
1043+
| main.rs:946:17:946:42 | ...::Ok(...) | T | file://:0:0:0:0 | Result |
1044+
| main.rs:946:17:946:42 | ...::Ok(...) | T.T | main.rs:925:5:926:14 | S1 |
1045+
| main.rs:946:28:946:41 | ...::Ok(...) | | file://:0:0:0:0 | Result |
1046+
| main.rs:946:28:946:41 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 |
1047+
| main.rs:946:39:946:40 | S1 | | main.rs:925:5:926:14 | S1 |
1048+
| main.rs:948:17:948:17 | x | | file://:0:0:0:0 | Result |
1049+
| main.rs:948:17:948:17 | x | T | file://:0:0:0:0 | Result |
1050+
| main.rs:948:17:948:17 | x | T.T | main.rs:925:5:926:14 | S1 |
1051+
| main.rs:948:17:948:18 | TryExpr | | file://:0:0:0:0 | Result |
1052+
| main.rs:948:17:948:18 | TryExpr | T | main.rs:925:5:926:14 | S1 |
1053+
| main.rs:948:17:948:29 | ... .map(...) | | file://:0:0:0:0 | Result |
1054+
| main.rs:949:9:949:22 | ...::Ok(...) | | file://:0:0:0:0 | Result |
1055+
| main.rs:949:9:949:22 | ...::Ok(...) | E | main.rs:928:5:929:14 | S2 |
1056+
| main.rs:949:9:949:22 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 |
1057+
| main.rs:949:20:949:21 | S1 | | main.rs:925:5:926:14 | S1 |
1058+
| main.rs:953:30:953:34 | input | | file://:0:0:0:0 | Result |
1059+
| main.rs:953:30:953:34 | input | E | main.rs:925:5:926:14 | S1 |
1060+
| main.rs:953:30:953:34 | input | T | main.rs:953:20:953:27 | T |
1061+
| main.rs:953:69:960:5 | { ... } | | file://:0:0:0:0 | Result |
1062+
| main.rs:953:69:960:5 | { ... } | E | main.rs:925:5:926:14 | S1 |
1063+
| main.rs:953:69:960:5 | { ... } | T | main.rs:953:20:953:27 | T |
1064+
| main.rs:954:13:954:17 | value | | main.rs:953:20:953:27 | T |
1065+
| main.rs:954:21:954:25 | input | | file://:0:0:0:0 | Result |
1066+
| main.rs:954:21:954:25 | input | E | main.rs:925:5:926:14 | S1 |
1067+
| main.rs:954:21:954:25 | input | T | main.rs:953:20:953:27 | T |
1068+
| main.rs:954:21:954:26 | TryExpr | | main.rs:953:20:953:27 | T |
1069+
| main.rs:955:22:955:38 | ...::Ok(...) | | file://:0:0:0:0 | Result |
1070+
| main.rs:955:22:955:38 | ...::Ok(...) | T | main.rs:953:20:953:27 | T |
1071+
| main.rs:955:22:958:10 | ... .and_then(...) | | file://:0:0:0:0 | Result |
1072+
| main.rs:955:33:955:37 | value | | main.rs:953:20:953:27 | T |
1073+
| main.rs:955:53:958:9 | { ... } | | file://:0:0:0:0 | Result |
1074+
| main.rs:955:53:958:9 | { ... } | E | main.rs:925:5:926:14 | S1 |
1075+
| main.rs:957:13:957:34 | ...::Ok::<...>(...) | | file://:0:0:0:0 | Result |
1076+
| main.rs:957:13:957:34 | ...::Ok::<...>(...) | E | main.rs:925:5:926:14 | S1 |
1077+
| main.rs:959:9:959:23 | ...::Err(...) | | file://:0:0:0:0 | Result |
1078+
| main.rs:959:9:959:23 | ...::Err(...) | E | main.rs:925:5:926:14 | S1 |
1079+
| main.rs:959:9:959:23 | ...::Err(...) | T | main.rs:953:20:953:27 | T |
1080+
| main.rs:959:21:959:22 | S1 | | main.rs:925:5:926:14 | S1 |
1081+
| main.rs:963:37:963:52 | try_same_error(...) | | file://:0:0:0:0 | Result |
1082+
| main.rs:963:37:963:52 | try_same_error(...) | E | main.rs:925:5:926:14 | S1 |
1083+
| main.rs:963:37:963:52 | try_same_error(...) | T | main.rs:925:5:926:14 | S1 |
1084+
| main.rs:967:37:967:55 | try_convert_error(...) | | file://:0:0:0:0 | Result |
1085+
| main.rs:967:37:967:55 | try_convert_error(...) | E | main.rs:928:5:929:14 | S2 |
1086+
| main.rs:967:37:967:55 | try_convert_error(...) | T | main.rs:925:5:926:14 | S1 |
1087+
| main.rs:971:37:971:49 | try_chained(...) | | file://:0:0:0:0 | Result |
1088+
| main.rs:971:37:971:49 | try_chained(...) | E | main.rs:928:5:929:14 | S2 |
1089+
| main.rs:971:37:971:49 | try_chained(...) | T | main.rs:925:5:926:14 | S1 |
1090+
| main.rs:975:37:975:63 | try_complex(...) | | file://:0:0:0:0 | Result |
1091+
| main.rs:975:37:975:63 | try_complex(...) | E | main.rs:925:5:926:14 | S1 |
1092+
| main.rs:975:37:975:63 | try_complex(...) | T | main.rs:925:5:926:14 | S1 |
1093+
| main.rs:975:49:975:62 | ...::Ok(...) | | file://:0:0:0:0 | Result |
1094+
| main.rs:975:49:975:62 | ...::Ok(...) | E | main.rs:925:5:926:14 | S1 |
1095+
| main.rs:975:49:975:62 | ...::Ok(...) | T | main.rs:925:5:926:14 | S1 |
1096+
| main.rs:975:60:975:61 | S1 | | main.rs:925:5:926:14 | S1 |
1097+
| main.rs:983:5:983:20 | ...::f(...) | | main.rs:67:5:67:21 | Foo |
1098+
| main.rs:984:5:984:60 | ...::g(...) | | main.rs:67:5:67:21 | Foo |
1099+
| main.rs:984:20:984:38 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |
1100+
| main.rs:984:41:984:59 | ...::Foo {...} | | main.rs:67:5:67:21 | Foo |

0 commit comments

Comments
 (0)