@@ -13,7 +13,7 @@ import Core.Intrinsics:
1313 checked_srem_int,
1414 checked_uadd_int, checked_usub_int, checked_umul_int, checked_udiv_int,
1515 checked_urem_int
16- import Base: no_op_err, @_inline_meta
16+ import Base: no_op_err, @_inline_meta , @_noinline_meta
1717
1818# define promotion behavior for checked operations
1919checked_add (x:: Integer , y:: Integer ) = checked_add (promote (x,y)... )
@@ -90,16 +90,18 @@ The overflow protection may impose a perceptible performance penalty.
9090function checked_neg (x:: T ) where T<: Integer
9191 checked_sub (T (0 ), x)
9292end
93+ throw_overflowerr_negation (x) = (@_noinline_meta ;
94+ throw (OverflowError (" checked arithmetic: cannot compute -x for x = $x ::$(typeof (x)) " )))
9395if BrokenSignedInt != Union{}
9496function checked_neg (x:: BrokenSignedInt )
9597 r = - x
96- (x< 0 ) & (r< 0 ) && throw ( OverflowError () )
98+ (x< 0 ) & (r< 0 ) && throw_overflowerr_negation (x )
9799 r
98100end
99101end
100102if BrokenUnsignedInt != Union{}
101103function checked_neg (x:: T ) where T<: BrokenUnsignedInt
102- x != 0 && throw ( OverflowError () )
104+ x != 0 && throw_overflowerr_negation (x )
103105 T (0 )
104106end
105107end
@@ -117,7 +119,7 @@ function checked_abs end
117119
118120function checked_abs (x:: SignedInt )
119121 r = ifelse (x< 0 , - x, x)
120- r< 0 && throw (OverflowError ())
122+ r< 0 && throw (OverflowError (string ( " checked arithmetic: cannot compute |x| for x = " , x, " :: " , typeof (x)) ))
121123 r
122124 end
123125checked_abs (x:: UnsignedInt ) = x
152154end
153155
154156
157+ throw_overflowerr_binaryop (op, x, y) = (@_noinline_meta ;
158+ throw (OverflowError (" $x $op $y overflowed for type $(typeof (x)) " )))
159+
155160"""
156161 Base.checked_add(x, y)
157162
@@ -162,7 +167,7 @@ The overflow protection may impose a perceptible performance penalty.
162167function checked_add (x:: T , y:: T ) where T<: Integer
163168 @_inline_meta
164169 z, b = add_with_overflow (x, y)
165- b && throw ( OverflowError () )
170+ b && throw_overflowerr_binaryop (: + , x, y )
166171 z
167172end
168173
@@ -219,7 +224,7 @@ The overflow protection may impose a perceptible performance penalty.
219224function checked_sub (x:: T , y:: T ) where T<: Integer
220225 @_inline_meta
221226 z, b = sub_with_overflow (x, y)
222- b && throw ( OverflowError () )
227+ b && throw_overflowerr_binaryop (: - , x, y )
223228 z
224229end
225230
@@ -284,7 +289,7 @@ The overflow protection may impose a perceptible performance penalty.
284289function checked_mul (x:: T , y:: T ) where T<: Integer
285290 @_inline_meta
286291 z, b = mul_with_overflow (x, y)
287- b && throw ( OverflowError () )
292+ b && throw_overflowerr_binaryop (: * , x, y )
288293 z
289294end
290295
0 commit comments