@@ -13,7 +13,7 @@ import Core.Intrinsics:
13
13
checked_srem_int,
14
14
checked_uadd_int, checked_usub_int, checked_umul_int, checked_udiv_int,
15
15
checked_urem_int
16
- import Base: no_op_err, @_inline_meta
16
+ import Base: no_op_err, @_inline_meta , @_noinline_meta
17
17
18
18
# define promotion behavior for checked operations
19
19
checked_add (x:: Integer , y:: Integer ) = checked_add (promote (x,y)... )
@@ -90,16 +90,18 @@ The overflow protection may impose a perceptible performance penalty.
90
90
function checked_neg (x:: T ) where T<: Integer
91
91
checked_sub (T (0 ), x)
92
92
end
93
+ throw_overflowerr_negation (x) = (@_noinline_meta ;
94
+ throw (OverflowError (" checked arithmetic: cannot compute -x for x = $x ::$(typeof (x)) " )))
93
95
if BrokenSignedInt != Union{}
94
96
function checked_neg (x:: BrokenSignedInt )
95
97
r = - x
96
- (x< 0 ) & (r< 0 ) && throw ( OverflowError () )
98
+ (x< 0 ) & (r< 0 ) && throw_overflowerr_negation (x )
97
99
r
98
100
end
99
101
end
100
102
if BrokenUnsignedInt != Union{}
101
103
function checked_neg (x:: T ) where T<: BrokenUnsignedInt
102
- x != 0 && throw ( OverflowError () )
104
+ x != 0 && throw_overflowerr_negation (x )
103
105
T (0 )
104
106
end
105
107
end
@@ -117,7 +119,7 @@ function checked_abs end
117
119
118
120
function checked_abs (x:: SignedInt )
119
121
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)) ))
121
123
r
122
124
end
123
125
checked_abs (x:: UnsignedInt ) = x
152
154
end
153
155
154
156
157
+ throw_overflowerr_binaryop (op, x, y) = (@_noinline_meta ;
158
+ throw (OverflowError (" $x $op $y overflowed for type $(typeof (x)) " )))
159
+
155
160
"""
156
161
Base.checked_add(x, y)
157
162
@@ -162,7 +167,7 @@ The overflow protection may impose a perceptible performance penalty.
162
167
function checked_add (x:: T , y:: T ) where T<: Integer
163
168
@_inline_meta
164
169
z, b = add_with_overflow (x, y)
165
- b && throw ( OverflowError () )
170
+ b && throw_overflowerr_binaryop (: + , x, y )
166
171
z
167
172
end
168
173
@@ -219,7 +224,7 @@ The overflow protection may impose a perceptible performance penalty.
219
224
function checked_sub (x:: T , y:: T ) where T<: Integer
220
225
@_inline_meta
221
226
z, b = sub_with_overflow (x, y)
222
- b && throw ( OverflowError () )
227
+ b && throw_overflowerr_binaryop (: - , x, y )
223
228
z
224
229
end
225
230
@@ -284,7 +289,7 @@ The overflow protection may impose a perceptible performance penalty.
284
289
function checked_mul (x:: T , y:: T ) where T<: Integer
285
290
@_inline_meta
286
291
z, b = mul_with_overflow (x, y)
287
- b && throw ( OverflowError () )
292
+ b && throw_overflowerr_binaryop (: * , x, y )
288
293
z
289
294
end
290
295
0 commit comments