From fa0ff7cf8672e25437299757d17261a186e69805 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 9 Jul 2022 15:13:52 +0900 Subject: [PATCH] numeric.c: better error message (Float -> Integer). --- src/numeric.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/numeric.c b/src/numeric.c index 7d2b3a8b4..aac802f11 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -33,6 +33,12 @@ mrb_int_zerodiv(mrb_state *mrb) mrb_raise(mrb, E_ZERODIV_ERROR, "divided by 0"); } +static mrb_noreturn void +mrb_int_noconv(mrb_state *mrb, mrb_value y) +{ + mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %Y into Integer", y); +} + /* * call-seq: * @@ -174,12 +180,12 @@ int_div(mrb_state *mrb, mrb_value x) x = mrb_complex_new(mrb, (mrb_float)a, 0); return mrb_complex_div(mrb, x, y); #endif - default: -#ifdef MRB_NO_FLOAT - mrb_raise(mrb, E_TYPE_ERROR, "non integer division"); -#else +#ifndef MRB_NO_FLOAT + case MRB_TT_FLOAT: return mrb_float_value(mrb, mrb_div_float((mrb_float)a, mrb_as_float(mrb, y))); #endif + default: + mrb_int_noconv(mrb, y); } } @@ -242,10 +248,11 @@ int_quo(mrb_state *mrb, mrb_value x) x = mrb_rational_new(mrb, a, 1); return mrb_rational_div(mrb, x, y); default: -#ifdef MRB_NO_FLOAT - mrb_raise(mrb, E_TYPE_ERROR, "non integer division"); -#else +#ifndef MRB_NO_FLOAT return mrb_float_value(mrb, mrb_div_float((mrb_float)a, mrb_as_float(mrb, y))); +#else + mrb_int_noconv(mrb, y); + break; #endif } #endif @@ -1133,7 +1140,7 @@ mrb_int_mul(mrb_state *mrb, mrb_value x, mrb_value y) return mrb_float_value(mrb, (mrb_float)a * mrb_as_float(mrb, y)); #endif default: - mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %Y into Integer", y); + mrb_int_noconv(mrb, y); } }