Skip to content

Commit 41e51dc

Browse files
committed
Annotate Kernel#nil?
1 parent e8c5248 commit 41e51dc

3 files changed

Lines changed: 43 additions & 7 deletions

File tree

test/ruby/test_zjit.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,25 @@ def test
859859
}
860860
end
861861

862+
def test_non_nil_nil
863+
assert_compiles 'false', %q{
864+
def test = 1.nil?
865+
test
866+
test
867+
}
868+
end
869+
870+
def test_obsolete_non_nil_nil
871+
assert_compiles '2', %q{
872+
def test
873+
1.nil?
874+
2
875+
end
876+
test
877+
test
878+
}
879+
end
880+
862881
# tool/ruby_vm/views/*.erb relies on the zjit instructions a) being contiguous and
863882
# b) being reliably ordered after all the other instructions.
864883
def test_instruction_order

zjit/src/cruby_methods.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub fn init() -> Annotations {
8181
annotate!(rb_cArray, "length", types::Fixnum, no_gc, leaf, elidable);
8282
annotate!(rb_cArray, "size", types::Fixnum, no_gc, leaf, elidable);
8383
annotate!(rb_cNilClass, "nil?", types::TrueClassExact, no_gc, leaf, elidable);
84+
annotate!(rb_mKernel, "nil?", types::FalseClassExact, no_gc, leaf, elidable);
8485

8586
Annotations {
8687
cfuncs: std::mem::take(cfuncs)

zjit/src/hir.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6344,18 +6344,34 @@ mod opt_tests {
63446344
}
63456345

63466346
#[test]
6347-
fn test_object_nil_specialized_to_send() {
6347+
fn test_non_nil_nil_specialized_to_ccall() {
63486348
eval("
6349-
def test = Object.new.nil?
6349+
def test = 1.nil?
63506350
");
63516351
assert_optimized_method_hir("test", expect![[r#"
63526352
fn test:
63536353
bb0(v0:BasicObject):
6354-
v3:BasicObject = GetConstantPath 0x1000
6355-
v4:NilClassExact = Const Value(nil)
6356-
v11:BasicObject = SendWithoutBlock v3, :new
6357-
v18:BasicObject = SendWithoutBlock v11, :nil?
6358-
Return v18
6354+
v2:Fixnum[1] = Const Value(1)
6355+
PatchPoint MethodRedefined(Integer@0x1000, nil?@0x1008)
6356+
v7:FalseClassExact = CCall nil?@0x1010, v2
6357+
Return v7
6358+
"#]]);
6359+
}
6360+
6361+
#[test]
6362+
fn test_eliminate_non_nil_nil_specialized_to_ccall() {
6363+
eval("
6364+
def test
6365+
1.nil?
6366+
2
6367+
end
6368+
");
6369+
assert_optimized_method_hir("test", expect![[r#"
6370+
fn test:
6371+
bb0(v0:BasicObject):
6372+
PatchPoint MethodRedefined(Integer@0x1000, nil?@0x1008)
6373+
v5:Fixnum[2] = Const Value(2)
6374+
Return v5
63596375
"#]]);
63606376
}
63616377
}

0 commit comments

Comments
 (0)