summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2026-02-05 20:38:00 +0100
committerBenoit Daloze <eregontp@gmail.com>2026-02-07 10:06:36 +0100
commitd066b9e01ccb2260fac8b2580c10e73335c7c7db (patch)
treeece08ae6b0309149e09a640a3b41537a6485ee13 /object.c
parent96d00640978d78ede1f5b2b63e422cfd1e849891 (diff)
Refactor type error to be more consistent
[Bug #21864] Co-Authored-By: Benoit Daloze <eregontp@gmail.com>
Diffstat (limited to 'object.c')
-rw-r--r--object.c67
1 files changed, 18 insertions, 49 deletions
diff --git a/object.c b/object.c
index 24202f069e..4dcd5d615f 100644
--- a/object.c
+++ b/object.c
@@ -3332,19 +3332,12 @@ convert_type_with_id(VALUE val, const char *tname, ID method, int raise, int ind
VALUE r = rb_check_funcall(val, method, 0, 0);
if (UNDEF_P(r)) {
if (raise) {
- const char *msg =
- ((index < 0 ? conv_method_index(rb_id2name(method)) : index)
- < IMPLICIT_CONVERSIONS) ?
- "no implicit conversion of" : "can't convert";
- const char *cname = NIL_P(val) ? "nil" :
- val == Qtrue ? "true" :
- val == Qfalse ? "false" :
- NULL;
- if (cname)
- rb_raise(rb_eTypeError, "%s %s into %s", msg, cname, tname);
- rb_raise(rb_eTypeError, "%s %"PRIsVALUE" into %s", msg,
- rb_obj_class(val),
- tname);
+ if ((index < 0 ? conv_method_index(rb_id2name(method)) : index) < IMPLICIT_CONVERSIONS) {
+ rb_no_implicit_conversion(val, tname);
+ }
+ else {
+ rb_cant_convert(val, tname);
+ }
}
return Qnil;
}
@@ -3360,17 +3353,6 @@ convert_type(VALUE val, const char *tname, const char *method, int raise)
return convert_type_with_id(val, tname, m, raise, i);
}
-/*! \private */
-NORETURN(static void conversion_mismatch(VALUE, const char *, const char *, VALUE));
-static void
-conversion_mismatch(VALUE val, const char *tname, const char *method, VALUE result)
-{
- VALUE cname = rb_obj_class(val);
- rb_raise(rb_eTypeError,
- "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")",
- cname, tname, cname, method, rb_obj_class(result));
-}
-
VALUE
rb_convert_type(VALUE val, int type, const char *tname, const char *method)
{
@@ -3379,7 +3361,7 @@ rb_convert_type(VALUE val, int type, const char *tname, const char *method)
if (TYPE(val) == type) return val;
v = convert_type(val, tname, method, TRUE);
if (TYPE(v) != type) {
- conversion_mismatch(val, tname, method, v);
+ rb_cant_convert_invalid_return(val, tname, method, v);
}
return v;
}
@@ -3393,7 +3375,7 @@ rb_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
if (TYPE(val) == type) return val;
v = convert_type_with_id(val, tname, method, TRUE, -1);
if (TYPE(v) != type) {
- conversion_mismatch(val, tname, RSTRING_PTR(rb_id2str(method)), v);
+ rb_cant_convert_invalid_return(val, tname, rb_id2name(method), v);
}
return v;
}
@@ -3408,7 +3390,7 @@ rb_check_convert_type(VALUE val, int type, const char *tname, const char *method
v = convert_type(val, tname, method, FALSE);
if (NIL_P(v)) return Qnil;
if (TYPE(v) != type) {
- conversion_mismatch(val, tname, method, v);
+ rb_cant_convert_invalid_return(val, tname, method, v);
}
return v;
}
@@ -3424,7 +3406,7 @@ rb_check_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
v = convert_type_with_id(val, tname, method, FALSE, -1);
if (NIL_P(v)) return Qnil;
if (TYPE(v) != type) {
- conversion_mismatch(val, tname, RSTRING_PTR(rb_id2str(method)), v);
+ rb_cant_convert_invalid_return(val, tname, rb_id2name(method), v);
}
return v;
}
@@ -3450,7 +3432,7 @@ rb_to_integer_with_id_exception(VALUE val, const char *method, ID mid, int raise
return Qnil;
}
if (!RB_INTEGER_TYPE_P(v)) {
- conversion_mismatch(val, "Integer", method, v);
+ rb_cant_convert_invalid_return(val, "Integer", method, v);
}
GET_EC()->cfp = current_cfp;
return v;
@@ -3527,7 +3509,7 @@ rb_convert_to_integer(VALUE val, int base, int raise_exception)
}
else if (NIL_P(val)) {
if (!raise_exception) return Qnil;
- rb_raise(rb_eTypeError, "can't convert nil into Integer");
+ rb_cant_convert(val, "Integer");
}
tmp = rb_protect(rb_check_to_int, val, NULL);
@@ -3821,18 +3803,6 @@ rat2dbl_without_to_f(VALUE x)
}
/*! \endcond */
-static inline void
-conversion_to_float(VALUE val)
-{
- special_const_to_float(val, "can't convert ", " into Float");
-}
-
-static inline void
-implicit_conversion_to_float(VALUE val)
-{
- special_const_to_float(val, "no implicit conversion to float from ", "");
-}
-
static int
to_float(VALUE *valp, int raise_exception)
{
@@ -3846,7 +3816,7 @@ to_float(VALUE *valp, int raise_exception)
return T_FLOAT;
}
else if (raise_exception) {
- conversion_to_float(val);
+ rb_cant_convert(val, "Float");
}
}
else {
@@ -3926,8 +3896,7 @@ static VALUE
numeric_to_float(VALUE val)
{
if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
- rb_raise(rb_eTypeError, "can't convert %"PRIsVALUE" into Float",
- rb_obj_class(val));
+ rb_cant_convert(val, "Float");
}
return rb_convert_type_with_id(val, T_FLOAT, "Float", id_to_f);
}
@@ -3971,7 +3940,7 @@ rb_num_to_dbl(VALUE val)
return rb_float_flonum_value(val);
}
else {
- conversion_to_float(val);
+ rb_cant_convert(val, "Float");
}
}
else {
@@ -4005,7 +3974,7 @@ rb_num2dbl(VALUE val)
return rb_float_flonum_value(val);
}
else {
- implicit_conversion_to_float(val);
+ rb_no_implicit_conversion(val, "Float");
}
}
else {
@@ -4017,7 +3986,7 @@ rb_num2dbl(VALUE val)
case T_RATIONAL:
return rat2dbl_without_to_f(val);
case T_STRING:
- rb_raise(rb_eTypeError, "no implicit conversion to float from string");
+ rb_no_implicit_conversion(val, "Float");
default:
break;
}
@@ -4111,7 +4080,7 @@ rb_Hash(VALUE val)
if (NIL_P(tmp)) {
if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
return rb_hash_new();
- rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
+ rb_cant_convert(val, "Hash");
}
return tmp;
}