Skip to main content

collect_diagnostic

Function collect_diagnostic 

Source
pub fn collect_diagnostic<R>(f: impl FnOnce() -> R) -> Result<R, Diagnostic>
Expand description

Runs f with a diagnostic sink installed.

An error that would normally print a report and exit/panic instead records a Diagnostic and unwinds back here. Nested calls are supported (the previous state is restored), and panics that signal bugs are not swallowed; they propagate through unchanged.

§Requires

An unwinding panic strategy (panic = "unwind"). This function catches the internal unwind used to carry a diagnostic out of the error funnels via std::panic::catch_unwind. Under panic = "abort" that unwind cannot be caught: the process aborts on the first error instead of this returning Err. Every profile candela ships unwinds, [profile.release] included, because candela build recovers this way too; an embedder that picks its own profile has to keep panic = "unwind" to use this.

While a collection is active this installs a process-global panic hook that silences only the internal diagnostic unwind and forwards every other panic to the host’s own hook. The host’s hook is captured on entry and restored once the outermost collection finishes, so it is never permanently replaced.

§Errors

Returns the Diagnostic recorded by the first error funnel that fired while f was running (parser, compiler or runtime).