Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions cc_bindings_from_rs/generate_bindings/format_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,15 @@ pub fn format_ty_for_cc<'tcx>(

let specialization = db.parse_rs_std_template_specialization(ty);
if specialization.as_ref().is_some_and(|specialization| {
specialization.is_err()
|| specialization.as_ref().is_ok_and(|rs_std_enum| {
(matches!(rs_std_enum.kind, TemplateSpecializationKind::Option { .. })
&& !location.is_bridgeable())
|| matches!(rs_std_enum.kind, TemplateSpecializationKind::Result { .. })
})
// We only want to consider errors when bridging could not occur.
// Otherwise, fallthrough to the normal bridging logic.
let error_occurred = !location.is_bridgeable() && specialization.is_err();
let is_option_or_result = specialization.as_ref().is_ok_and(|rs_std_enum| {
!location.is_bridgeable()
&& matches!(rs_std_enum.kind, TemplateSpecializationKind::Option { .. })
|| matches!(rs_std_enum.kind, TemplateSpecializationKind::Result { .. })
});
error_occurred || is_option_or_result
}) {
let rs_std_enum = specialization.unwrap()?;
let tokens = rs_std_enum.core.self_ty_cc.clone().into_tokens(&mut prereqs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ fn test_format_bridged_type_in_generic_types() {
let err = result.unwrap_err();
assert_eq!(
err,
"Error handling parameter #0 of type `std::option::Option<std::boxed::Box<std::result::Result<RustType, ()>>>`: Generic types are not supported yet (b/259749095)"
"Error handling parameter #0 of type `std::option::Option<std::boxed::Box<std::result::Result<RustType, ()>>>`: \
Failed to construct CrubitAbiType for std::boxed::Box<std::result::Result<RustType, ()>> because it does not have a move ctor or assignment operator."
);
});
}
Expand Down
9 changes: 9 additions & 0 deletions cc_bindings_from_rs/test/enums/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,12 @@ pub struct OptionWithSizeTypes {
pub uval: Option<usize>,
pub ival: Option<isize>,
}

#[doc = "CRUBIT_ANNOTATE: cpp_type=int"]
#[doc = "CRUBIT_ANNOTATE: include_path=unused_include.h"]
#[repr(transparent)]
pub struct BridgedType(i32);

pub fn take_option_bridged(x: Option<BridgedType>) -> i32 {
x.map(|b| b.0).unwrap_or(-1)
}
14 changes: 14 additions & 0 deletions cc_bindings_from_rs/test/enums/option_cc_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@

namespace option {
struct HasOptions;

// Error generating bindings for `option_golden::BridgedType` defined at
// cc_bindings_from_rs/test/enums/option.rs;l=178:
// Type bindings for option_golden::BridgedType suppressed due to being mapped
// to an existing C++ type (int)

// Generated from:
// cc_bindings_from_rs/test/enums/option.rs;l=120
struct CRUBIT_INTERNAL_RUST_TYPE(":: option_golden :: CloneNoDefault") alignas(
Expand Down Expand Up @@ -297,6 +303,14 @@ OptionWithSizeTypes final {
// cc_bindings_from_rs/test/enums/option.rs;l=158:
// Zero-sized types (ZSTs) are not supported (b/258259459)

// Error generating bindings for `option_golden::take_option_bridged` defined at
// cc_bindings_from_rs/test/enums/option.rs;l=180:
// Error handling parameter #0 of type
// `std::option::Option<option_golden::BridgedType>`: Failed to format type for
// the definition of `option_golden::BridgedType`: Error formatting the
// fully-qualified C++ name of `BridgedType`: `int` is a C++ reserved keyword
// and can't be used as a C++ identifier

} // namespace option

#ifndef _CRUBIT_BINDINGS_FOR_rs_ustd_x00000020_x0000003a_x0000003a_x00000020Option_x00000020_x0000003c_x00000020_x0000003a_x0000003a_x00000020option_x00000020_x0000003a_x0000003a_x00000020CloneNoDefault_x00000020_x0000003e
Expand Down
Loading