From a1ff445b5fc406aa825c029ad295e6bbfba5ef86 Mon Sep 17 00:00:00 2001 From: Devin Jeanpierre Date: Wed, 11 Mar 2026 12:01:29 -0700 Subject: [PATCH] Allow aliases, especially string_view, in overloaded operators. PiperOrigin-RevId: 882127803 --- .../generate_bindings/generate_function.rs | 19 ++++++++++++------- .../test/struct/operators/foreign_lib.h | 1 + .../test/struct/operators/operators.h | 8 ++++++++ .../test/struct/operators/operators_test.rs | 8 ++++++++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/rs_bindings_from_cc/generate_bindings/generate_function.rs b/rs_bindings_from_cc/generate_bindings/generate_function.rs index 35914bd66..ba237e64a 100644 --- a/rs_bindings_from_cc/generate_bindings/generate_function.rs +++ b/rs_bindings_from_cc/generate_bindings/generate_function.rs @@ -236,17 +236,22 @@ fn type_by_value_or_under_const_ref<'a>( errors.add(anyhow!("Expected {value_desc} reference to be immutable, but found mutable reference: {kind_string}")); *mutability = Mutability::Const; } - if let RsTypeKind::Record { record: lhs_record, .. } = &**lhs { + if let RsTypeKind::Record { record: lhs_record, .. } = lhs.unalias() { Ok((lhs, lhs_record)) } else { bail_to_errors!(errors, "Expected {value_desc} to be a record or a const reference to a record, found a reference that doesn't refer to a record: {kind_string}"); } } - RsTypeKind::Record { record: ref lhs_record, .. } => Ok((kind, lhs_record)), - _ => bail_to_errors!( - errors, - "Expected {value_desc} to be a record or const reference to a record, found {kind_string}" - ), + ref other => { + if let RsTypeKind::Record { record: lhs_record, .. } = other.unalias() { + Ok((kind, lhs_record)) + } else { + bail_to_errors!( + errors, + "Expected {value_desc} to be a record or const reference to a record, found {kind_string}" + ) + } + } } } @@ -1040,7 +1045,7 @@ fn adjust_param_types_for_trait_impl( .iter_mut() .enumerate() .map(|(i, param_type)| { - let RsTypeKind::Record { record: param_record, .. } = ¶m_type else { + let RsTypeKind::Record { record: param_record, .. } = param_type.unalias() else { return Default::default(); }; if !is_record_clonable(db, param_record.clone()) { diff --git a/rs_bindings_from_cc/test/struct/operators/foreign_lib.h b/rs_bindings_from_cc/test/struct/operators/foreign_lib.h index 9830f657b..b5f76aae8 100644 --- a/rs_bindings_from_cc/test/struct/operators/foreign_lib.h +++ b/rs_bindings_from_cc/test/struct/operators/foreign_lib.h @@ -7,6 +7,7 @@ namespace foreign { struct ForeignType {}; +using Alias = ForeignType; } // namespace foreign #endif // CRUBIT_RS_BINDINGS_FROM_CC_TEST_STRUCT_OPERATORS_FOREIGN_LIB_H_ diff --git a/rs_bindings_from_cc/test/struct/operators/operators.h b/rs_bindings_from_cc/test/struct/operators/operators.h index 7cb7f76dd..01435a5a5 100644 --- a/rs_bindings_from_cc/test/struct/operators/operators.h +++ b/rs_bindings_from_cc/test/struct/operators/operators.h @@ -233,4 +233,12 @@ struct OperandForeign {}; inline bool operator==(foreign::ForeignType, OperandForeign) { return true; } inline bool operator==(OperandForeign, foreign::ForeignType) { return true; } +struct OperandAlias {}; +inline bool operator==(const foreign::Alias, const OperandAlias&) { + return true; +} +inline bool operator==(const OperandAlias&, const foreign::Alias) { + return true; +} + #endif // CRUBIT_RS_BINDINGS_FROM_CC_TEST_STRUCT_OPERATORS_OPERATORS_H_ diff --git a/rs_bindings_from_cc/test/struct/operators/operators_test.rs b/rs_bindings_from_cc/test/struct/operators/operators_test.rs index feb347aab..a81328c02 100644 --- a/rs_bindings_from_cc/test/struct/operators/operators_test.rs +++ b/rs_bindings_from_cc/test/struct/operators/operators_test.rs @@ -323,3 +323,11 @@ fn test_foreign_eq() { assert!(s1 == s2); assert!(s2 == s1); } + +#[gtest] +fn test_alias_eq() { + let s1 = OperandAlias::default(); + let s2 = foreign_lib::foreign::ForeignType::default(); + assert!(s1 == s2); + assert!(s2 == s1); +}