From 8b901f389e20b6a2d9133a23609d5d578c467d77 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 17:25:57 +0000 Subject: [PATCH 1/2] Qualify explicit object member functions converted to function pointers --- cpp2rust/converter/converter.cpp | 12 ++++++---- .../expr.unary/expr.unary.op.cpp | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 5ce8d433f4..18d7e0d9df 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2010,10 +2010,14 @@ void Converter::EmitFnPtrCall(clang::CallExpr *expr) { } std::string Converter::GetFunctionRefName(const clang::FunctionDecl *fn_decl) { - if (auto *method = clang::dyn_cast(fn_decl); - method && method->isStatic()) { - return std::format("{}::{}", GetRecordName(method->getParent()), - GetMethodName(method)); + if (auto *method = clang::dyn_cast(fn_decl)) { + if (method->isStatic()) { + return std::format("{}::{}", GetRecordName(method->getParent()), + GetMethodName(method)); + } + if (method->isExplicitObjectMemberFunction()) { + return std::format("{}::{}", GetUFCSName(method), GetMethodName(method)); + } } return Mapper::MapFunctionName(ctx_, fn_decl); } diff --git a/tests/std/expr/expr.compound/expr.unary/expr.unary.op.cpp b/tests/std/expr/expr.compound/expr.unary/expr.unary.op.cpp index e4bbc2727d..07cb22985d 100644 --- a/tests/std/expr/expr.compound/expr.unary/expr.unary.op.cpp +++ b/tests/std/expr/expr.compound/expr.unary/expr.unary.op.cpp @@ -22,6 +22,28 @@ void test() { } } // namespace p3_2_static_member_is_ordinary_pointer +// N4950 [expr.unary.op]/3.2: "Otherwise, the result has type "pointer to T" and +// points to the designated object (6.7.1) or function (6.8.4). If the operand +// names an explicit object member function (9.3.4.6), the operand shall be a +// qualified-id." +namespace p3_2_explicit_object_member_is_ordinary_pointer { +struct HasExplicitObjectMember { + int base; + int add(this const HasExplicitObjectMember &self, int amount) { + return self.base + amount; + } +}; + +void test() { + int (*function_pointer)(const HasExplicitObjectMember &, int) = + &HasExplicitObjectMember::add; + HasExplicitObjectMember object{3}; + assert(function_pointer(object, 4) == 7); + object.base = 5; + assert(function_pointer(object, 1) == 6); +} +} // namespace p3_2_explicit_object_member_is_ordinary_pointer + // N4950 [expr.unary.op]/4: a pointer to member is only formed when an explicit // & is used and its operand is a qualified-id not enclosed in parentheses. namespace p4_parenthesized_qualified_id_is_object_pointer { @@ -49,6 +71,7 @@ void test() { int main() { p3_2_static_member_is_ordinary_pointer::test(); + p3_2_explicit_object_member_is_ordinary_pointer::test(); p4_parenthesized_qualified_id_is_object_pointer::test(); return 0; } From fd59fdb231c42c6ce086ffe2eb315520d7cd4303 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 18 Nov 2026 17:52:23 +0000 Subject: [PATCH 2/2] Update tests --- .../expr.unary/out/refcount/expr.unary.op.rs | 35 ++++++++++++++++++- .../expr.unary/out/unsafe/expr.unary.op.rs | 29 ++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.unary.op.rs b/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.unary.op.rs index b6409f9ba9..8b04d3a4d1 100644 --- a/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.unary.op.rs +++ b/tests/std/expr/expr.compound/expr.unary/out/refcount/expr.unary.op.rs @@ -32,11 +32,27 @@ pub fn test_2() { } #[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] #[byte_size(4)] +pub struct p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember { + #[offset(0)] + pub base: i32, +} +pub fn test_3() { + let mut function_pointer : FnPtr ,i32 ,) -> i32 > = ( FnPtr:: ,i32 ,) -> i32 >::new( as p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMemberImpl>::add) ) ; + let object: Value = + Rc::new(RefCell::new( + p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember { base: 3 }, + )); + assert!((({ function_pointer.call(object.as_pointer(), 4,) }) == 7)); + (*object.borrow_mut()).base = 5; + assert!((({ function_pointer.call(object.as_pointer(), 1,) }) == 6)); +} +#[derive(Copy, Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(4)] pub struct p4_parenthesized_qualified_id_is_object_pointer_TakesAddressOfOwnMember { #[offset(0)] pub value: i32, } -pub fn test_3() { +pub fn test_4() { let first: Value = Rc::new(RefCell::new( p4_parenthesized_qualified_id_is_object_pointer_TakesAddressOfOwnMember { value: 3 }, @@ -71,8 +87,25 @@ pub fn main() { fn main_0() -> i32 { ({ test_2() }); ({ test_3() }); + ({ test_4() }); return 0; } +pub trait p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMemberImpl { + fn add( + self_: Ptr, + amount: i32, + ) -> i32; +} +impl p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMemberImpl + for Ptr +{ + fn add( + self_: Ptr, + mut amount: i32, + ) -> i32 { + return ({ self_.with(|__s| __s.base) } + { amount }); + } +} pub trait p4_parenthesized_qualified_id_is_object_pointer_TakesAddressOfOwnMemberImpl { fn parenthesized(&self) -> Ptr; fn unqualified(&self) -> Ptr; diff --git a/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.unary.op.rs b/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.unary.op.rs index 004d0b1c2c..89acdae101 100644 --- a/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.unary.op.rs +++ b/tests/std/expr/expr.compound/expr.unary/out/unsafe/expr.unary.op.rs @@ -29,6 +29,32 @@ pub unsafe fn test_2() { } #[repr(C)] #[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember { + pub base: i32, +} +impl p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember { + pub unsafe fn add( + self_: *const p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember, + mut amount: i32, + ) -> i32 { + return (((*self_).base) + (amount)); + } +} +pub unsafe fn test_3() { + let mut function_pointer: Option< + unsafe fn( + *const p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember, + i32, + ) -> i32, + > = (Some(p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember::add)); + let mut object: p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember = + p3_2_explicit_object_member_is_ordinary_pointer_HasExplicitObjectMember { base: 3 }; + assert!(((unsafe { (function_pointer).unwrap()(&object, 4,) }) == (7))); + object.base = 5; + assert!(((unsafe { (function_pointer).unwrap()(&object, 1,) }) == (6))); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] pub struct p4_parenthesized_qualified_id_is_object_pointer_TakesAddressOfOwnMember { pub value: i32, } @@ -40,7 +66,7 @@ impl p4_parenthesized_qualified_id_is_object_pointer_TakesAddressOfOwnMember { return (&mut self.value as *mut i32); } } -pub unsafe fn test_3() { +pub unsafe fn test_4() { let mut first: p4_parenthesized_qualified_id_is_object_pointer_TakesAddressOfOwnMember = p4_parenthesized_qualified_id_is_object_pointer_TakesAddressOfOwnMember { value: 3 }; let mut second: p4_parenthesized_qualified_id_is_object_pointer_TakesAddressOfOwnMember = @@ -73,6 +99,7 @@ pub fn main() { unsafe fn main_0() -> i32 { (unsafe { test_2() }); (unsafe { test_3() }); + (unsafe { test_4() }); return 0; } pub unsafe fn __cpp2rust_init_globals() {