Skip to content

Commit 47aea74

Browse files
committed
wip2
1 parent 704fe11 commit 47aea74

5 files changed

Lines changed: 22 additions & 72 deletions

File tree

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -432,17 +432,6 @@ private module Input3 implements InputSig3 {
432432
}
433433

434434
class Call extends FunctionCallMatchingInput::Access {
435-
/** Gets the target of this call. */
436-
Callable getTargetCertain() {
437-
none() and
438-
exists(ImplOrTraitItemNodeOption i, FunctionDeclaration f, Path p |
439-
result.isFunction(i, f) and
440-
p = CallExprImpl::getFunctionPath(this) and
441-
f = resolvePath(p) and
442-
f.isDirectlyFor(i)
443-
)
444-
}
445-
446435
Callable getTarget(string derefChainBorrow) { result = super.getTarget(derefChainBorrow) }
447436
}
448437

@@ -880,17 +869,6 @@ private Type getCallExprTypeArgument(CallExpr ce, TypeArgumentPosition apos, Typ
880869
)
881870
}
882871

883-
pragma[nomagic]
884-
private Type inferFunctionBodyType(AstNode n, TypePath path) {
885-
exists(Function f |
886-
n = f.getFunctionBody() and
887-
result = getReturnTypeMention(f).getTypeAt(path) and
888-
not exists(ImplTraitReturnType i | i.getFunction() = f |
889-
result = i or result = i.getATypeParameter()
890-
)
891-
)
892-
}
893-
894872
/**
895873
* Holds if `me` is a call to the `panic!` macro.
896874
*
@@ -3480,7 +3458,10 @@ private DynTraitTypeParameter getDynFutureOutputTypeParameter() {
34803458
pragma[nomagic]
34813459
predicate isUnitBlockExpr(BlockExpr be) {
34823460
not be.getStmtList().hasTailExpr() and
3483-
// not be = any(Callable c).getBody() and
3461+
not exists(Callable c |
3462+
be = c.getBody() and
3463+
c = any(ReturnExpr re).getEnclosingCallable()
3464+
) and
34843465
not be.hasLabel()
34853466
}
34863467

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ mod implicit_deref_coercion_cycle {
141141

142142
#[rustfmt::skip]
143143
pub fn test() {
144-
let mut key_to_key = HashMap::<&Key, &Key>::new(); // $ target=new
144+
let mut key_to_key = HashMap::<_, &Key>::new(); // $ target=new
145145
let mut key = &Key {}; // Initialize key2 to a reference
146146
if let Some(ref_key) = key_to_key.get(key) { // $ target=get
147147
// Below `ref_key` is implicitly dereferenced from `&&Key` to `&Key`

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,7 +1953,7 @@ mod impl_trait {
19531953
impl<T: Clone> MyTrait<T> for S3<T> {
19541954
fn get_a(&self) -> T {
19551955
let S3(t) = self;
1956-
t.clone()
1956+
t.clone() // $ MISSING: target=clone type=t:T
19571957
}
19581958
}
19591959

@@ -2737,7 +2737,7 @@ mod literal_overlap {
27372737

27382738
pub fn f() -> usize {
27392739
let mut x = 0;
2740-
x = x.f(); // $ target=usizef $ SPURIOUS: target=i32f
2740+
x = x.f(); // $ MISSING: target=usizef $ SPURIOUS: target=i32f
27412741
x
27422742
}
27432743

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3063,7 +3063,6 @@ inferCertainType
30633063
| main.rs:2618:18:2618:26 | "b: {:?}\\n" | | {EXTERNAL LOCATION} | & |
30643064
| main.rs:2618:18:2618:26 | "b: {:?}\\n" | TRef | {EXTERNAL LOCATION} | str |
30653065
| main.rs:2618:18:2618:29 | { ... } | | {EXTERNAL LOCATION} | () |
3066-
| main.rs:2622:20:2624:5 | { ... } | | {EXTERNAL LOCATION} | () |
30673066
| main.rs:2627:11:2627:14 | cond | | {EXTERNAL LOCATION} | bool |
30683067
| main.rs:2628:13:2628:13 | a | | {EXTERNAL LOCATION} | () |
30693068
| main.rs:2628:17:2632:9 | { ... } | | {EXTERNAL LOCATION} | () |
@@ -6049,15 +6048,19 @@ inferType
60496048
| dereference.rs:144:17:144:26 | key_to_key | | {EXTERNAL LOCATION} | HashMap |
60506049
| dereference.rs:144:17:144:26 | key_to_key | K | {EXTERNAL LOCATION} | & |
60516050
| dereference.rs:144:17:144:26 | key_to_key | K.TRef | dereference.rs:122:5:123:21 | Key |
6051+
| dereference.rs:144:17:144:26 | key_to_key | K.TRef | {EXTERNAL LOCATION} | & |
6052+
| dereference.rs:144:17:144:26 | key_to_key | K.TRef.TRef | dereference.rs:122:5:123:21 | Key |
60526053
| dereference.rs:144:17:144:26 | key_to_key | S | {EXTERNAL LOCATION} | RandomState |
60536054
| dereference.rs:144:17:144:26 | key_to_key | V | {EXTERNAL LOCATION} | & |
60546055
| dereference.rs:144:17:144:26 | key_to_key | V.TRef | dereference.rs:122:5:123:21 | Key |
6055-
| dereference.rs:144:30:144:57 | ...::new(...) | | {EXTERNAL LOCATION} | HashMap |
6056-
| dereference.rs:144:30:144:57 | ...::new(...) | K | {EXTERNAL LOCATION} | & |
6057-
| dereference.rs:144:30:144:57 | ...::new(...) | K.TRef | dereference.rs:122:5:123:21 | Key |
6058-
| dereference.rs:144:30:144:57 | ...::new(...) | S | {EXTERNAL LOCATION} | RandomState |
6059-
| dereference.rs:144:30:144:57 | ...::new(...) | V | {EXTERNAL LOCATION} | & |
6060-
| dereference.rs:144:30:144:57 | ...::new(...) | V.TRef | dereference.rs:122:5:123:21 | Key |
6056+
| dereference.rs:144:30:144:54 | ...::new(...) | | {EXTERNAL LOCATION} | HashMap |
6057+
| dereference.rs:144:30:144:54 | ...::new(...) | K | {EXTERNAL LOCATION} | & |
6058+
| dereference.rs:144:30:144:54 | ...::new(...) | K.TRef | dereference.rs:122:5:123:21 | Key |
6059+
| dereference.rs:144:30:144:54 | ...::new(...) | K.TRef | {EXTERNAL LOCATION} | & |
6060+
| dereference.rs:144:30:144:54 | ...::new(...) | K.TRef.TRef | dereference.rs:122:5:123:21 | Key |
6061+
| dereference.rs:144:30:144:54 | ...::new(...) | S | {EXTERNAL LOCATION} | RandomState |
6062+
| dereference.rs:144:30:144:54 | ...::new(...) | V | {EXTERNAL LOCATION} | & |
6063+
| dereference.rs:144:30:144:54 | ...::new(...) | V.TRef | dereference.rs:122:5:123:21 | Key |
60616064
| dereference.rs:145:17:145:19 | key | | {EXTERNAL LOCATION} | & |
60626065
| dereference.rs:145:17:145:19 | key | TRef | dereference.rs:122:5:123:21 | Key |
60636066
| dereference.rs:145:17:145:19 | key | TRef | {EXTERNAL LOCATION} | & |
@@ -6080,6 +6083,8 @@ inferType
60806083
| dereference.rs:146:32:146:41 | key_to_key | | {EXTERNAL LOCATION} | HashMap |
60816084
| dereference.rs:146:32:146:41 | key_to_key | K | {EXTERNAL LOCATION} | & |
60826085
| dereference.rs:146:32:146:41 | key_to_key | K.TRef | dereference.rs:122:5:123:21 | Key |
6086+
| dereference.rs:146:32:146:41 | key_to_key | K.TRef | {EXTERNAL LOCATION} | & |
6087+
| dereference.rs:146:32:146:41 | key_to_key | K.TRef.TRef | dereference.rs:122:5:123:21 | Key |
60836088
| dereference.rs:146:32:146:41 | key_to_key | S | {EXTERNAL LOCATION} | RandomState |
60846089
| dereference.rs:146:32:146:41 | key_to_key | V | {EXTERNAL LOCATION} | & |
60856090
| dereference.rs:146:32:146:41 | key_to_key | V.TRef | dereference.rs:122:5:123:21 | Key |
@@ -6105,6 +6110,8 @@ inferType
61056110
| dereference.rs:150:9:150:18 | key_to_key | | {EXTERNAL LOCATION} | HashMap |
61066111
| dereference.rs:150:9:150:18 | key_to_key | K | {EXTERNAL LOCATION} | & |
61076112
| dereference.rs:150:9:150:18 | key_to_key | K.TRef | dereference.rs:122:5:123:21 | Key |
6113+
| dereference.rs:150:9:150:18 | key_to_key | K.TRef | {EXTERNAL LOCATION} | & |
6114+
| dereference.rs:150:9:150:18 | key_to_key | K.TRef.TRef | dereference.rs:122:5:123:21 | Key |
61086115
| dereference.rs:150:9:150:18 | key_to_key | S | {EXTERNAL LOCATION} | RandomState |
61096116
| dereference.rs:150:9:150:18 | key_to_key | V | {EXTERNAL LOCATION} | & |
61106117
| dereference.rs:150:9:150:18 | key_to_key | V.TRef | dereference.rs:122:5:123:21 | Key |
@@ -11492,9 +11499,8 @@ inferType
1149211499
| main.rs:2618:18:2618:29 | { ... } | | {EXTERNAL LOCATION} | () |
1149311500
| main.rs:2618:18:2618:29 | { ... } | | {EXTERNAL LOCATION} | () |
1149411501
| main.rs:2619:9:2619:9 | 0 | | {EXTERNAL LOCATION} | i32 |
11495-
| main.rs:2622:20:2624:5 | { ... } | | {EXTERNAL LOCATION} | () |
11502+
| main.rs:2622:20:2624:5 | { ... } | | {EXTERNAL LOCATION} | i32 |
1149611503
| main.rs:2623:16:2623:16 | 0 | | {EXTERNAL LOCATION} | i32 |
11497-
| main.rs:2623:16:2623:16 | 0 | | {EXTERNAL LOCATION} | () |
1149811504
| main.rs:2627:11:2627:14 | cond | | {EXTERNAL LOCATION} | bool |
1149911505
| main.rs:2627:30:2635:5 | { ... } | | {EXTERNAL LOCATION} | i32 |
1150011506
| main.rs:2628:13:2628:13 | a | | {EXTERNAL LOCATION} | () |
@@ -14651,7 +14657,6 @@ testFailures
1465114657
| main.rs:2438:37:2438:76 | //... | Missing result: certainType=b:(T_2) |
1465214658
| main.rs:2536:34:2536:71 | //... | Missing result: certainType=path1:Path |
1465314659
| main.rs:2540:40:2540:83 | //... | Missing result: certainType=pathbuf1:PathBuf |
14654-
| main.rs:2740:20:2740:61 | //... | Missing result: target=usizef |
1465514660
| pattern_matching.rs:782:43:782:139 | //... | Missing result: certainType=extracted@(T_2)<T0>:i32 |
1465614661
| pattern_matching.rs:782:43:782:139 | //... | Missing result: certainType=extracted@(T_2)<T1>:i32 |
1465714662
| pattern_matching.rs:785:37:785:80 | //... | Missing result: certainType=red:u8 |

shared/typeinference/codeql/typeinference/internal/TypeInference.qll

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,11 +2214,6 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
22142214
/** Gets the AST node corresponding to the position `pos` of this call. */
22152215
AstNode getNodeAt(TypePosition pos);
22162216

