Skip to content

Commit e8c5248

Browse files
committed
ZJIT: Annotate NilClass#nil?
1 parent f4ea42a commit e8c5248

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

test/ruby/test_zjit.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,25 @@ def test(x)
840840
}, call_threshold: 1, insns: [:branchnil]
841841
end
842842

843+
def test_nil_nil
844+
assert_compiles 'true', %q{
845+
def test = nil.nil?
846+
test
847+
test
848+
}
849+
end
850+
851+
def test_obsolete_nil_nil
852+
assert_compiles '1', %q{
853+
def test
854+
nil.nil?
855+
1
856+
end
857+
test
858+
test
859+
}
860+
end
861+
843862
# tool/ruby_vm/views/*.erb relies on the zjit instructions a) being contiguous and
844863
# b) being reliably ordered after all the other instructions.
845864
def test_instruction_order

zjit/src/cruby_methods.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub fn init() -> Annotations {
8080
annotate!(rb_cModule, "===", types::BoolExact, no_gc, leaf);
8181
annotate!(rb_cArray, "length", types::Fixnum, no_gc, leaf, elidable);
8282
annotate!(rb_cArray, "size", types::Fixnum, no_gc, leaf, elidable);
83+
annotate!(rb_cNilClass, "nil?", types::TrueClassExact, no_gc, leaf, elidable);
8384

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

zjit/src/hir.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6310,4 +6310,52 @@ mod opt_tests {
63106310
Return v7
63116311
"#]]);
63126312
}
6313+
6314+
#[test]
6315+
fn test_nil_nil_specialized_to_ccall() {
6316+
eval("
6317+
def test = nil.nil?
6318+
");
6319+
assert_optimized_method_hir("test", expect![[r#"
6320+
fn test:
6321+
bb0(v0:BasicObject):
6322+
v2:NilClassExact = Const Value(nil)
6323+
PatchPoint MethodRedefined(NilClass@0x1000, nil?@0x1008)
6324+
v7:TrueClassExact = CCall nil?@0x1010, v2
6325+
Return v7
6326+
"#]]);
6327+
}
6328+
6329+
#[test]
6330+
fn test_eliminate_nil_nil_specialized_to_ccall() {
6331+
eval("
6332+
def test
6333+
nil.nil?
6334+
1
6335+
end
6336+
");
6337+
assert_optimized_method_hir("test", expect![[r#"
6338+
fn test:
6339+
bb0(v0:BasicObject):
6340+
PatchPoint MethodRedefined(NilClass@0x1000, nil?@0x1008)
6341+
v5:Fixnum[1] = Const Value(1)
6342+
Return v5
6343+
"#]]);
6344+
}
6345+
6346+
#[test]
6347+
fn test_object_nil_specialized_to_send() {
6348+
eval("
6349+
def test = Object.new.nil?
6350+
");
6351+
assert_optimized_method_hir("test", expect![[r#"
6352+
fn test:
6353+
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
6359+
"#]]);
6360+
}
63136361
}

0 commit comments

Comments
 (0)