Rust: Implement type inference for ref expression as type equality

This commit is contained in:
Simon Friis Vindum
2025-06-11 09:09:22 +02:00
parent 8f5d9d7702
commit 01701de4a7
3 changed files with 62 additions and 47 deletions

View File

@@ -257,6 +257,10 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat
)
)
or
n1 = n2.(RefExpr).getExpr() and
prefix1.isEmpty() and
prefix2 = TypePath::singleton(TRefTypeParameter())
or
n1 = n2.(DerefExpr).getExpr() and
prefix1 = TypePath::singleton(TRefTypeParameter()) and
prefix2.isEmpty()
@@ -973,43 +977,9 @@ private Type inferFieldExprType(AstNode n, TypePath path) {
)
}
/**
* Gets the type of `n` at `path`, where `n` is either a reference expression
* `& x` or an expression `x` inside a reference expression `& x`.
*/
/** Gets the root type of the reference expression `re`. */
pragma[nomagic]
private Type inferRefExprType(Expr e, TypePath path) {
exists(RefExpr re |
e = re and
path.isEmpty() and
result = TRefType()
or
e = re and
exists(TypePath exprPath | result = inferType(re.getExpr(), exprPath) |
if exprPath.isCons(TRefTypeParameter(), _)
then
// `&x` simply means `x` when `x` already has reference type
path = exprPath
else (
path = TypePath::cons(TRefTypeParameter(), exprPath) and
not (exprPath.isEmpty() and result = TRefType())
)
)
or
e = re.getExpr() and
exists(TypePath exprPath, TypePath refPath, Type exprType |
result = inferType(re, exprPath) and
exprPath.isCons(TRefTypeParameter(), refPath) and
exprType = inferType(e)
|
if exprType = TRefType()
then
// `&x` simply means `x` when `x` already has reference type
path = exprPath
else path = refPath
)
)
}
private Type inferRefExprType(RefExpr re) { exists(re) and result = TRefType() }
pragma[nomagic]
private Type inferTryExprType(TryExpr te, TypePath path) {
@@ -1505,7 +1475,8 @@ private module Cached {
or
result = inferFieldExprType(n, path)
or
result = inferRefExprType(n, path)
result = inferRefExprType(n) and
path.isEmpty()
or
result = inferTryExprType(n, path)
or

View File

@@ -1191,13 +1191,13 @@ mod borrowed_typed {
x.f2(); // $ method=f2
S::f3(&x);
let n = **&&true; // $ MISSING: type=n:bool
let n = **&&true; // $ type=n:bool
// In this example the type of `flag` must be inferred at the call to
// `flip` and flow through the borrow in the argument.
let mut flag = Default::default();
MyFlag::flip(&mut flag);
println!("{:?}", flag); // $ MISSING: type=flag:MyFlag
println!("{:?}", flag); // $ type=flag:MyFlag
}
}

View File

@@ -1442,25 +1442,49 @@ inferType
| main.rs:1171:15:1171:19 | SelfParam | | file://:0:0:0:0 | & |
| main.rs:1171:15:1171:19 | SelfParam | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1171:31:1173:9 | { ... } | | file://:0:0:0:0 | & |
| main.rs:1171:31:1173:9 | { ... } | &T | file://:0:0:0:0 | & |
| main.rs:1171:31:1173:9 | { ... } | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1171:31:1173:9 | { ... } | &T.&T | file://:0:0:0:0 | & |
| main.rs:1171:31:1173:9 | { ... } | &T.&T.&T | file://:0:0:0:0 | & |
| main.rs:1171:31:1173:9 | { ... } | &T.&T.&T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1172:13:1172:19 | &... | | file://:0:0:0:0 | & |
| main.rs:1172:13:1172:19 | &... | &T | file://:0:0:0:0 | & |
| main.rs:1172:13:1172:19 | &... | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1172:13:1172:19 | &... | &T.&T | file://:0:0:0:0 | & |
| main.rs:1172:13:1172:19 | &... | &T.&T.&T | file://:0:0:0:0 | & |
| main.rs:1172:13:1172:19 | &... | &T.&T.&T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1172:14:1172:19 | &... | | file://:0:0:0:0 | & |
| main.rs:1172:14:1172:19 | &... | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1172:14:1172:19 | &... | | main.rs:1168:5:1168:13 | S |
| main.rs:1172:14:1172:19 | &... | &T | file://:0:0:0:0 | & |
| main.rs:1172:14:1172:19 | &... | &T.&T | file://:0:0:0:0 | & |
| main.rs:1172:14:1172:19 | &... | &T.&T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1172:15:1172:19 | &self | | file://:0:0:0:0 | & |
| main.rs:1172:15:1172:19 | &self | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1172:15:1172:19 | &self | &T | file://:0:0:0:0 | & |
| main.rs:1172:15:1172:19 | &self | &T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1172:16:1172:19 | self | | file://:0:0:0:0 | & |
| main.rs:1172:16:1172:19 | self | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1175:15:1175:25 | SelfParam | | file://:0:0:0:0 | & |
| main.rs:1175:15:1175:25 | SelfParam | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1175:37:1177:9 | { ... } | | file://:0:0:0:0 | & |
| main.rs:1175:37:1177:9 | { ... } | &T | file://:0:0:0:0 | & |
| main.rs:1175:37:1177:9 | { ... } | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1175:37:1177:9 | { ... } | &T.&T | file://:0:0:0:0 | & |
| main.rs:1175:37:1177:9 | { ... } | &T.&T.&T | file://:0:0:0:0 | & |
| main.rs:1175:37:1177:9 | { ... } | &T.&T.&T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1176:13:1176:19 | &... | | file://:0:0:0:0 | & |
| main.rs:1176:13:1176:19 | &... | &T | file://:0:0:0:0 | & |
| main.rs:1176:13:1176:19 | &... | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1176:13:1176:19 | &... | &T.&T | file://:0:0:0:0 | & |
| main.rs:1176:13:1176:19 | &... | &T.&T.&T | file://:0:0:0:0 | & |
| main.rs:1176:13:1176:19 | &... | &T.&T.&T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1176:14:1176:19 | &... | | file://:0:0:0:0 | & |
| main.rs:1176:14:1176:19 | &... | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1176:14:1176:19 | &... | | main.rs:1168:5:1168:13 | S |
| main.rs:1176:14:1176:19 | &... | &T | file://:0:0:0:0 | & |
| main.rs:1176:14:1176:19 | &... | &T.&T | file://:0:0:0:0 | & |
| main.rs:1176:14:1176:19 | &... | &T.&T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1176:15:1176:19 | &self | | file://:0:0:0:0 | & |
| main.rs:1176:15:1176:19 | &self | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1176:15:1176:19 | &self | &T | file://:0:0:0:0 | & |
| main.rs:1176:15:1176:19 | &self | &T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1176:16:1176:19 | self | | file://:0:0:0:0 | & |
| main.rs:1176:16:1176:19 | self | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1179:15:1179:15 | x | | file://:0:0:0:0 | & |
@@ -1472,13 +1496,25 @@ inferType
| main.rs:1183:15:1183:15 | x | | file://:0:0:0:0 | & |
| main.rs:1183:15:1183:15 | x | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1183:34:1185:9 | { ... } | | file://:0:0:0:0 | & |
| main.rs:1183:34:1185:9 | { ... } | &T | file://:0:0:0:0 | & |
| main.rs:1183:34:1185:9 | { ... } | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1183:34:1185:9 | { ... } | &T.&T | file://:0:0:0:0 | & |
| main.rs:1183:34:1185:9 | { ... } | &T.&T.&T | file://:0:0:0:0 | & |
| main.rs:1183:34:1185:9 | { ... } | &T.&T.&T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1184:13:1184:16 | &... | | file://:0:0:0:0 | & |
| main.rs:1184:13:1184:16 | &... | &T | file://:0:0:0:0 | & |
| main.rs:1184:13:1184:16 | &... | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1184:13:1184:16 | &... | &T.&T | file://:0:0:0:0 | & |
| main.rs:1184:13:1184:16 | &... | &T.&T.&T | file://:0:0:0:0 | & |
| main.rs:1184:13:1184:16 | &... | &T.&T.&T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1184:14:1184:16 | &... | | file://:0:0:0:0 | & |
| main.rs:1184:14:1184:16 | &... | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1184:14:1184:16 | &... | | main.rs:1168:5:1168:13 | S |
| main.rs:1184:14:1184:16 | &... | &T | file://:0:0:0:0 | & |
| main.rs:1184:14:1184:16 | &... | &T.&T | file://:0:0:0:0 | & |
| main.rs:1184:14:1184:16 | &... | &T.&T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1184:15:1184:16 | &x | | file://:0:0:0:0 | & |
| main.rs:1184:15:1184:16 | &x | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1184:15:1184:16 | &x | &T | file://:0:0:0:0 | & |
| main.rs:1184:15:1184:16 | &x | &T.&T | main.rs:1168:5:1168:13 | S |
| main.rs:1184:16:1184:16 | x | | file://:0:0:0:0 | & |
| main.rs:1184:16:1184:16 | x | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1189:13:1189:13 | x | | main.rs:1168:5:1168:13 | S |
@@ -1494,15 +1530,23 @@ inferType
| main.rs:1192:15:1192:16 | &x | | file://:0:0:0:0 | & |
| main.rs:1192:15:1192:16 | &x | &T | main.rs:1168:5:1168:13 | S |
| main.rs:1192:16:1192:16 | x | | main.rs:1168:5:1168:13 | S |
| main.rs:1194:18:1194:24 | * ... | | {EXTERNAL LOCATION} | bool |
| main.rs:1194:13:1194:13 | n | | {EXTERNAL LOCATION} | bool |
| main.rs:1194:17:1194:24 | * ... | | {EXTERNAL LOCATION} | bool |
| main.rs:1194:18:1194:24 | * ... | | file://:0:0:0:0 | & |
| main.rs:1194:18:1194:24 | * ... | &T | {EXTERNAL LOCATION} | bool |
| main.rs:1194:19:1194:24 | &... | | file://:0:0:0:0 | & |
| main.rs:1194:19:1194:24 | &... | &T | {EXTERNAL LOCATION} | bool |
| main.rs:1194:19:1194:24 | &... | &T | file://:0:0:0:0 | & |
| main.rs:1194:19:1194:24 | &... | &T.&T | {EXTERNAL LOCATION} | bool |
| main.rs:1194:20:1194:24 | &true | | file://:0:0:0:0 | & |
| main.rs:1194:20:1194:24 | &true | &T | {EXTERNAL LOCATION} | bool |
| main.rs:1194:21:1194:24 | true | | {EXTERNAL LOCATION} | bool |
| main.rs:1198:13:1198:20 | mut flag | | main.rs:1157:5:1160:5 | MyFlag |
| main.rs:1198:24:1198:41 | ...::default(...) | | main.rs:1157:5:1160:5 | MyFlag |
| main.rs:1199:22:1199:30 | &mut flag | | file://:0:0:0:0 | & |
| main.rs:1199:22:1199:30 | &mut flag | &T | main.rs:1157:5:1160:5 | MyFlag |
| main.rs:1199:27:1199:30 | flag | | main.rs:1157:5:1160:5 | MyFlag |
| main.rs:1200:18:1200:23 | "{:?}\\n" | | {EXTERNAL LOCATION} | str |
| main.rs:1200:26:1200:29 | flag | | main.rs:1157:5:1160:5 | MyFlag |
| main.rs:1214:43:1217:5 | { ... } | | {EXTERNAL LOCATION} | Result |
| main.rs:1214:43:1217:5 | { ... } | E | main.rs:1207:5:1208:14 | S1 |
| main.rs:1214:43:1217:5 | { ... } | T | main.rs:1207:5:1208:14 | S1 |