Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update zig @panic calls to roc_panic for numeric errors #6062

Merged
merged 17 commits into from
Dec 5, 2023

Conversation

JRMurr
Copy link
Sponsor Contributor

@JRMurr JRMurr commented Nov 22, 2023

fixes #6027

fyi I dont really know zig so please let me know if i did something wrong

The issue

calls to @panic in the zig bytecode generator would not bubble up the rest of the compiler so we would get nonsene numbers back instead of raise the error to the roc code that should have errored. For example 1dec / 0 would give a giant number in wasm and 0 in llvm

What I did

updated all the numeric related calls to @panic to roc_panic. TBH not sure on the difference between the two but added some tests and they only fail when expected when using roc_panic

Some errors I'm seeing

I posted some info in zulip

TLDR we still show "normal" output when a panic or call to crash happens.

I could see the argument that its nice to display the type signature still but maybe we hid the value for crashes?

Misc changes

added node and zls to the dev shell since npx is needed for the site dev and zls is nice when editing zig.

flake.nix Outdated Show resolved Hide resolved
@JRMurr
Copy link
Sponsor Contributor Author

JRMurr commented Nov 23, 2023

looking into failures. The apple-silicon failure looks like #5932 so possibly a flake?

The valgrind error i'm having trouble reproducing locally. will investigate

Comment on lines +5 to 15
extern fn roc_panic(msg: *const RocStr, tag_id: u32) callconv(.C) noreturn;

pub fn panic_help(msg: []const u8, tag_id: u32) void {
pub fn panic_help(msg: []const u8, tag_id: u32) noreturn {
var str = RocStr.init(msg.ptr, msg.len);
roc_panic(&str, tag_id);
}

// must export this explicitly because right now it is not used from zig code
pub fn panic(msg: *const RocStr, alignment: u32) callconv(.C) void {
pub fn panic(msg: *const RocStr, alignment: u32) callconv(.C) noreturn {
return roc_panic(msg, alignment);
}
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was having a weird error in CI were valgrind was crashing but was not happening locally

My gut feeling is that us adding unreachable after a roc_panic call is whats causing the issue.

Looks like unreachable is undefined behavior when compiled with RelaseFast https://ziglang.org/documentation/master/#try which i think we do

This return type lets you do

if (some_cond) {
    roc_panic(...)
} else {
    return foo
}

without zig type errors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So glad this worked. Thanks for the find

@@ -632,7 +629,7 @@ fn mul_and_decimalize(a: u128, b: u128) i128 {
const d = answer[0];

if (overflowed == 1) {
@panic("TODO runtime exception for overflow!");
roc_panic("Decimal multiplication overflow22!", 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
roc_panic("Decimal multiplication overflow22!", 0);
roc_panic("Decimal multiplication overflow!", 0);

@@ -1208,7 +1205,7 @@ pub fn fromF64C(arg: f64) callconv(.C) i128 {
if (@call(.always_inline, RocDec.fromF64, .{arg})) |dec| {
return dec.num;
} else {
@panic("TODO runtime exception failing convert f64 to RocDec");
roc_panic("Decimal conversion from f64!", 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
roc_panic("Decimal conversion from f64!", 0);
roc_panic("Decimal conversion from f64 failed!", 0);

@@ -1217,7 +1214,7 @@ pub fn fromF32C(arg_f32: f32) callconv(.C) i128 {
if (@call(.always_inline, RocDec.fromF64, .{arg_f64})) |dec| {
return dec.num;
} else {
@panic("TODO runtime exception failing convert f64 to RocDec");
roc_panic("Decimal conversion from f32!", 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
roc_panic("Decimal conversion from f32!", 0);
roc_panic("Decimal conversion from f32 failed!", 0);

@@ -1232,7 +1229,7 @@ pub fn exportFromInt(comptime T: type, comptime name: []const u8) void {

const answer = @mulWithOverflow(this, RocDec.one_point_zero_i128);
if (answer[1] == 1) {
@panic("TODO runtime exception failing convert integer to RocDec");
roc_panic("Decimal conversion from integer!", 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
roc_panic("Decimal conversion from integer!", 0);
roc_panic("Decimal conversion from integer failed!", 0);

@@ -233,7 +233,9 @@ pub fn exportCeiling(comptime F: type, comptime T: type, comptime name: []const
pub fn exportDivCeil(comptime T: type, comptime name: []const u8) void {
comptime var f = struct {
fn func(a: T, b: T) callconv(.C) T {
return math.divCeil(T, a, b) catch @panic("TODO runtime exception for dividing by 0!");
return math.divCeil(T, a, b) catch {
roc_panic("integer division by 0!", 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be worth trying to convert these all back to Integer. Probably will require update some tests. I assume most in cargo test-gen-llvm

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ill give that a shot

Copy link
Sponsor Contributor Author

@JRMurr JRMurr Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a bunch of find replace and made all the panics i found capitalize Integer

Comment on lines +5 to 15
extern fn roc_panic(msg: *const RocStr, tag_id: u32) callconv(.C) noreturn;

pub fn panic_help(msg: []const u8, tag_id: u32) void {
pub fn panic_help(msg: []const u8, tag_id: u32) noreturn {
var str = RocStr.init(msg.ptr, msg.len);
roc_panic(&str, tag_id);
}

// must export this explicitly because right now it is not used from zig code
pub fn panic(msg: *const RocStr, alignment: u32) callconv(.C) void {
pub fn panic(msg: *const RocStr, alignment: u32) callconv(.C) noreturn {
return roc_panic(msg, alignment);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So glad this worked. Thanks for the find

@@ -1,4 +1,4 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you have a number of unrelated changes in this file and below. Probably don't want those here?

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh yea this file is not related to this branch. I can remove

the readme/gitignore updates were useful when i was testing the wasm repl so i figured those were fine to leave in but can move out

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still a bit confused about the random repl and flake changes, but otherwise, this pr looks great.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs here were out of date, linking to the public dir did not seem to hot reload for me

@bhansconnect
Copy link
Contributor

Do to some ci testing changes, I think this needs rebased or have main merged then tests should run fine and it should be good to merge.

@JRMurr
Copy link
Sponsor Contributor Author

JRMurr commented Dec 4, 2023

@bhansconnect you ok with merging this with the doc changes or do you want me to pull those out?

@bhansconnect bhansconnect merged commit e336aa5 into roc-lang:main Dec 5, 2023
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Num.div by 0 on Dec should crash, but doesn't
5 participants