From 2ba9e13aabc61eb044c791b6186034cfa0ac2828 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 7 Oct 2026 20:26:09 +0100 Subject: [PATCH 1/2] Distinguish declarations from one macro expansion in decl IDs --- cpp2rust/converter/converter_lib.cpp | 10 ++++++++-- tests/unit/template_static_member.cpp | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 2ff808e32..d75ac89e7 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -744,8 +744,14 @@ unsigned GetArraySize(clang::QualType array_type) { } static std::string GetLocationID(const clang::Decl *decl) { - return GetFileName(decl) + std::to_string(GetLineNumber(decl)) + - std::to_string(GetColumnNumber(decl)); + auto id = GetFileName(decl) + std::to_string(GetLineNumber(decl)) + + std::to_string(GetColumnNumber(decl)); + if (auto loc = decl->getLocation(); loc.isMacroID()) { + const auto &src_mgr = decl->getASTContext().getSourceManager(); + id += std::to_string(src_mgr.getSpellingLineNumber(loc)) + + std::to_string(src_mgr.getSpellingColumnNumber(loc)); + } + return id; } static std::string GetParamSignature(const clang::Decl *decl) { diff --git a/tests/unit/template_static_member.cpp b/tests/unit/template_static_member.cpp index a4ab11695..3874174fc 100644 --- a/tests/unit/template_static_member.cpp +++ b/tests/unit/template_static_member.cpp @@ -6,6 +6,18 @@ template struct Static { template T Static::s = 55; +template struct Range { + static T lo; + static T hi; +}; + +#define DEFINE_RANGE(T, a, b) \ + template <> T Range::lo = a; \ + template <> T Range::hi = b + +DEFINE_RANGE(int, -1, 1); +DEFINE_RANGE(long, -2, 2); + int main() { Static::s = 22; Static::s = 33; @@ -13,6 +25,8 @@ int main() { assert(Static::s == 22); assert(Static::s == 33); assert(Static::s == 55); + assert(Range::lo == -1 && Range::hi == 1); + assert(Range::lo == -2 && Range::hi == 2); return 0; } From c0d18992943b408004b2d8c856b1d1d3cfdf8257 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 7 Oct 2026 22:52:35 +0100 Subject: [PATCH 2/2] Update tests --- .../out/refcount/template_static_member.rs | 27 +++++++++++++++++++ .../unit/out/unsafe/template_static_member.rs | 23 ++++++++++++++++ tests/unit/template_static_member.cpp | 2 +- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/unit/out/refcount/template_static_member.rs b/tests/unit/out/refcount/template_static_member.rs index 145d72c1a..d4fb45f82 100644 --- a/tests/unit/out/refcount/template_static_member.rs +++ b/tests/unit/out/refcount/template_static_member.rs @@ -24,6 +24,24 @@ thread_local!( #[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] #[byte_size(1)] pub struct Static_long_ {} +thread_local!( + pub static lo_3: Value = Rc::new(RefCell::new(-1_i32)); +); +thread_local!( + pub static hi_4: Value = Rc::new(RefCell::new(1)); +); +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct Range_int_ {} +thread_local!( + pub static lo_5: Value = Rc::new(RefCell::new((-2_i32 as i64))); +); +thread_local!( + pub static hi_6: Value = Rc::new(RefCell::new(2_i64)); +); +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct Range_long_ {} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -34,10 +52,19 @@ fn main_0() -> i32 { assert!((s_0.with(|rc| *rc.borrow()) == 22)); assert!(((s_1.with(|rc| *rc.borrow()) as i32) == 33)); assert!((s_2.with(|rc| *rc.borrow()) == 55_i64)); + assert!((lo_3.with(|rc| *rc.borrow()) == -1_i32) && (hi_4.with(|rc| *rc.borrow()) == 1)); + assert!( + (lo_5.with(|rc| *rc.borrow()) == (-2_i32 as i64)) + && (hi_6.with(|rc| *rc.borrow()) == 2_i64) + ); return 0; } pub fn __cpp2rust_init_globals() { let _ = s_0.with(|_| ()); let _ = s_1.with(|_| ()); let _ = s_2.with(|_| ()); + let _ = lo_3.with(|_| ()); + let _ = hi_4.with(|_| ()); + let _ = lo_5.with(|_| ()); + let _ = hi_6.with(|_| ()); } diff --git a/tests/unit/out/unsafe/template_static_member.rs b/tests/unit/out/unsafe/template_static_member.rs index d40645f03..070b03fc4 100644 --- a/tests/unit/out/unsafe/template_static_member.rs +++ b/tests/unit/out/unsafe/template_static_member.rs @@ -19,6 +19,17 @@ pub static mut s_2: std::cell::LazyCell = std::cell::LazyCell::new(|| unsaf #[repr(C)] #[derive(Copy, Clone, VaArg, FnPtrArg, Default)] pub struct Static_long_ {} +pub static mut lo_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { -1_i32 }); +pub static mut hi_4: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 1 }); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Range_int_ {} +pub static mut lo_5: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { (-2_i32 as i64) }); +pub static mut hi_6: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 2_i64 }); +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Range_long_ {} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -31,10 +42,22 @@ unsafe fn main_0() -> i32 { assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut s_0)) == (22))); assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut s_1)) as i32) == (33))); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut s_2)) == (55_i64))); + assert!( + ((*std::cell::LazyCell::force_mut(&mut *&raw mut lo_3)) == (-1_i32)) + && ((*std::cell::LazyCell::force_mut(&mut *&raw mut hi_4)) == (1)) + ); + assert!( + ((*std::cell::LazyCell::force_mut(&mut *&raw mut lo_5)) == (-2_i32 as i64)) + && ((*std::cell::LazyCell::force_mut(&mut *&raw mut hi_6)) == (2_i64)) + ); return 0; } pub unsafe fn __cpp2rust_init_globals() { std::cell::LazyCell::force(&*&raw const s_0); std::cell::LazyCell::force(&*&raw const s_1); std::cell::LazyCell::force(&*&raw const s_2); + std::cell::LazyCell::force(&*&raw const lo_3); + std::cell::LazyCell::force(&*&raw const hi_4); + std::cell::LazyCell::force(&*&raw const lo_5); + std::cell::LazyCell::force(&*&raw const hi_6); } diff --git a/tests/unit/template_static_member.cpp b/tests/unit/template_static_member.cpp index 3874174fc..ee75e760d 100644 --- a/tests/unit/template_static_member.cpp +++ b/tests/unit/template_static_member.cpp @@ -11,7 +11,7 @@ template struct Range { static T hi; }; -#define DEFINE_RANGE(T, a, b) \ +#define DEFINE_RANGE(T, a, b) \ template <> T Range::lo = a; \ template <> T Range::hi = b