@@ -68,7 +68,11 @@ impl<'a> VM<'a> {
6868 HeapObj :: Tuple ( v) => v. len ( ) as i64 ,
6969 HeapObj :: Dict ( v) => v. borrow ( ) . len ( ) as i64 ,
7070 HeapObj :: Set ( v) => v. borrow ( ) . len ( ) as i64 ,
71- HeapObj :: Range ( s, e, st) => { let st=* st; ( ( e-s+st-st. signum ( ) ) /st) . max ( 0 ) }
71+ HeapObj :: Range ( s, e, st) => {
72+ let ( s, e, st) = ( * s as i128 , * e as i128 , * st as i128 ) ;
73+ if st == 0 { return Err ( cold_value ( "range() step cannot be zero" ) ) ; }
74+ ( ( ( e - s + st - st. signum ( ) ) / st) . max ( 0 ) ) as i64
75+ }
7276 _ => return Err ( cold_type ( "object has no len()" ) ) ,
7377 } } else { return Err ( cold_type ( "object has no len()" ) ) ; } ;
7478 self . push ( Val :: int ( n) ) ; Ok ( ( ) )
@@ -237,8 +241,17 @@ impl<'a> VM<'a> {
237241 HeapObj :: Range ( s, e, st) if include_range => {
238242 let ( mut cur, end, step) = ( * s, * e, * st) ;
239243 let mut out = Vec :: new ( ) ;
240- if step > 0 { while cur < end { out. push ( Val :: int ( cur) ) ; cur += step; } }
241- else { while cur > end { out. push ( Val :: int ( cur) ) ; cur += step; } }
244+ if step > 0 {
245+ while cur < end {
246+ out. push ( Val :: int ( cur) ) ;
247+ match cur. checked_add ( step) { Some ( n) => cur = n, None => break }
248+ }
249+ } else {
250+ while cur > end {
251+ out. push ( Val :: int ( cur) ) ;
252+ match cur. checked_add ( step) { Some ( n) => cur = n, None => break }
253+ }
254+ }
242255 Some ( out)
243256 }
244257 HeapObj :: Dict ( d) => Some ( d. borrow ( ) . keys ( ) . collect ( ) ) ,
0 commit comments