Skip to content

Commit dc4e7fb

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

3 files changed

Lines changed: 40 additions & 8 deletions

File tree

test/ruby/test_zjit.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,6 @@ def test_nil_nil
844844
assert_compiles 'true', %q{
845845
def test = nil.nil?
846846
test
847-
test
848847
}
849848
end
850849

@@ -855,6 +854,22 @@ def test
855854
1
856855
end
857856
test
857+
}
858+
end
859+
860+
def test_non_nil_nil
861+
assert_compiles 'false', %q{
862+
def test = 1.nil?
863+
test
864+
}
865+
end
866+
867+
def test_obsolete_non_nil_nil
868+
assert_compiles '2', %q{
869+
def test
870+
1.nil?
871+
2
872+
end
858873
test
859874
}
860875
end

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)