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

Even better model panics #1721

Merged
merged 9 commits into from
Mar 27, 2023
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions crates/fj-proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ pub fn model(_: TokenStream, input: TokenStream) -> TokenStream {
),
);

let attrs = item.attrs;
let vis = item.vis;
let sig = item.sig;
let statements = item.block.stmts;

let item = quote::quote! {
#(#attrs)* #vis #sig {
fj::abi::initialize_panic_handling();
#(#statements)*
}
};

Copy link
Owner

Choose a reason for hiding this comment

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

You seem to be adding the new and improved code here, without removing the old and busted one. initialize_panic_handling says it can be called multiple times, but I think that's not enough reason to keep the old code around 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I Had removed the old version and that println but for some reason merging brought a lot of the old stuff back. I missed those two.
I'm going to have to give this re-basing thing a try.

I'll try to fix that up today.

let tokens = quote::quote! {
#item
#init
Expand Down
1 change: 1 addition & 0 deletions crates/fj/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ anyhow = "1.0.70"

[dependencies]
fj-proc.workspace = true
backtrace = "0.3.67"

[dependencies.serde]
version = "1.0.158"
Expand Down
11 changes: 10 additions & 1 deletion crates/fj/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod host;
mod metadata;
mod model;

use backtrace::Backtrace;
use std::{any::Any, fmt::Display, panic, sync::Mutex};

pub use self::{
Expand Down Expand Up @@ -122,6 +123,7 @@ pub const INIT_FUNCTION_NAME: &str = "fj_model_init";
struct PanicInfo {
message: Option<String>,
location: Option<Location>,
backtrace: Backtrace,
}

impl Display for PanicInfo {
Expand All @@ -139,6 +141,8 @@ impl Display for PanicInfo {
write!(f, "no location given")?;
}

writeln!(f, "\nBacktrace:\n{:?}", self.backtrace)?;

Ok(())
}
}
Expand Down Expand Up @@ -180,7 +184,12 @@ pub fn initialize_panic_handling() {
column: location.column(),
});

*last_panic = Some(PanicInfo { message, location });
let backtrace = backtrace::Backtrace::new();
*last_panic = Some(PanicInfo {
message,
location,
backtrace,
});
}));
}

Expand Down