Include inspect value of object in FrozenError messages

FrozenError#receiver was added recently for getting the related
object programmatically.  However, there are cases where FrozenError
is raised and not handled, and in those cases the resulting error
messages lack detail, which makes debugging the error more difficult,
especially in cases where the error is not easily reproducible.
This includes the inspect value of the frozen object in FrozenError
messages, which should make debugging simpler.
This commit is contained in:
Jeremy Evans 2019-05-27 17:52:35 -07:00
Родитель 96d6527424
Коммит f1f04caf60
4 изменённых файлов: 8 добавлений и 8 удалений

10
error.c
Просмотреть файл

@ -2897,13 +2897,13 @@ rb_error_frozen_object(VALUE frozen_obj)
VALUE path = rb_ary_entry(debug_info, 0);
VALUE line = rb_ary_entry(debug_info, 1);
rb_frozen_error_raise(frozen_obj,
"can't modify frozen %"PRIsVALUE", created at %"PRIsVALUE":%"PRIsVALUE,
CLASS_OF(frozen_obj), path, line);
rb_frozen_error_raise(frozen_obj,
"can't modify frozen %"PRIsVALUE": %"PRIsVALUE", created at %"PRIsVALUE":%"PRIsVALUE,
CLASS_OF(frozen_obj), rb_inspect(frozen_obj), path, line);
}
else {
rb_frozen_error_raise(frozen_obj, "can't modify frozen %"PRIsVALUE,
CLASS_OF(frozen_obj));
rb_frozen_error_raise(frozen_obj, "can't modify frozen %"PRIsVALUE": %"PRIsVALUE,
CLASS_OF(frozen_obj), rb_inspect(frozen_obj));
}
}

2
eval.c
Просмотреть файл

@ -454,7 +454,7 @@ rb_class_modify_check(VALUE klass)
goto noclass;
}
}
rb_frozen_error_raise(klass, "can't modify frozen %s", desc);
rb_frozen_error_raise(klass, "can't modify frozen %s: %"PRIsVALUE, desc, klass);
}
}

Просмотреть файл

@ -988,7 +988,7 @@ class TestRubyOptions < Test::Unit::TestCase
def test_frozen_string_literal_debug
with_debug_pat = /created at/
wo_debug_pat = /can\'t modify frozen String \(FrozenError\)\n\z/
wo_debug_pat = /can\'t modify frozen String: "\w+" \(FrozenError\)\n\z/
frozen = [
["--enable-frozen-string-literal", true],
["--disable-frozen-string-literal", false],

Просмотреть файл

@ -135,7 +135,7 @@ class TestVariable < Test::Unit::TestCase
def test_special_constant_ivars
[ true, false, :symbol, "dsym#{rand(9999)}".to_sym, 1, 1.0 ].each do |v|
assert_empty v.instance_variables
msg = "can't modify frozen #{v.class}"
msg = "can't modify frozen #{v.class}: #{v.inspect}"
assert_raise_with_message(FrozenError, msg) do
v.instance_variable_set(:@foo, :bar)