Skip to content

Commit d236496

Browse files
committed
refact(core): add runtime_error and use where appropriate
Thus, there are three levels of errors in Rivet: - runtime error: This arises when an error occurs during some operation in the `core` module, for example a null pointer dereference. The exit code for this is 100. - panic: This occurs when something unexpected happens and there is no way to fix it, such as modifying a non-existent text file. The code for this is 101. - unhandled error: This occurs when an error is thrown and is not handled. The code for this is 102.
1 parent 5c2c70b commit d236496

File tree

11 files changed

+51
-42
lines changed

11 files changed

+51
-42
lines changed

lib/core/src/StringFormatter.ri

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct StringFormatter {
2626
arg_idx := args_idx;
2727
args_idx += 1;
2828
if arg_idx >= args.len {
29-
process_panic(
29+
runtime_error(
3030
"string.fmt(): argument index out of range (argument index: {}, len: {}) at index {}",
3131
arg_idx, args.len, self.i
3232
);
@@ -59,15 +59,15 @@ pub struct StringFormatter {
5959
buf.push(unsafe { self.buf.ptr[self.i] });
6060
self.i += 1;
6161
if self.i >= self.buf.len {
62-
process_panic(
62+
runtime_error(
6363
"string.fmt(): incomplete format string at index {}",
6464
start
6565
);
6666
}
6767
}
6868
index := buf.as_uint64();
6969
if index >= args.len {
70-
process_panic(
70+
runtime_error(
7171
"string.fmt(): argument index out of range (index: {}, len: {})",
7272
index, args.len
7373
);
@@ -89,7 +89,7 @@ pub struct StringFormatter {
8989
}
9090
}
9191
} else {
92-
process_panic(
92+
runtime_error(
9393
"string.fmt(): expecting closing `}}` in format string at index {}",
9494
self.i
9595
);
@@ -100,7 +100,7 @@ pub struct StringFormatter {
100100
self.res.write_byte(b'}');
101101
self.i += 1;
102102
} else {
103-
process_panic(
103+
runtime_error(
104104
"string.fmt(): single `}}` encountered in format string at index {}",
105105
self.i
106106
);
@@ -123,12 +123,12 @@ pub struct StringFormatter {
123123
buf.push(unsafe { self.buf.ptr[self.i] });
124124
self.i += 1;
125125
if self.i >= self.buf.len {
126-
process_panic("string.fmt(): incomplete format string (index: {})", start);
126+
runtime_error("string.fmt(): incomplete format string (index: {})", start);
127127
}
128128
}
129129
fwidth := buf.as_int();
130130
if fwidth == 0 {
131-
process_panic(
131+
runtime_error(
132132
"string.fmt(): invalid width value (cannot be 0 and cannot be omitted) -> (index: {})",
133133
start
134134
);

lib/core/src/Vector.c.ri

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ struct Vector {
3939

4040
func get(self, idx: uint) -> rawptr {
4141
if idx >= self.len {
42-
process_panic("vector index out of range (index: {}, len: {})", idx, self.len);
42+
runtime_error("vector index out of range (index: {}, len: {})", idx, self.len);
4343
}
4444
return unsafe { @ptr_add(@as([&]mut uint8, self.ptr), idx * self.elem_size) };
4545
}
4646

4747
func set(self, idx: uint, val: rawptr) {
4848
if idx >= self.len {
49-
process_panic("vector index out of range (index: {}, len: {})", idx, self.len);
49+
runtime_error("vector index out of range (index: {}, len: {})", idx, self.len);
5050
}
5151
unsafe {
5252
mem_copy(
@@ -70,7 +70,7 @@ struct Vector {
7070
/// vector is empty, this will panic.
7171
func pop(mut self) -> rawptr {
7272
if self.len == 0 {
73-
process_panic("Vec.pop: vector is empty");
73+
runtime_error("Vec.pop: vector is empty");
7474
}
7575
new_len := self.len - 1;
7676
self.len = new_len;
@@ -84,8 +84,8 @@ struct Vector {
8484
pub func delete(mut self, i: uint, size: uint := 1, no_slices: bool := false) {
8585
if i + size > self.len {
8686
end_idx := if size == 1 { "..{}".fmt(i + size) } else { "" };
87-
process_panic(
88-
"Vector.delete(): index out of range (i: {}{}, self.len: {})",
87+
runtime_error(
88+
"Vector.delete(): index out of range (i: {}..{}, self.len: {})",
8989
i, end_idx, self.len
9090
);
9191
}
@@ -156,7 +156,7 @@ struct Vector {
156156

157157
func slice(self, start: uint, end: uint) -> Self {
158158
if start > end or end > self.len {
159-
process_panic(
159+
runtime_error(
160160
"slice index out of range (range: {}..{}, len: {})", start, end, self.len
161161
);
162162
}

lib/core/src/array.c.ri

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import c/libc;
66

77
func array_index(len: uint, idx: uint) {
88
if idx >= len {
9-
process_panic("array index out of range (index: {}, size: {})", idx, len);
9+
runtime_error("array index out of range (index: {}, size: {})", idx, len);
1010
}
1111
}
1212

@@ -22,7 +22,7 @@ func array_ne(arr: rawptr, other_arr: rawptr, len: uint) -> bool {
2222

2323
func array_slice(arr: rawptr, elem_size: uint, size: uint, start: uint, end: uint) -> Vector {
2424
if start > end or end > size {
25-
process_panic("slice index out of range (range: {}..{}, len: {})", start, end, size);
25+
runtime_error("slice index out of range (range: {}..{}, len: {})", start, end, size);
2626
}
2727
len := end - start;
2828
return unsafe {

lib/core/src/entry_point.ri

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,20 @@ func main(
3030
) {
3131
setup_signals_handler();
3232
init_args(@as(uint, _argc), _argv);
33+
3334
#if !_TESTS_ // `init_string_lits` is called after
3435
init_string_lits();
3536
#endif
3637
init_globals();
3738
libcoreIsStarted = true;
39+
3840
#if _TESTS_
3941
if test_runner.tests.len > 0 {
4042
test_runner.run();
4143
}
4244
#else
4345
mod_main();
4446
#endif
47+
4548
drop_globals();
4649
}

lib/core/src/errors.ri

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct ReturnTrace {
3030
#[inline]
3131
func add(mut self, trace: CallTrace) {
3232
if self.cur_idx == RETURN_TRACE_MAX_SIZE {
33-
process_panic("maximum return trace size exceeded");
33+
runtime_error("maximum return trace size exceeded");
3434
}
3535
self.traces[self.cur_idx] = trace;
3636
self.cur_idx += 1;

lib/core/src/lib.ri

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44

55
var mut libcoreIsStarted := false;
66

7-
func panic_if(cond: bool, msg: string) {
8-
if cond {
9-
console_ewriteln("panic: {}", msg);
10-
bt_print(2);
11-
process_exit(1);
12-
}
13-
}
14-
157
func assert(cond: bool, msg: string) {
168
if !cond {
179
console_ewriteln("panic: assertion failed: {}", msg);
@@ -20,10 +12,16 @@ func assert(cond: bool, msg: string) {
2012
}
2113
}
2214

15+
func runtime_error(s: string, args: ...Stringable) -> never {
16+
console_ewriteln("runtime error: {}", s.fmt(args));
17+
bt_print(2);
18+
process_exit(100);
19+
}
20+
2321
#[inline]
2422
func internal_alloc(size: uint) -> rawptr {
2523
return mem_alloc(size) catch {
26-
process_panic(
24+
runtime_error(
2725
"internal error: cannot allocate memory" #if _DEBUG_ " (size: {})", size #endif
2826
)
2927
};
@@ -32,7 +30,7 @@ func internal_alloc(size: uint) -> rawptr {
3230
#[inline]
3331
func internal_zeroed(size: uint) -> rawptr {
3432
return mem_zeroed(size) catch {
35-
process_panic(
33+
runtime_error(
3634
"internal error: cannot allocate zeroed memory" #if _DEBUG_ " (size: {})", size #endif
3735
)
3836
};
@@ -41,7 +39,7 @@ func internal_zeroed(size: uint) -> rawptr {
4139
#[inline]
4240
func internal_dup(src: rawptr, sz: uint) -> rawptr {
4341
return mem_dup(src, sz) catch {
44-
process_panic(
42+
runtime_error(
4543
"internal error: cannot duplicate memory" #if _DEBUG_ " (size: {})", sz #endif
4644
)
4745
};
@@ -50,7 +48,7 @@ func internal_dup(src: rawptr, sz: uint) -> rawptr {
5048
#[inline]
5149
func internal_resize(ptr: ?rawptr, sz: uint) -> rawptr {
5250
return mem_resize(ptr, sz) catch {
53-
process_panic(
51+
runtime_error(
5452
"internal error: cannot resize memory" #if _DEBUG_ " (size: {})", sz #endif
5553
)
5654
};

lib/core/src/rune.c.ri

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ extend rune < Stringable {
3838
/// Panics if given a radix larger than 36.
3939
pub func from_digit(num: uint32, radix: uint32) -> ?rune {
4040
if radix > 36 {
41-
process_panic("rune.from_digit: radix is too high (maximum 36)");
41+
runtime_error("rune.from_digit: radix is too high (maximum 36)");
4242
}
4343
if num < radix {
4444
num_ := @as(uint8, num);
45-
byte := (if num_ < 10 { b'0' + num_ } else { b'a' + num_ - 10 });
45+
byte := if num_ < 10 { b'0' + num_ } else { b'a' + num_ - 10 };
4646
return byte.to_rune();
4747
}
4848
return none;

lib/core/src/signals.c.ri

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ func signals_handler(_sig: int32, info: &mut libc.siginfo_t, _context: rawptr) {
2626
func segfault_handler() {
2727
console_ewriteln(
2828
if libcoreIsStarted {
29-
"panic: invalid memory address or null pointer dereference"
29+
"runtime error: invalid memory address or null pointer dereference"
3030
} else {
31-
"panic: invalid memory address or null pointer dereference while starting the core"
31+
"runtime error: invalid memory address or null pointer dereference while starting the core"
3232
}
3333
);
3434
bt_print(4);
35-
process_exit(1);
35+
process_exit(100);
3636
}
3737

3838
func fpe_handler() {
3939
console_ewriteln(
4040
if libcoreIsStarted {
41-
"panic: floating point exception"
41+
"runtime error: floating point exception"
4242
} else {
43-
"panic: floating point exception while starting the core"
43+
"runtime error: floating point exception while starting the core"
4444
}
4545
);
4646
bt_print(4);
47-
process_exit(1);
47+
process_exit(100);
4848
}

lib/core/src/string.c.ri

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct string < Stringable, Hashable, Throwable {
2929
}
3030
return if safe_ptr := ptr {
3131
if unsafe { safe_ptr[len] != 0 } {
32-
process_panic("`ptr` is not a valid string, doesn't ends with NLL byte");
32+
runtime_error("`ptr` is not a valid string, doesn't ends with NLL byte");
3333
}
3434
Self(safe_ptr, len, is_ref)
3535
} else {
@@ -62,7 +62,7 @@ pub struct string < Stringable, Hashable, Throwable {
6262

6363
pub func at(self, idx: uint) -> uint8 {
6464
if idx >= self.len {
65-
process_panic("string index out of range (index: {}, len: {})", idx, self.len);
65+
runtime_error("string index out of range (index: {}, len: {})", idx, self.len);
6666
}
6767
return unsafe { self.ptr[idx] };
6868
}
@@ -788,7 +788,7 @@ pub struct string < Stringable, Hashable, Throwable {
788788
func slice(self, start: uint, end: uint) -> Self {
789789
unsafe {
790790
if start > end or start > self.len or end > self.len {
791-
process_panic(
791+
runtime_error(
792792
"string slice index out of range (range: {}..{}, len: {})", start,
793793
end, self.len
794794
);

lib/core/src/utils.c.ri

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func enum_cast(ptr: rawptr, expected_id: uint) -> rawptr {
1313
if enum_info._id_ != expected_id {
1414
// TODO: replace indexes with names
1515
console_ewriteln(
16-
"panic: enum cast: cannot cast ID({}) to ID({})", enum_info._id_, expected_id
16+
"runtime error: enum cast: cannot cast ID({}) to ID({})", enum_info._id_, expected_id
1717
);
1818
bt_print(2);
1919
process_exit(1);
@@ -25,7 +25,7 @@ func trait_cast(got_obj: rawptr, got_id: uint, expected_id: uint) -> rawptr {
2525
if got_id != expected_id {
2626
// TODO: replace indexes with names
2727
console_ewriteln(
28-
"panic: trait cast: cannot cast ID({}) to ID({})", got_id, expected_id
28+
"runtime error: trait cast: cannot cast ID({}) to ID({})", got_id, expected_id
2929
);
3030
bt_print(2);
3131
process_exit(1);

rivetc/src/codegen/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ def gen_expr(self, expr, custom_tmp = None):
982982
self.gen_expr(expr.args[1])]
983983
)
984984
elif expr.name == "unreachable":
985-
self.panic("entered unreachable code")
985+
self.runtime_error("entered unreachable code")
986986
elif expr.name == "breakpoint" and self.comp.prefs.build_mode != prefs.BuildMode.Release:
987987
self.cur_fn.breakpoint()
988988
elif isinstance(expr, ast.TupleLiteral):
@@ -2620,6 +2620,14 @@ def option_none(self, typ):
26202620
)
26212621
return tmp
26222622

2623+
def runtime_error(self, msg):
2624+
self.cur_fn.add_call(
2625+
"_R4core13runtime_errorF", [
2626+
self.gen_string_literal(utils.smart_quote(msg, False)),
2627+
self.empty_vec(self.comp.universe["[]core.Stringable"])
2628+
]
2629+
)
2630+
26232631
def panic(self, msg):
26242632
self.cur_fn.add_call(
26252633
"_R4core13process_panicF", [

0 commit comments

Comments
 (0)