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 exception handling to use std::exception_ptr #1180

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Commits on Feb 24, 2023

  1. Add CxxException to wrap a C++ exception

    The exception object can be passed as a pointer via `CxxException`
    error through Rust frames.
    
    A method to create the default message-based `rust::Error` and to drop
    and clone the exception are provided.
    schreter committed Feb 24, 2023
    Configuration menu
    Copy the full SHA
    ffd3e4c View commit details
    Browse the repository at this point in the history
  2. Add CxxExceptionPtr atom

    This represents C++ `std::exception_ptr` on the Rust side via
    `CxxException` object, which can be mentioned as a return type for
    functions creating custom exceptions or evaluating exceptions caught
    from C++ (added in further commits).
    schreter committed Feb 24, 2023
    Configuration menu
    Copy the full SHA
    73eeef3 View commit details
    Browse the repository at this point in the history
  3. Update exception handling to use std::exception_ptr

    This makes the interface between C++ exception handling and Rust
    `Result` cleaner and allows passing C++ exception from the inner C++
    call to the outer C++ call via Rust's `CxxException` unmodified, i.e.,
    without losing information.
    
    To allow custom exception types, trait `ToCxxException` was introduced.
    With this trait, it's possible to convert a Rust error to the wrapper
    `CxxExeception` via a C++ function, thus allowing throwing other
    exceptions as well. This required changing `r#try` function into
    macros, so we can properly default to `rust::Error` for errors not
    having `ToCxxException` defined.
    
    Background: The `throw` statement in C++ (__cxa_throw) effectively
    first allocates space on the heap and creates the exception within,
    then starts unwinding. This can be also done via standard C++11 API in
    two steps. First, `std::make_exception_ptr()` creates a new
    `std::exception_ptr`, which points into this allocated space and
    internally is just a pointer (it's a smart pointer much like
    `std::shared_ptr`). Then, `std::rethrow_exception()` can be used to
    actually throw and/or rethrow this exception.
    
    Basically, the new implementation now uses `std::make_exception_ptr()`
    called from Rust to construct an exception for the `Result<_, E>` and
    then after returning it back to C++ via `CxxResult` (which is now BTW
    smaller, just 8B) the C++ part throws it using
    `std::rethrow_exception()`.
    schreter committed Feb 24, 2023
    Configuration menu
    Copy the full SHA
    b11a622 View commit details
    Browse the repository at this point in the history

Commits on Feb 25, 2023

  1. Fix a warning on no_std and the expected output for Result

    Since invalid `Result<T>` output changed, the expected file had to be
    updated.
    
    The output actually got better, since now the location of the actual
    declaration of the offending struct is also shown, not only the usage.
    schreter committed Feb 25, 2023
    Configuration menu
    Copy the full SHA
    e7b7116 View commit details
    Browse the repository at this point in the history
  2. Fix handling of MSVC std::exception_ptr

    Contrary to the most platforms, which just store a single pointer in
    `std::exception_ptr`, MSVC stores two. Add necessary code to handle
    this.
    schreter committed Feb 25, 2023
    Configuration menu
    Copy the full SHA
    b7bb463 View commit details
    Browse the repository at this point in the history
  3. Fix: pick up changed tests.h/cc to rebuild bridge

    Previously, modifying tests.h/cc would not trigger rebuild of the
    bridge, effectively preventing test development.
    schreter committed Feb 25, 2023
    Configuration menu
    Copy the full SHA
    c45ce05 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    7528dde View commit details
    Browse the repository at this point in the history
  5. Add NUL character at the end of copied error message

    C++ error message returned by `what` must be NUL-terminated. However,
    the current copy function only copied the characters, but didn't add
    the NUL. Allocate one more byte and set it to NUL.
    schreter committed Feb 25, 2023
    Configuration menu
    Copy the full SHA
    2805a91 View commit details
    Browse the repository at this point in the history
  6. Disable the ui test result_no_display due to a bug in trybuild cr…

    …ate.
    
    The normalization in `trybuild` crate doesn't work in the CI (but works
    fine locally).
    schreter committed Feb 25, 2023
    Configuration menu
    Copy the full SHA
    3714d23 View commit details
    Browse the repository at this point in the history

Commits on Feb 28, 2023

  1. Remove an unneeded exported macro

    Instead, expand the match directly in `expand.rs` to minimize the
    public API.
    schreter committed Feb 28, 2023
    Configuration menu
    Copy the full SHA
    380b9ef View commit details
    Browse the repository at this point in the history

Commits on Mar 5, 2023

  1. Configuration menu
    Copy the full SHA
    15225fb View commit details
    Browse the repository at this point in the history