aboutsummaryrefslogtreecommitdiffstats
path: root/error.c
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2020-05-04 16:56:45 +0200
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-05-26 14:10:33 +0900
commit385ac07fd8f5a50825aee8db459086e617f492aa (patch)
treefc1d12126f2f04cb5032e9e7329157dabb6ef6d6 /error.c
parent4e1f2283b432e833bd4e6f7724ba0496760e68e8 (diff)
downloadruby-385ac07fd8f5a50825aee8db459086e617f492aa.tar.gz
Use receiver #name rather than #inspect to build NameError message
Diffstat (limited to 'error.c')
-rw-r--r--error.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/error.c b/error.c
index 31ffc32723..553f9c7daa 100644
--- a/error.c
+++ b/error.c
@@ -1763,6 +1763,17 @@ name_err_mesg_equal(VALUE obj1, VALUE obj2)
/* :nodoc: */
static VALUE
+name_err_mesg_receiver_name(VALUE obj)
+{
+ if (RB_SPECIAL_CONST_P(obj)) return Qundef;
+ if (RB_BUILTIN_TYPE(obj) == T_MODULE || RB_BUILTIN_TYPE(obj) == T_CLASS) {
+ return rb_check_funcall(obj, rb_intern("name"), 0, 0);
+ }
+ return Qundef;
+}
+
+/* :nodoc: */
+static VALUE
name_err_mesg_to_str(VALUE obj)
{
VALUE *ptr, mesg;
@@ -1789,7 +1800,9 @@ name_err_mesg_to_str(VALUE obj)
d = FAKE_CSTR(&d_str, "false");
break;
default:
- d = rb_protect(rb_inspect, obj, &state);
+ d = rb_protect(name_err_mesg_receiver_name, obj, &state);
+ if (state || d == Qundef || d == Qnil)
+ d = rb_protect(rb_inspect, obj, &state);
if (state)
rb_set_errinfo(Qnil);
if (NIL_P(d)) {