Fix assert failure: only require that the desugared type is a function.

This fixes the following assert failure:

```
F0821 15:29:35.857368    4034 logging.cc:62] assert.h assertion failed at third_party/crubit/lifetime_annotations/type_lifetimes.cc:560 in const FunctionLifetimes &clang::tidy::lifetimes::ValueLifetimes::GetFuncLifetimes() const: clang::isa<clang::FunctionProtoType>(type_)
*** Check failure stack trace: ***
    @     0x55aaebec8bf4  absl::log_internal::LogMessage::SendToLog()
    @     0x55aaebec8950  absl::log_internal::LogMessage::Flush()
    @     0x55aaebec90b9  absl::log_internal::LogMessageFatal::~LogMessageFatal()
    @     0x55aaebe9a394  __assert_fail
    @     0x55aae9e930b8  clang::tidy::lifetimes::ValueLifetimes::GetFuncLifetimes()
    @     0x55aae9e555ba  crubit::Importer::ConvertUnattributedType()
    @     0x55aae9e541a6  crubit::Importer::ConvertType()
    @     0x55aae9e57440  crubit::Importer::ConvertQualType()
    @     0x55aae9e7ffc0  crubit::FunctionDeclImporter::Import()
    @     0x55aae9e824b1  crubit::DeclImporterBase<>::ImportDecl()
    @     0x55aae9e4ff87  crubit::Importer::GetDeclItem()
    @     0x55aae9e4f6ad  crubit::Importer::GetItemIdsInSourceOrder()
    @     0x55aae9e76ac0  crubit::CXXRecordDeclImporter::Import()
    @     0x55aae9e792a1  crubit::DeclImporterBase<>::ImportDecl()
    @     0x55aae9e4ff87  crubit::Importer::GetDeclItem()
    @     0x55aae9e5166c  crubit::Importer::ImportDeclsFromDeclContext()
    @     0x55aae9e50f27  crubit::Importer::Import()
    @     0x55aae9e4c7c4  crubit::AstConsumer::HandleTranslationUnit()
    @     0x55aaea15f396  clang::ParseAST()
    @     0x55aae9f402c4  clang::FrontendAction::Execute()
    @     0x55aae9eaff2f  clang::CompilerInstance::ExecuteAction()
    @     0x55aae9e9cb7b  clang::tooling::FrontendActionFactory::runInvocation()
    @     0x55aae9e9ca4c  clang::tooling::ToolInvocation::runInvocation()
    @     0x55aae9e9c024  clang::tooling::ToolInvocation::run()
    @     0x55aae9e9b808  clang::tooling::runToolOnCodeWithArgs()
    @     0x55aae9e9b4df  clang::tooling::runToolOnCodeWithArgs()
    @     0x55aae9e48dc5  crubit::IrFromCc()
    @     0x55aae9e18802  crubit::GenerateBindingsAndMetadata()
    @     0x55aae9e14096  crubit::Main()
    @     0x55aae9e14be5  main
    @     0x7fb4c65f93d4  __libc_start_main
    @     0x55aae9e135ea  _start
*** SIGABRT received by PID 4034 (TID 4034) on cpu 204 from PID 4034; ***
```

When we're getting the lifetime, the access here is guarded by:

```c++
    if (const auto* func_type =
            pointee_type->getAs<clang::FunctionProtoType>()) {
```

Which is not the same as `isa<clang::FunctionProtoType>()`! I'm really not following the logic, but certainly it seems like both of these asserts should line up, and removing sugar seems as decent a thing to do as anything. Nothing else fails if we do.

PiperOrigin-RevId: 666223224
Change-Id: I43c1298bbf316180b67c1751f81282b453ea98bb
1 file changed
tree: be65c0eee9343b79c090531b5f75cb7fbcd7b423
  1. .bazelci/
  2. bazel/
  3. cc_bindings_from_rs/
  4. common/
  5. docs/
  6. examples/
  7. features/
  8. lifetime_analysis/
  9. lifetime_annotations/
  10. migrator/
  11. nullability/
  12. rs_bindings_from_cc/
  13. support/
  14. .bazelrc
  15. .gitignore
  16. BUILD
  17. Cargo.lock
  18. CODE_OF_CONDUCT
  19. CONTRIBUTING
  20. LICENSE
  21. MODULE.bazel
  22. README.md
  23. WORKSPACE.bzlmod
README.md

Crubit: C++/Rust Bidirectional Interop Tool

Build status

NOTE: Crubit currently expects deep integration with the build system, and is difficult to deploy to environments dissimilar to Google's monorepo. We do not have our tooling set up to accept external contributions at this time.

Crubit is a bidirectional bindings generator for C++ and Rust, with the goal of integrating the C++ and Rust ecosystems.

Status

Support for calling FFI-friendly C++ from Rust is in progress.

Support for calling Rust from C++ will arrive in 2024H2.

Example

Consider the following C++ function:

extern "C" bool IsGreater(int lhs, int rhs);

This function, if present in a header file which is processed by Crubit, becomes callable from Rust as if it were defined as:

pub fn IsGreater(lhs: ffi::c_int, rhs: ffi::c_int) -> bool {...}

Note: There are some temporary restrictions on the API shape. For example, functions that are not extern "C", or that accept a type like std::string, can't be called from Rust directly via Crubit. These restrictions will be relaxed over time.

Getting Started

Here are some resources for getting started with Crubit:

  • Rust Bindings for C++ Libraries is a detailed walkthrough on how to use C++ from Rust using Crubit.

  • The examples/cpp/ directory has copy-pastable examples of calling C++ from Rust, together with snapshots of what the generated Rust interface looks like.

Building Crubit

$ apt install clang lld bazel
$ git clone git@github.com:google/crubit.git
$ cd crubit
$ bazel build --linkopt=-fuse-ld=/usr/bin/ld.lld //rs_bindings_from_cc:rs_bindings_from_cc_impl

Using a prebuilt LLVM tree

$ git clone https://github.com/llvm/llvm-project
$ cd llvm-project
$ CC=clang CXX=clang++ cmake -S llvm -B build -DLLVM_ENABLE_PROJECTS='clang' -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install
$ cmake --build build -j
$ # wait...
$ cmake --install build
$ cd ../crubit
$ LLVM_INSTALL_PATH=../llvm-project/install bazel build //rs_bindings_from_cc:rs_bindings_from_cc_impl