From eb47de300519d6680dd1f1180a427a7509f97b8f Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 27 Sep 2015 14:32:50 +0000 Subject: [PATCH] class.c: refine error messages * class.c (rb_define_class, rb_define_class_id_under): refine error messages. * class.c (rb_define_module, rb_define_module_id_under): ditto, and make consistent with class. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51958 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ class.c | 31 ++++++++++++++++++++----------- test/ruby/test_class.rb | 7 +++++++ test/ruby/test_module.rb | 7 +++++++ 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index ae27b780a9..a51d2f094b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Sun Sep 27 23:32:46 2015 Nobuyoshi Nakada + + * class.c (rb_define_class, rb_define_class_id_under): refine + error messages. + + * class.c (rb_define_module, rb_define_module_id_under): ditto, + and make consistent with class. + Sun Sep 27 18:44:43 2015 SHIBATA Hiroshi * ChangeLog: removed duplicated message. diff --git a/class.c b/class.c index 90b6c9dfa9..a1b3eba02c 100644 --- a/class.c +++ b/class.c @@ -636,7 +636,8 @@ rb_define_class(const char *name, VALUE super) if (rb_const_defined(rb_cObject, id)) { klass = rb_const_get(rb_cObject, id); if (!RB_TYPE_P(klass, T_CLASS)) { - rb_raise(rb_eTypeError, "%s is not a class", name); + rb_raise(rb_eTypeError, "%s is not a class (%"PRIsVALUE")", + name, rb_obj_class(klass)); } if (rb_class_real(RCLASS_SUPER(klass)) != super) { rb_raise(rb_eTypeError, "superclass mismatch for class %s", name); @@ -703,11 +704,15 @@ rb_define_class_id_under(VALUE outer, ID id, VALUE super) if (rb_const_defined_at(outer, id)) { klass = rb_const_get_at(outer, id); if (!RB_TYPE_P(klass, T_CLASS)) { - rb_raise(rb_eTypeError, "%"PRIsVALUE" is not a class", rb_id2str(id)); + rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a class" + " (%"PRIsVALUE")", + outer, rb_id2str(id), rb_obj_class(klass)); } if (rb_class_real(RCLASS_SUPER(klass)) != super) { - rb_raise(rb_eTypeError, "superclass mismatch for class %"PRIsVALUE"", - rb_id2str(id)); + rb_raise(rb_eTypeError, "superclass mismatch for class " + "%"PRIsVALUE"::%"PRIsVALUE"" + " (%"PRIsVALUE" is given but was %"PRIsVALUE")", + outer, rb_id2str(id), RCLASS_SUPER(klass), super); } return klass; } @@ -752,9 +757,11 @@ rb_define_module(const char *name) id = rb_intern(name); if (rb_const_defined(rb_cObject, id)) { module = rb_const_get(rb_cObject, id); - if (RB_TYPE_P(module, T_MODULE)) - return module; - rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module)); + if (!RB_TYPE_P(module, T_MODULE)) { + rb_raise(rb_eTypeError, "%s is not a module (%"PRIsVALUE")", + name, rb_obj_class(module)); + } + return module; } module = rb_define_module_id(id); rb_vm_add_root_module(id, module); @@ -776,10 +783,12 @@ rb_define_module_id_under(VALUE outer, ID id) if (rb_const_defined_at(outer, id)) { module = rb_const_get_at(outer, id); - if (RB_TYPE_P(module, T_MODULE)) - return module; - rb_raise(rb_eTypeError, "%s::%s is not a module", - rb_class2name(outer), rb_obj_classname(module)); + if (!RB_TYPE_P(module, T_MODULE)) { + rb_raise(rb_eTypeError, "%"PRIsVALUE"::%"PRIsVALUE" is not a module" + " (%"PRIsVALUE")", + outer, rb_id2str(id), rb_obj_class(module)); + } + return module; } module = rb_define_module_id(id); rb_const_set(outer, id, module); diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb index 9c016dd693..1d9ee55d7a 100644 --- a/test/ruby/test_class.rb +++ b/test/ruby/test_class.rb @@ -547,5 +547,12 @@ class TestClass < Test::Unit::TestCase assert_raise_with_message(TypeError, "#{n} is not a class") { m.module_eval "class #{n}; end" } + + assert_separately([], <<-"end;") + Date = (class C\u{1f5ff}; self; end).new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) { + require 'date' + } + end; end end diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index e04152f845..3d2d551a30 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -2103,6 +2103,13 @@ class TestModule < Test::Unit::TestCase assert_raise_with_message(TypeError, "#{n} is not a module") { m.module_eval "module #{n}; end" } + + assert_separately([], <<-"end;") + Etc = (class C\u{1f5ff}; self; end).new + assert_raise_with_message(TypeError, /C\u{1f5ff}/) { + require 'etc' + } + end; end private