@@ -3332,19 +3332,12 @@ convert_type_with_id(VALUE val, const char *tname, ID method, int raise, int ind
33323332 VALUE r = rb_check_funcall (val , method , 0 , 0 );
33333333 if (UNDEF_P (r )) {
33343334 if (raise ) {
3335- const char * msg =
3336- ((index < 0 ? conv_method_index (rb_id2name (method )) : index )
3337- < IMPLICIT_CONVERSIONS ) ?
3338- "no implicit conversion of" : "can't convert" ;
3339- const char * cname = NIL_P (val ) ? "nil" :
3340- val == Qtrue ? "true" :
3341- val == Qfalse ? "false" :
3342- NULL ;
3343- if (cname )
3344- rb_raise (rb_eTypeError , "%s %s into %s" , msg , cname , tname );
3345- rb_raise (rb_eTypeError , "%s %" PRIsVALUE " into %s" , msg ,
3346- rb_obj_class (val ),
3347- tname );
3335+ if ((index < 0 ? conv_method_index (rb_id2name (method )) : index ) < IMPLICIT_CONVERSIONS ) {
3336+ rb_no_implicit_conversion (val , tname );
3337+ }
3338+ else {
3339+ rb_cant_convert (val , tname );
3340+ }
33483341 }
33493342 return Qnil ;
33503343 }
@@ -3360,17 +3353,6 @@ convert_type(VALUE val, const char *tname, const char *method, int raise)
33603353 return convert_type_with_id (val , tname , m , raise , i );
33613354}
33623355
3363- /*! \private */
3364- NORETURN (static void conversion_mismatch (VALUE , const char * , const char * , VALUE ));
3365- static void
3366- conversion_mismatch (VALUE val , const char * tname , const char * method , VALUE result )
3367- {
3368- VALUE cname = rb_obj_class (val );
3369- rb_raise (rb_eTypeError ,
3370- "can't convert %" PRIsVALUE " to %s (%" PRIsVALUE "#%s gives %" PRIsVALUE ")" ,
3371- cname , tname , cname , method , rb_obj_class (result ));
3372- }
3373-
33743356VALUE
33753357rb_convert_type (VALUE val , int type , const char * tname , const char * method )
33763358{
@@ -3379,7 +3361,7 @@ rb_convert_type(VALUE val, int type, const char *tname, const char *method)
33793361 if (TYPE (val ) == type ) return val ;
33803362 v = convert_type (val , tname , method , TRUE);
33813363 if (TYPE (v ) != type ) {
3382- conversion_mismatch (val , tname , method , v );
3364+ rb_cant_convert_invalid_return (val , tname , method , v );
33833365 }
33843366 return v ;
33853367}
@@ -3393,7 +3375,7 @@ rb_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
33933375 if (TYPE (val ) == type ) return val ;
33943376 v = convert_type_with_id (val , tname , method , TRUE, -1 );
33953377 if (TYPE (v ) != type ) {
3396- conversion_mismatch (val , tname , RSTRING_PTR ( rb_id2str ( method ) ), v );
3378+ rb_cant_convert_invalid_return (val , tname , rb_id2name ( method ), v );
33973379 }
33983380 return v ;
33993381}
@@ -3408,7 +3390,7 @@ rb_check_convert_type(VALUE val, int type, const char *tname, const char *method
34083390 v = convert_type (val , tname , method , FALSE);
34093391 if (NIL_P (v )) return Qnil ;
34103392 if (TYPE (v ) != type ) {
3411- conversion_mismatch (val , tname , method , v );
3393+ rb_cant_convert_invalid_return (val , tname , method , v );
34123394 }
34133395 return v ;
34143396}
@@ -3424,7 +3406,7 @@ rb_check_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
34243406 v = convert_type_with_id (val , tname , method , FALSE, -1 );
34253407 if (NIL_P (v )) return Qnil ;
34263408 if (TYPE (v ) != type ) {
3427- conversion_mismatch (val , tname , RSTRING_PTR ( rb_id2str ( method ) ), v );
3409+ rb_cant_convert_invalid_return (val , tname , rb_id2name ( method ), v );
34283410 }
34293411 return v ;
34303412}
@@ -3450,7 +3432,7 @@ rb_to_integer_with_id_exception(VALUE val, const char *method, ID mid, int raise
34503432 return Qnil ;
34513433 }
34523434 if (!RB_INTEGER_TYPE_P (v )) {
3453- conversion_mismatch (val , "Integer" , method , v );
3435+ rb_cant_convert_invalid_return (val , "Integer" , method , v );
34543436 }
34553437 GET_EC ()-> cfp = current_cfp ;
34563438 return v ;
@@ -3527,7 +3509,7 @@ rb_convert_to_integer(VALUE val, int base, int raise_exception)
35273509 }
35283510 else if (NIL_P (val )) {
35293511 if (!raise_exception ) return Qnil ;
3530- rb_raise ( rb_eTypeError , "can't convert nil into Integer" );
3512+ rb_cant_convert ( val , "Integer" );
35313513 }
35323514
35333515 tmp = rb_protect (rb_check_to_int , val , NULL );
@@ -3821,18 +3803,6 @@ rat2dbl_without_to_f(VALUE x)
38213803 }
38223804/*! \endcond */
38233805
3824- static inline void
3825- conversion_to_float (VALUE val )
3826- {
3827- special_const_to_float (val , "can't convert " , " into Float" );
3828- }
3829-
3830- static inline void
3831- implicit_conversion_to_float (VALUE val )
3832- {
3833- special_const_to_float (val , "no implicit conversion to float from " , "" );
3834- }
3835-
38363806static int
38373807to_float (VALUE * valp , int raise_exception )
38383808{
@@ -3846,7 +3816,7 @@ to_float(VALUE *valp, int raise_exception)
38463816 return T_FLOAT ;
38473817 }
38483818 else if (raise_exception ) {
3849- conversion_to_float (val );
3819+ rb_cant_convert (val , "Float" );
38503820 }
38513821 }
38523822 else {
@@ -3926,8 +3896,7 @@ static VALUE
39263896numeric_to_float (VALUE val )
39273897{
39283898 if (!rb_obj_is_kind_of (val , rb_cNumeric )) {
3929- rb_raise (rb_eTypeError , "can't convert %" PRIsVALUE " into Float" ,
3930- rb_obj_class (val ));
3899+ rb_cant_convert (val , "Float" );
39313900 }
39323901 return rb_convert_type_with_id (val , T_FLOAT , "Float" , id_to_f );
39333902}
@@ -3971,7 +3940,7 @@ rb_num_to_dbl(VALUE val)
39713940 return rb_float_flonum_value (val );
39723941 }
39733942 else {
3974- conversion_to_float (val );
3943+ rb_cant_convert (val , "Float" );
39753944 }
39763945 }
39773946 else {
@@ -4005,7 +3974,7 @@ rb_num2dbl(VALUE val)
40053974 return rb_float_flonum_value (val );
40063975 }
40073976 else {
4008- implicit_conversion_to_float (val );
3977+ rb_no_implicit_conversion (val , "Float" );
40093978 }
40103979 }
40113980 else {
@@ -4017,7 +3986,7 @@ rb_num2dbl(VALUE val)
40173986 case T_RATIONAL :
40183987 return rat2dbl_without_to_f (val );
40193988 case T_STRING :
4020- rb_raise ( rb_eTypeError , "no implicit conversion to float from string " );
3989+ rb_no_implicit_conversion ( val , "Float " );
40213990 default :
40223991 break ;
40233992 }
@@ -4111,7 +4080,7 @@ rb_Hash(VALUE val)
41114080 if (NIL_P (tmp )) {
41124081 if (RB_TYPE_P (val , T_ARRAY ) && RARRAY_LEN (val ) == 0 )
41134082 return rb_hash_new ();
4114- rb_raise ( rb_eTypeError , "can't convert %s into Hash" , rb_obj_classname ( val ) );
4083+ rb_cant_convert ( val , "Hash" );
41154084 }
41164085 return tmp ;
41174086}
0 commit comments