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

ruby-changes:61997

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 29 Jun 2020 11:06:50 +0900 (JST)
Subject: [ruby-changes:61997] a2677815f5 (master): rb_check_typeddata: do not goto into a branch

https://git.ruby-lang.org/ruby.git/commit/?id=a2677815f5

From a2677815f5e18116e37d7eb1d3e876798eb8e6d8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
 <shyouhei@r...>
Date: Mon, 15 Jun 2020 11:37:35 +0900
Subject: rb_check_typeddata: do not goto into a branch

I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.

diff --git a/error.c b/error.c
index fb86023..efe7bb3 100644
--- a/error.c
+++ b/error.c
@@ -908,23 +908,23 @@ rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type) https://github.com/ruby/ruby/blob/trunk/error.c#L908
     const char *etype;
 
     if (!RB_TYPE_P(obj, T_DATA)) {
-      wrong_type:
-	etype = builtin_class_name(obj);
-	if (!etype)
-	    rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)",
-		     rb_obj_class(obj), data_type->wrap_struct_name);
-      wrong_datatype:
-	rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
-		 etype, data_type->wrap_struct_name);
-    }
-    if (!RTYPEDDATA_P(obj)) {
-	goto wrong_type;
+        etype = builtin_class_name(obj);
+    }
+    else if (!RTYPEDDATA_P(obj)) {
+        etype = builtin_class_name(obj);
     }
     else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
-	etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name;
-	goto wrong_datatype;
+        etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name;
+    }
+    else {
+        return DATA_PTR(obj);
     }
-    return DATA_PTR(obj);
+
+    /* rb_obj_classname() cannot be used.  A class name can be non-ASCII. */
+    const char *expected = data_type->wrap_struct_name;
+    VALUE actual = (etype) ? rb_str_new_cstr(etype) : rb_obj_class(obj);
+    rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)",
+             actual, expected);
 }
 
 /* exception classes */
-- 
cgit v0.10.2


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

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