diff --git a/cc_bindings_from_rs/generate_bindings/lib.rs b/cc_bindings_from_rs/generate_bindings/lib.rs index 5a0d38f2d..2b8552716 100644 --- a/cc_bindings_from_rs/generate_bindings/lib.rs +++ b/cc_bindings_from_rs/generate_bindings/lib.rs @@ -195,8 +195,8 @@ fn specializations<'tcx>(db: &crate::BindingsGenerator<'tcx>) -> Rc<[CppTypeSpec let tcx = db.tcx(); let mut specializations = Vec::new(); - let defs_in_crate = db.public_paths_by_def_id(db.source_crate_num()); - for (adt_or_alias_def_id, paths) in defs_in_crate { + let all_public_paths = db.all_public_paths_by_def_id(); + for (adt_or_alias_def_id, paths) in all_public_paths { specializations.extend( std::iter::once(adt_or_alias_def_id) .chain( diff --git a/cc_bindings_from_rs/test/bazel/cross_crate/BUILD b/cc_bindings_from_rs/test/bazel/cross_crate/BUILD index bdcf38838..ff80bb0cf 100644 --- a/cc_bindings_from_rs/test/bazel/cross_crate/BUILD +++ b/cc_bindings_from_rs/test/bazel/cross_crate/BUILD @@ -5,6 +5,7 @@ load( "@rules_rust//rust:defs.bzl", "rust_library", ) +load("//cc_bindings_from_rs/bazel_support:cc_bindings_from_rust_library_config_aspect_hint.bzl", "cc_bindings_from_rust_library_config") load( "//cc_bindings_from_rs/bazel_support:cc_bindings_from_rust_rule.bzl", "cc_bindings_from_rust", @@ -13,10 +14,27 @@ load("//common:crubit_wrapper_macros_oss.bzl", "crubit_cc_test") package(default_applicable_licenses = ["//:license"]) +cc_bindings_from_rust_library_config( + name = "other_crate_hint", + extra_cc_hdrs = ["other_crate_types.h"], +) + rust_library( name = "other_crate", testonly = 1, srcs = ["other_crate.rs"], + aspect_hints = [ + ":other_crate_hint", + ], + proc_macro_deps = [ + "//support:crubit_annotate", + ], +) + +cc_bindings_from_rust( + name = "other_crate_cc_api", + testonly = 1, + crate = ":other_crate", ) rust_library( @@ -36,6 +54,7 @@ crubit_cc_test( name = "cross_crate_test", srcs = ["cross_crate_test.cc"], deps = [ + ":other_crate_cc_api", ":test_api_cc_api", "//testing/base/public:gunit_main", ], diff --git a/cc_bindings_from_rs/test/bazel/cross_crate/cross_crate_test.cc b/cc_bindings_from_rs/test/bazel/cross_crate/cross_crate_test.cc index 4315e3a99..c7ec5e83d 100644 --- a/cc_bindings_from_rs/test/bazel/cross_crate/cross_crate_test.cc +++ b/cc_bindings_from_rs/test/bazel/cross_crate/cross_crate_test.cc @@ -6,6 +6,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "cc_bindings_from_rs/test/bazel/cross_crate/other_crate_types.h" #include "cc_bindings_from_rs/test/bazel/cross_crate/test_api.h" namespace crubit { @@ -17,6 +18,11 @@ TEST(CrossCrateTests, BasicEndToEndTest) { EXPECT_EQ(123, i); } +TEST(CrossCrateTests, SpecializationTest) { + crubit::test::MyStatus s = test_api::return_status_from_other_crate(); + EXPECT_TRUE(s.ok); +} + // b/292231336 // TEST(CrossCrateTests, RustToolchainCrate) { // ::alloc::string::String s = diff --git a/cc_bindings_from_rs/test/bazel/cross_crate/other_crate.rs b/cc_bindings_from_rs/test/bazel/cross_crate/other_crate.rs index c3f98dae7..74c160f75 100644 --- a/cc_bindings_from_rs/test/bazel/cross_crate/other_crate.rs +++ b/cc_bindings_from_rs/test/bazel/cross_crate/other_crate.rs @@ -6,3 +6,19 @@ //! `other_crate.rs` are used in public API exposed by `test_api.rs`. pub struct SomeStruct(pub i32); + +#[crubit_annotate::cpp_layout_equivalent( + cpp_type = "crubit::test::MyStatusOr<{T}>", + include_path = "cc_bindings_from_rs/test/bazel/cross_crate/other_crate_types.h" +)] +#[repr(C)] +pub struct MyStatusOr { + pub has_value: bool, + pub value: T, +} + +#[crubit_annotate::cpp_specialization( + cpp_type = "crubit::test::MyStatus", + include_path = "cc_bindings_from_rs/test/bazel/cross_crate/other_crate_types.h" +)] +pub type MyStatus = MyStatusOr<()>; diff --git a/cc_bindings_from_rs/test/bazel/cross_crate/other_crate_types.h b/cc_bindings_from_rs/test/bazel/cross_crate/other_crate_types.h new file mode 100644 index 000000000..0e6ae309e --- /dev/null +++ b/cc_bindings_from_rs/test/bazel/cross_crate/other_crate_types.h @@ -0,0 +1,24 @@ +// Part of the Crubit project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef THIRD_PARTY_CRUBIT_CC_BINDINGS_FROM_RS_TEST_BAZEL_CROSS_CRATE_OTHER_CRATE_TYPES_H_ +#define THIRD_PARTY_CRUBIT_CC_BINDINGS_FROM_RS_TEST_BAZEL_CROSS_CRATE_OTHER_CRATE_TYPES_H_ + +namespace crubit { +namespace test { + +struct MyStatus { + bool ok; +}; + +template +struct MyStatusOr { + bool has_value; + T value; +}; + +} // namespace test +} // namespace crubit + +#endif // THIRD_PARTY_CRUBIT_CC_BINDINGS_FROM_RS_TEST_BAZEL_CROSS_CRATE_OTHER_CRATE_TYPES_H_ diff --git a/cc_bindings_from_rs/test/bazel/cross_crate/test_api.rs b/cc_bindings_from_rs/test/bazel/cross_crate/test_api.rs index 6bddb890c..c40be8b9f 100644 --- a/cc_bindings_from_rs/test/bazel/cross_crate/test_api.rs +++ b/cc_bindings_from_rs/test/bazel/cross_crate/test_api.rs @@ -22,3 +22,7 @@ pub fn extract_int(s: other_crate::SomeStruct) -> i32 { pub fn return_a_type_from_a_rust_toolchain_crate() -> String { "String".to_owned() } + +pub fn return_status_from_other_crate() -> other_crate::MyStatus { + other_crate::MyStatusOr { has_value: true, value: () } +}