Skip to content

Commit 8ca71fc

Browse files
committed
ZJIT: Use nil? optimization to test guard generation against different types
1 parent 4f5c92e commit 8ca71fc

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

zjit/src/hir.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6658,4 +6658,106 @@ mod opt_tests {
66586658
Return v8
66596659
"#]]);
66606660
}
6661+
6662+
#[test]
6663+
fn test_guard_false_for_nil_opt() {
6664+
eval("
6665+
def test(val) = val.nil?
6666+
6667+
test(false)
6668+
");
6669+
assert_optimized_method_hir("test", expect![[r#"
6670+
fn test:
6671+
bb0(v0:BasicObject, v1:BasicObject):
6672+
PatchPoint MethodRedefined(FalseClass@0x1000, nil?@0x1008)
6673+
v7:FalseClassExact = GuardType v1, FalseClassExact
6674+
v8:FalseClassExact = CCall nil?@0x1010, v7
6675+
Return v8
6676+
"#]]);
6677+
}
6678+
6679+
#[test]
6680+
fn test_guard_true_for_nil_opt() {
6681+
eval("
6682+
def test(val) = val.nil?
6683+
6684+
test(true)
6685+
");
6686+
assert_optimized_method_hir("test", expect![[r#"
6687+
fn test:
6688+
bb0(v0:BasicObject, v1:BasicObject):
6689+
PatchPoint MethodRedefined(TrueClass@0x1000, nil?@0x1008)
6690+
v7:TrueClassExact = GuardType v1, TrueClassExact
6691+
v8:FalseClassExact = CCall nil?@0x1010, v7
6692+
Return v8
6693+
"#]]);
6694+
}
6695+
6696+
#[test]
6697+
fn test_guard_symbol_for_nil_opt() {
6698+
eval("
6699+
def test(val) = val.nil?
6700+
6701+
test(:foo)
6702+
");
6703+
assert_optimized_method_hir("test", expect![[r#"
6704+
fn test:
6705+
bb0(v0:BasicObject, v1:BasicObject):
6706+
PatchPoint MethodRedefined(Symbol@0x1000, nil?@0x1008)
6707+
v7:StaticSymbol = GuardType v1, StaticSymbol
6708+
v8:FalseClassExact = CCall nil?@0x1010, v7
6709+
Return v8
6710+
"#]]);
6711+
}
6712+
6713+
#[test]
6714+
fn test_guard_fixnum_for_nil_opt() {
6715+
eval("
6716+
def test(val) = val.nil?
6717+
6718+
test(1)
6719+
");
6720+
assert_optimized_method_hir("test", expect![[r#"
6721+
fn test:
6722+
bb0(v0:BasicObject, v1:BasicObject):
6723+
PatchPoint MethodRedefined(Integer@0x1000, nil?@0x1008)
6724+
v7:Fixnum = GuardType v1, Fixnum
6725+
v8:FalseClassExact = CCall nil?@0x1010, v7
6726+
Return v8
6727+
"#]]);
6728+
}
6729+
6730+
#[test]
6731+
fn test_guard_float_for_nil_opt() {
6732+
eval("
6733+
def test(val) = val.nil?
6734+
6735+
test(1.0)
6736+
");
6737+
assert_optimized_method_hir("test", expect![[r#"
6738+
fn test:
6739+
bb0(v0:BasicObject, v1:BasicObject):
6740+
PatchPoint MethodRedefined(Float@0x1000, nil?@0x1008)
6741+
v7:Flonum = GuardType v1, Flonum
6742+
v8:FalseClassExact = CCall nil?@0x1010, v7
6743+
Return v8
6744+
"#]]);
6745+
}
6746+
6747+
#[test]
6748+
fn test_guard_string_for_nil_opt() {
6749+
eval("
6750+
def test(val) = val.nil?
6751+
6752+
test('foo')
6753+
");
6754+
assert_optimized_method_hir("test", expect![[r#"
6755+
fn test:
6756+
bb0(v0:BasicObject, v1:BasicObject):
6757+
PatchPoint MethodRedefined(String@0x1000, nil?@0x1008)
6758+
v7:StringExact = GuardType v1, StringExact
6759+
v8:FalseClassExact = CCall nil?@0x1010, v7
6760+
Return v8
6761+
"#]]);
6762+
}
66616763
}

0 commit comments

Comments
 (0)