From 95d89b714e2e1fe98ae2c9dff01b60ac309f3006 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 12:58:43 +0000 Subject: [PATCH 1/4] Key function template specializations by their mangled name in the cross-file dedup --- cpp2rust/converter/converter_lib.cpp | 4 ++++ .../basic/basic.def.odr/p15-function-template/a.cpp | 10 +++++++--- .../basic/basic.def.odr/p15-function-template/b.cpp | 5 +++++ .../std/basic/basic.def.odr/p15-function-template/h.h | 2 ++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 8499531211..c1d37cb829 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -1039,6 +1039,10 @@ static std::string GetLexicalSpecializationID(const clang::Decl *decl) { clang::dyn_cast(decl)) { id += GetMangledName(var); } + if (const auto *fn = clang::dyn_cast(decl); + fn && fn->getTemplateSpecializationArgs()) { + id += GetMangledName(fn); + } if (const auto *self = clang::dyn_cast(decl)) { id += GetMangledTypeName(self); diff --git a/tests/std/basic/basic.def.odr/p15-function-template/a.cpp b/tests/std/basic/basic.def.odr/p15-function-template/a.cpp index 6462d0bf58..b9fd0901e6 100644 --- a/tests/std/basic/basic.def.odr/p15-function-template/a.cpp +++ b/tests/std/basic/basic.def.odr/p15-function-template/a.cpp @@ -3,16 +3,20 @@ #include "../../../support/asserts-dtor-count-at-exit.h" #include "h.h" -AssertsDtorCountAtExit check{2}; +AssertsDtorCountAtExit check{4}; int main() { assert(LifetimeTracked::ctorCount == 0); + struct S {}; + assert(count() == 1); + assert(b_count_local() == 1); + assert(count() == 2); assert(count() == 1); assert(b_count_int() == 2); assert(count() == 3); - assert(LifetimeTracked::ctorCount == 1); + assert(LifetimeTracked::ctorCount == 3); assert(count() == 1); assert(b_count_long() == 2); - assert(LifetimeTracked::ctorCount == 2); + assert(LifetimeTracked::ctorCount == 4); return 0; } diff --git a/tests/std/basic/basic.def.odr/p15-function-template/b.cpp b/tests/std/basic/basic.def.odr/p15-function-template/b.cpp index 1068ec9648..51c775fd42 100644 --- a/tests/std/basic/basic.def.odr/p15-function-template/b.cpp +++ b/tests/std/basic/basic.def.odr/p15-function-template/b.cpp @@ -3,3 +3,8 @@ int b_count_int() { return count(); } int b_count_long() { return count(); } + +int b_count_local() { + struct S {}; + return count(); +} diff --git a/tests/std/basic/basic.def.odr/p15-function-template/h.h b/tests/std/basic/basic.def.odr/p15-function-template/h.h index 59a1696008..65f3305a1d 100644 --- a/tests/std/basic/basic.def.odr/p15-function-template/h.h +++ b/tests/std/basic/basic.def.odr/p15-function-template/h.h @@ -14,3 +14,5 @@ template int count() { int b_count_int(); int b_count_long(); + +int b_count_local(); From 001f12f7307fa3c300fb737433e4f4c19c24ce64 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 14:11:25 +0000 Subject: [PATCH 2/4] Update tests --- .../out/refcount/expr.prim.lambda.closure.rs | 26 ++++++++++++++++++ .../out/unsafe/expr.prim.lambda.closure.rs | 27 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.closure.rs create mode 100644 tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.closure.rs diff --git a/tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.closure.rs b/tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.closure.rs new file mode 100644 index 0000000000..c559a87fd2 --- /dev/null +++ b/tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.closure.rs @@ -0,0 +1,26 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn test_0() { + let twice: Value> = + Rc::new(RefCell::new(lambda!(Generic, {}, |mut x: i32| -> i32 { + return (x * 2); + }))); + let mut n: i32 = 4; + assert!((n == 4)); + assert!(({ (({ (*twice.borrow()).spec:: i32>(0).call(3,) }) == 6) })); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.closure.rs b/tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.closure.rs new file mode 100644 index 0000000000..c30d0f591a --- /dev/null +++ b/tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.closure.rs @@ -0,0 +1,27 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn test_0() { + let mut twice: FnPtr = lambda_unsafe!(Generic, {}, |mut x: i32| -> i32 { + return ((x) * (2)); + }); + let mut n: i32 = 4; + assert!(((n) == (4))); + assert!(((unsafe { twice.spec:: i32>(0).call(3,) }) == (6))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} From 76a69b79bcc525477d336757ef9bfc1cbdb1540a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 14:13:22 +0000 Subject: [PATCH 3/4] Update tests --- .../out/refcount/p15-function-template.rs | 52 +++++++++++++++---- .../out/unsafe/p15-function-template.rs | 46 ++++++++++++---- 2 files changed, 76 insertions(+), 22 deletions(-) diff --git a/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs b/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs index 23e85e5d19..1ec062701b 100644 --- a/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs +++ b/tests/std/basic/basic.def.odr/p15-function-template/out/refcount/p15-function-template.rs @@ -79,6 +79,9 @@ pub fn count_4() -> i32 { pub fn count_7() -> i32 { return (*n_9.with(Value::clone).borrow_mut()).prefix_inc(); } +pub fn count_10() -> i32 { + return (*n_12.with(Value::clone).borrow_mut()).prefix_inc(); +} thread_local!( static t_5: Value = Rc::new(RefCell::new(LifetimeTracked::new())); ); @@ -92,8 +95,14 @@ thread_local!( static n_9: Value = Rc::new(RefCell::new(0)); ); thread_local!( - pub static check_10: Value = - Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 2 })); + static t_11: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +thread_local!( + static n_12: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + pub static check_13: Value = + Rc::new(RefCell::new(AssertsDtorCountAtExit { expected: 4 })); ); pub fn main() { __cpp2rust_init_globals(); @@ -102,20 +111,41 @@ pub fn main() { fn main_0() -> i32 { assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0)); assert!((({ count_4() }) == 1)); - assert!((({ b_count_int_11() }) == 2)); - assert!((({ count_4() }) == 3)); - assert!((ctorCount_0.with(|rc| *rc.borrow()) == 1)); + assert!((({ b_count_local_15() }) == 1)); + assert!((({ count_4() }) == 2)); assert!((({ count_7() }) == 1)); - assert!((({ b_count_long_12() }) == 2)); - assert!((ctorCount_0.with(|rc| *rc.borrow()) == 2)); + assert!((({ b_count_int_16() }) == 2)); + assert!((({ count_7() }) == 3)); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 3)); + assert!((({ count_10() }) == 1)); + assert!((({ b_count_long_17() }) == 2)); + assert!((ctorCount_0.with(|rc| *rc.borrow()) == 4)); return 0; } -pub fn b_count_int_11() -> i32 { - return ({ count_4() }); +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct S_14 {} +pub fn count_18() -> i32 { + return (*n_20.with(Value::clone).borrow_mut()).prefix_inc(); } -pub fn b_count_long_12() -> i32 { +thread_local!( + static t_19: Value = Rc::new(RefCell::new(LifetimeTracked::new())); +); +thread_local!( + static n_20: Value = Rc::new(RefCell::new(0)); +); +pub fn b_count_int_16() -> i32 { return ({ count_7() }); } +pub fn b_count_long_17() -> i32 { + return ({ count_10() }); +} +pub fn b_count_local_15() -> i32 { + return ({ count_18() }); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct S_21 {} pub trait AssertsDtorCountAtExitImpl { fn destructor(&self); } @@ -147,5 +177,5 @@ pub fn __cpp2rust_init_globals() { let _ = copyCount_1.with(|_| ()); let _ = dtorCount_2.with(|_| ()); let _ = useCount_3.with(|_| ()); - let _ = check_10.with(|_| ()); + let _ = check_13.with(|_| ()); } diff --git a/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs b/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs index 930c85a943..b044d15810 100644 --- a/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs +++ b/tests/std/basic/basic.def.odr/p15-function-template/out/unsafe/p15-function-template.rs @@ -76,14 +76,20 @@ pub unsafe fn count_4() -> i32 { pub unsafe fn count_7() -> i32 { return (*std::cell::LazyCell::force_mut(&mut *&raw mut n_9)).prefix_inc(); } +pub unsafe fn count_10() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut n_12)).prefix_inc(); +} static mut t_5: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); static mut n_6: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); static mut t_8: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); static mut n_9: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); -pub static mut check_10: std::cell::LazyCell = - std::cell::LazyCell::new(|| unsafe { AssertsDtorCountAtExit { expected: 2 } }); +static mut t_11: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +static mut n_12: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub static mut check_13: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { AssertsDtorCountAtExit { expected: 4 } }); pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -93,24 +99,42 @@ pub fn main() { unsafe fn main_0() -> i32 { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (0))); assert!(((unsafe { count_4() }) == (1))); - assert!(((unsafe { b_count_int_11() }) == (2))); - assert!(((unsafe { count_4() }) == (3))); - assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (1))); + assert!(((unsafe { b_count_local_15() }) == (1))); + assert!(((unsafe { count_4() }) == (2))); assert!(((unsafe { count_7() }) == (1))); - assert!(((unsafe { b_count_long_12() }) == (2))); - assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (2))); + assert!(((unsafe { b_count_int_16() }) == (2))); + assert!(((unsafe { count_7() }) == (3))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (3))); + assert!(((unsafe { count_10() }) == (1))); + assert!(((unsafe { b_count_long_17() }) == (2))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ctorCount_0)) == (4))); return 0; } -pub unsafe fn b_count_int_11() -> i32 { - return (unsafe { count_4() }); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct S_14 {} +pub unsafe fn count_18() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut n_20)).prefix_inc(); } -pub unsafe fn b_count_long_12() -> i32 { +static mut t_19: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { LifetimeTracked::new() }); +static mut n_20: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub unsafe fn b_count_int_16() -> i32 { return (unsafe { count_7() }); } +pub unsafe fn b_count_long_17() -> i32 { + return (unsafe { count_10() }); +} +pub unsafe fn b_count_local_15() -> i32 { + return (unsafe { count_18() }); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct S_21 {} pub unsafe fn __cpp2rust_init_globals() { std::cell::LazyCell::force(&*&raw const ctorCount_0); std::cell::LazyCell::force(&*&raw const copyCount_1); std::cell::LazyCell::force(&*&raw const dtorCount_2); std::cell::LazyCell::force(&*&raw const useCount_3); - std::cell::LazyCell::force(&*&raw const check_10); + std::cell::LazyCell::force(&*&raw const check_13); } From 5c55044dc73a91c5f9c15f6213d94d00795f4600 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 17 Nov 2026 14:17:39 +0000 Subject: [PATCH 4/4] Delete tests --- .../out/refcount/expr.prim.lambda.closure.rs | 26 ------------------ .../out/unsafe/expr.prim.lambda.closure.rs | 27 ------------------- 2 files changed, 53 deletions(-) delete mode 100644 tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.closure.rs delete mode 100644 tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.closure.rs diff --git a/tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.closure.rs b/tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.closure.rs deleted file mode 100644 index c559a87fd2..0000000000 --- a/tests/std/expr/expr.prim/expr.prim.lambda/out/refcount/expr.prim.lambda.closure.rs +++ /dev/null @@ -1,26 +0,0 @@ -extern crate libcc2rs; -use libcc2rs::*; -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::io::prelude::*; -use std::io::{Read, Seek, Write}; -use std::os::fd::AsFd; -use std::rc::{Rc, Weak}; -pub fn test_0() { - let twice: Value> = - Rc::new(RefCell::new(lambda!(Generic, {}, |mut x: i32| -> i32 { - return (x * 2); - }))); - let mut n: i32 = 4; - assert!((n == 4)); - assert!(({ (({ (*twice.borrow()).spec:: i32>(0).call(3,) }) == 6) })); -} -pub fn main() { - __cpp2rust_init_globals(); - std::process::exit(main_0()); -} -fn main_0() -> i32 { - ({ test_0() }); - return 0; -} -pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.closure.rs b/tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.closure.rs deleted file mode 100644 index c30d0f591a..0000000000 --- a/tests/std/expr/expr.prim/expr.prim.lambda/out/unsafe/expr.prim.lambda.closure.rs +++ /dev/null @@ -1,27 +0,0 @@ -extern crate libc; -use libc::*; -extern crate libcc2rs; -use libcc2rs::*; -use std::collections::BTreeMap; -use std::io::{Read, Seek, Write}; -use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; -use std::rc::Rc; -pub unsafe fn test_0() { - let mut twice: FnPtr = lambda_unsafe!(Generic, {}, |mut x: i32| -> i32 { - return ((x) * (2)); - }); - let mut n: i32 = 4; - assert!(((n) == (4))); - assert!(((unsafe { twice.spec:: i32>(0).call(3,) }) == (6))); -} -pub fn main() { - unsafe { - __cpp2rust_init_globals(); - std::process::exit(main_0() as i32); - } -} -unsafe fn main_0() -> i32 { - (unsafe { test_0() }); - return 0; -} -pub unsafe fn __cpp2rust_init_globals() {}