2217-
/**
2218-
* Gets the target of this call, to be used when inferring certain types.
2219-
*/
2220-
Callable getTargetCertain();
2221-
22222217
/** Gets the target of this call in the given context. */
22232218
Callable getTarget(CallResolutionContext ctx);
22242219
}
@@ -2398,35 +2393,6 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
23982393
path.isEmpty()
23992394
}
24002395

2401-
pragma[nomagic]
2402-
private Type getCertainCallExprReturnType(Call call, TypePath path) {
2403-
exists(TypePosition ret |
2404-
ret.isReturn() and
2405-
forex(Callable target | target = call.getTargetCertain() |
2406-
result = target.getDeclaredType(ret, path)
2407-
)
2408-
)
2409-
}
2410-
2411-
pragma[nomagic]
2412-
private Type inferCertainCallExprReturnType(Call call, TypePath path) {
2413-
exists(Type ty, TypePath prefix | ty = getCertainCallExprReturnType(call, prefix) |
2414-
exists(
2415-
Callable target, TypePath suffix, TypeParameterPosition tppos,
2416-
TypeArgumentPosition tapos
2417-
|
2418-
ty = target.getTypeParameter(tppos) and
2419-
path = prefix.append(suffix) and
2420-
result = call.getTypeArgument(tapos, suffix) and
2421-
typeArgumentParameterPositionMatch(tapos, tppos)
2422-
)
2423-
or
2424-
not ty instanceof TypeParameter and
2425-
result = ty and
2426-
path = prefix
2427-
)
2428-
}
2429-
24302396
/** Gets the inferred certain type of `n` at `path`. */
24312397
cached
24322398
Type inferTypeCertain(AstNode n, TypePath path) {
@@ -2439,8 +2405,6 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
24392405
or
24402406
result = inferLogicalOperationType(n, path)
24412407
or
2442-
result = inferCertainCallExprReturnType(n, path)
2443-
or
24442408
infersCertainTypeAt(n, path, result.getATypeParameter())
24452409
}
24462410

0 commit comments

Comments
 (0)