[前][次][番号順一覧][スレッド一覧]

ruby-changes:39877

From: nobu <ko1@a...>
Date: Sun, 27 Sep 2015 23:33:22 +0900 (JST)
Subject: [ruby-changes:39877] nobu:r51958 (trunk): class.c: refine error messages

nobu	2015-09-27 23:32:50 +0900 (Sun, 27 Sep 2015)

  New Revision: 51958

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51958

  Log:
    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.

  Modified files:
    trunk/ChangeLog
    trunk/class.c
    trunk/test/ruby/test_class.rb
    trunk/test/ruby/test_module.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51957)
+++ ChangeLog	(revision 51958)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Sep 27 23:32:46 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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  <hsbt@r...>
 
 	* ChangeLog: removed duplicated message.
Index: class.c
===================================================================
--- class.c	(revision 51957)
+++ class.c	(revision 51958)
@@ -636,7 +636,8 @@ rb_define_class(const char *name, VALUE https://github.com/ruby/ruby/blob/trunk/class.c#L636
     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 https://github.com/ruby/ruby/blob/trunk/class.c#L704
     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) https://github.com/ruby/ruby/blob/trunk/class.c#L757
     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, I https://github.com/ruby/ruby/blob/trunk/class.c#L783
 
     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);
Index: test/ruby/test_module.rb
===================================================================
--- test/ruby/test_module.rb	(revision 51957)
+++ test/ruby/test_module.rb	(revision 51958)
@@ -2103,6 +2103,13 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L2103
     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
Index: test/ruby/test_class.rb
===================================================================
--- test/ruby/test_class.rb	(revision 51957)
+++ test/ruby/test_class.rb	(revision 51958)
@@ -547,5 +547,12 @@ class TestClass < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_class.rb#L547
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]