Skip to content

Commit 5b26bc3

Browse files
Nick Bentonfacebook-github-bot
Nick Benton
authored andcommitted
naive early evaluation of instanceof
Summary: This is a simple-minded change to `Formula.and_equal_instaceof` that evaluates eagerly if the dynamic type is known at the point we make the assertion. It doesn't integrate with the propagation of equalities etc, as I'm now going to leave that until I've got a proper instanceof domain instead. But this should suffice to check the effects of eager solving on coverage etc. Reviewed By: skcho Differential Revision: D54537978 fbshipit-source-id: bb590fd76ac707c84163da14e527cfe091321ef6
1 parent 6f15407 commit 5b26bc3

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

infer/src/pulse/PulseFormula.ml

+14-8
Original file line numberDiff line numberDiff line change
@@ -3437,14 +3437,6 @@ let and_equal_vars v1 v2 formula =
34373437
34383438
let and_not_equal = and_mk_atom Ne
34393439
3440-
let and_equal_instanceof v1 v2 t ~get_dynamic_type ~tenv formula =
3441-
ignore get_dynamic_type ;
3442-
ignore tenv ;
3443-
(* just testing the plumbing *)
3444-
let atom = Atom.equal (Var v1) (IsInstanceOf (v2, t)) in
3445-
and_atom atom formula
3446-
3447-
34483440
let and_is_int v formula =
34493441
let atom = Atom.equal (IsInt (Var v)) Term.one in
34503442
and_atom atom formula
@@ -3557,6 +3549,20 @@ module DynamicTypes = struct
35573549
else Sat (formula, RevList.empty)
35583550
end
35593551
3552+
(* Just do most naive thing of evaluating instanceof if we know the dynamic type at the time of assertion
3553+
Because that's pretty weak, leave existing normalisation at summary time in for now
3554+
*)
3555+
let and_equal_instanceof v1 v2 t ~get_dynamic_type ~tenv formula =
3556+
let atom =
3557+
match DynamicTypes.evaluate_instanceof tenv ~get_dynamic_type v2 t with
3558+
| None ->
3559+
Atom.equal (Var v1) (IsInstanceOf (v2, t))
3560+
| Some bool_term ->
3561+
Atom.equal (Var v1) bool_term
3562+
in
3563+
and_atom atom formula
3564+
3565+
35603566
let normalize tenv ~get_dynamic_type formula =
35613567
Debug.p "@\n@\n***NORMALIZING NOW***@\n@\n" ;
35623568
(* normalization happens incrementally except for dynamic types (TODO) *)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Facebook, Inc. and its affiliates.
2+
//
3+
// This source code is licensed under the MIT license found in the
4+
// LICENSE file in the root directory of this source tree.
5+
6+
// These tests are all OK, but useful for looking at the debug output to confirm
7+
// that the eager pruning is working as expected
8+
class EagerTypePrune {
9+
public static function testPositiveOK(): void {
10+
$i = 3;
11+
if ($i is int) {
12+
return;
13+
} else {
14+
// if we're really eager, we shouldn't even analyse this call
15+
// have confirmed that we do at the moment
16+
$dummy = self::g();
17+
}
18+
}
19+
20+
public static function testNegativeOK(): void {
21+
$i = 3;
22+
if ($i is string) {
23+
$dummy = self::g();
24+
} else {
25+
return;
26+
}
27+
}
28+
29+
public static function testBothOK(mixed $i): void {
30+
if ($i is int) {
31+
$dummy = self::g();
32+
} else {
33+
$dummy = self::f();
34+
}
35+
}
36+
37+
public static function g(): int {
38+
return 42;
39+
}
40+
41+
public static function f(): int {
42+
return 42;
43+
}
44+
}

0 commit comments

Comments
 (0)