Skip to main content

Module error

Module error 

Source
Expand description

Error handling helpers for R API calls.

§User-facing path: tagged condition SEXP

Every #[miniextendr] function runs inside with_r_unwind_protect. Rust panics and user-raised conditions (error!(), warning!(), message!(), condition!()) are caught, packaged as a tagged SEXP, and returned normally. The generated R wrapper inspects the SEXP and raises the appropriate R condition with rust_* class layering. No Rf_error longjmp happens on this path.

User code should use:

  • panic!() — for unrecoverable Rust errors (becomes rust_error in R)
  • error!() / warning!() / message!() / condition!() — for structured R conditions (see crate::condition)

See crate::error_value for the tagged-SEXP layout, the error_in_r default + no_error_in_r / unwrap_in_r opt-outs, and the PROTECT-discipline gotcha that R-devel surfaces.

§When Rf_error still fires (framework-internal)

Rf_error (longjmp via r_stop) survives only at FFI guard sites where there is no SEXP slot to return through:

  1. ffi_guard::guarded_ffi_call(GuardMode::CatchUnwind, …) — worker thread panic conversion before the worker→main boundary returns a SEXP.
  2. trait_abi::check_arity — pre-shim arity check that runs before the vtable shim has a SEXP to return.

ALTREP RUnwind callbacks now route through with_r_unwind_protect_sourcedraise_rust_condition_via_stop, which preserves rust_* class layering without going through r_stop.

r_stop is pub(crate) — no user code should depend on it.

§Example

use miniextendr_api::miniextendr;

#[miniextendr]
fn validate_input(x: i32) -> i32 {
    assert!(x >= 0, "x must be non-negative, got {x}");
    x * 2
}

Functions§

_r_print_newline 👻
Print a newline to R’s console (internal implementation). Automatically routes to R’s main thread if called from a worker thread.
_r_print_str 👻
Print a message to R’s console (internal implementation). Automatically routes to R’s main thread if called from a worker thread.
r_stop 🔒
Raise an R error via Rf_error (longjmp). Crate-internal only.
r_warning
Raise an R warning with the given message.