From 4dc9b4c411d57f1770f19c8cb75c945ba8f86045 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 26 May 2026 13:59:08 +0100 Subject: [PATCH 1/5] Avoid shadowing of static variables --- cpp2rust/converter/converter.cpp | 9 ++- tests/unit/out/refcount/addr_of_global.rs | 32 +++++----- .../out/refcount/bool_condition_logical.rs | 10 +-- .../out/refcount/bool_condition_logical_c.rs | 10 +-- tests/unit/out/refcount/default_in_statics.rs | 61 ++++++++++--------- tests/unit/out/refcount/enum_int_interop.rs | 26 ++++---- tests/unit/out/refcount/enum_int_interop_c.rs | 26 ++++---- tests/unit/out/refcount/fn_ptr_global.rs | 16 ++--- tests/unit/out/refcount/fn_ptr_vtable.rs | 8 +-- .../global-initialization-using-global.rs | 12 ++-- tests/unit/out/refcount/global_pointers.rs | 14 +++-- .../refcount/global_without_initializer.rs | 12 ++-- tests/unit/out/refcount/null_in_statics.rs | 32 +++++----- tests/unit/out/refcount/static_local.rs | 16 ++--- tests/unit/out/refcount/static_shadow.rs | 37 +++++++++++ .../unit/out/refcount/static_var_in_class.rs | 8 +-- tests/unit/out/refcount/string_escape.rs | 4 +- tests/unit/out/refcount/void_cast.rs | 16 ++--- tests/unit/out/unsafe/addr_of_global.rs | 32 +++++----- .../unit/out/unsafe/bool_condition_logical.rs | 10 +-- .../out/unsafe/bool_condition_logical_c.rs | 10 +-- tests/unit/out/unsafe/default_in_statics.rs | 56 ++++++++--------- tests/unit/out/unsafe/enum_int_interop.rs | 26 ++++---- tests/unit/out/unsafe/enum_int_interop_c.rs | 29 +++++---- tests/unit/out/unsafe/fn_ptr_global.rs | 16 ++--- tests/unit/out/unsafe/fn_ptr_vtable.rs | 8 +-- .../global-initialization-using-global.rs | 8 +-- tests/unit/out/unsafe/global_pointers.rs | 12 ++-- .../out/unsafe/global_without_initializer.rs | 12 ++-- tests/unit/out/unsafe/null_in_statics.rs | 32 +++++----- tests/unit/out/unsafe/static_local.rs | 14 ++--- tests/unit/out/unsafe/static_shadow.rs | 36 +++++++++++ tests/unit/out/unsafe/static_var_in_class.rs | 8 +-- tests/unit/out/unsafe/string_escape.rs | 4 +- .../out/unsafe/union_tagged_struct_arms.rs | 4 +- tests/unit/out/unsafe/void_cast.rs | 16 ++--- tests/unit/static_shadow.c | 19 ++++++ 37 files changed, 403 insertions(+), 298 deletions(-) create mode 100644 tests/unit/out/refcount/static_shadow.rs create mode 100644 tests/unit/out/unsafe/static_shadow.rs create mode 100644 tests/unit/static_shadow.c diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 780bd7d8..b2a7d03a 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -393,6 +393,10 @@ void Converter::ConvertVaListVarDecl(clang::VarDecl *decl) { StrCat(keyword_mut_, GetNamedDeclAsString(decl), token::kColon, "VaList"); } +static std::string StaticVarName(const clang::NamedDecl *decl) { + return "s_" + ReplaceAll(Mapper::ToString(decl), "::", "_"); +} + bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) { auto qual_type = decl->getType(); auto name = GetNamedDeclAsString(decl); @@ -403,7 +407,7 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) { } if (decl->isFileVarDecl()) { - name = ReplaceAll(Mapper::ToString(decl), "::", "_"); + name = StaticVarName(decl); if ((decl->isThisDeclarationADefinition() == clang::VarDecl::DeclarationOnly && !decl->hasInit()) || @@ -414,6 +418,7 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) { keyword_mut_); ENSURE(decl_ids_.insert(GetID(decl)).second); } else if (decl->isStaticLocal()) { + name = StaticVarName(decl); StrCat(keyword::kStatic, keyword_mut_); } else if (decl->isLocalVarDecl()) { StrCat(keyword::kLet); @@ -2378,7 +2383,7 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) { } if (IsGlobalVar(expr)) { - return ReplaceAll(Mapper::ToString(expr->getDecl()), "::", "_"); + return StaticVarName(expr->getDecl()); } return GetNamedDeclAsString(decl); diff --git a/tests/unit/out/refcount/addr_of_global.rs b/tests/unit/out/refcount/addr_of_global.rs index 943025df..f4480502 100644 --- a/tests/unit/out/refcount/addr_of_global.rs +++ b/tests/unit/out/refcount/addr_of_global.rs @@ -42,29 +42,29 @@ impl Clone for Outer { } impl ByteRepr for Outer {} thread_local!( - pub static alpha: Value = Rc::new(RefCell::new(Inner { + pub static s_alpha: Value = Rc::new(RefCell::new(Inner { value: Rc::new(RefCell::new(1)), })); ); thread_local!( - pub static beta: Value = Rc::new(RefCell::new(Inner { + pub static s_beta: Value = Rc::new(RefCell::new(Inner { value: Rc::new(RefCell::new(2)), })); ); thread_local!( - pub static shared: Value = Rc::new(RefCell::new(Inner { + pub static s_shared: Value = Rc::new(RefCell::new(Inner { value: Rc::new(RefCell::new(42)), })); ); thread_local!( - pub static items: Value]>> = Rc::new(RefCell::new(Box::new([ - (alpha.with(Value::clone).as_pointer()), - (beta.with(Value::clone).as_pointer()), + pub static s_items: Value]>> = Rc::new(RefCell::new(Box::new([ + (s_alpha.with(Value::clone).as_pointer()), + (s_beta.with(Value::clone).as_pointer()), ]))); ); thread_local!( - pub static obj: Value = Rc::new(RefCell::new(Outer { - p: Rc::new(RefCell::new((shared.with(Value::clone).as_pointer()))), + pub static s_obj: Value = Rc::new(RefCell::new(Outer { + p: Rc::new(RefCell::new((s_shared.with(Value::clone).as_pointer()))), })); ); pub fn main() { @@ -72,7 +72,7 @@ pub fn main() { } fn main_0() -> i32 { assert!( - ((*(*(*items.with(Value::clone).borrow())[(0) as usize] + ((*(*(*s_items.with(Value::clone).borrow())[(0) as usize] .upgrade() .deref()) .value @@ -80,7 +80,7 @@ fn main_0() -> i32 { == 1) ); assert!( - ((*(*(*items.with(Value::clone).borrow())[(1) as usize] + ((*(*(*s_items.with(Value::clone).borrow())[(1) as usize] .upgrade() .deref()) .value @@ -88,7 +88,7 @@ fn main_0() -> i32 { == 2) ); assert!( - ((*(*(*(*obj.with(Value::clone).borrow()).p.borrow()) + ((*(*(*(*s_obj.with(Value::clone).borrow()).p.borrow()) .upgrade() .deref()) .value @@ -96,13 +96,13 @@ fn main_0() -> i32 { == 42) ); thread_local!( - static cache: Value]>> = Rc::new(RefCell::new(Box::new([ - (alpha.with(Value::clone).as_pointer()), - (beta.with(Value::clone).as_pointer()), + static s_cache: Value]>> = Rc::new(RefCell::new(Box::new([ + (s_alpha.with(Value::clone).as_pointer()), + (s_beta.with(Value::clone).as_pointer()), ]))); ); assert!( - ((*(*(*cache.with(Value::clone).borrow())[(0) as usize] + ((*(*(*s_cache.with(Value::clone).borrow())[(0) as usize] .upgrade() .deref()) .value @@ -110,7 +110,7 @@ fn main_0() -> i32 { == 1) ); assert!( - ((*(*(*cache.with(Value::clone).borrow())[(1) as usize] + ((*(*(*s_cache.with(Value::clone).borrow())[(1) as usize] .upgrade() .deref()) .value diff --git a/tests/unit/out/refcount/bool_condition_logical.rs b/tests/unit/out/refcount/bool_condition_logical.rs index 31425290..cab89b4c 100644 --- a/tests/unit/out/refcount/bool_condition_logical.rs +++ b/tests/unit/out/refcount/bool_condition_logical.rs @@ -25,11 +25,11 @@ impl From for Code { } libcc2rs::impl_enum_inc_dec!(Code); thread_local!( - pub static side_effect: Value = Rc::new(RefCell::new(0)); + pub static s_side_effect: Value = Rc::new(RefCell::new(0)); ); pub fn observe_0(v: i32) -> i32 { let v: Value = Rc::new(RefCell::new(v)); - (*side_effect.with(Value::clone).borrow_mut()).prefix_inc(); + (*s_side_effect.with(Value::clone).borrow_mut()).prefix_inc(); return (*v.borrow()); } pub fn returns_one_1() -> i32 { @@ -66,7 +66,7 @@ fn main_0() -> i32 { { assert!(true); } - (*side_effect.with(Value::clone).borrow_mut()) = 0; + (*s_side_effect.with(Value::clone).borrow_mut()) = 0; if ((*zero.borrow()) != 0) && (({ let _v: i32 = 1; @@ -75,7 +75,7 @@ fn main_0() -> i32 { { assert!(false); } - assert!(((*side_effect.with(Value::clone).borrow()) == 0)); + assert!(((*s_side_effect.with(Value::clone).borrow()) == 0)); if ((*n.borrow()) != 0) || (({ let _v: i32 = 1; @@ -84,7 +84,7 @@ fn main_0() -> i32 { { assert!(true); } - assert!(((*side_effect.with(Value::clone).borrow()) == 0)); + assert!(((*s_side_effect.with(Value::clone).borrow()) == 0)); let x: Value = Rc::new(RefCell::new(5)); let y: Value = Rc::new(RefCell::new(3)); let flags: Value = Rc::new(RefCell::new(2_u32)); diff --git a/tests/unit/out/refcount/bool_condition_logical_c.rs b/tests/unit/out/refcount/bool_condition_logical_c.rs index 31e3d7fd..4a7132ae 100644 --- a/tests/unit/out/refcount/bool_condition_logical_c.rs +++ b/tests/unit/out/refcount/bool_condition_logical_c.rs @@ -25,11 +25,11 @@ impl From for Code { } libcc2rs::impl_enum_inc_dec!(Code); thread_local!( - pub static side_effect: Value = Rc::new(RefCell::new(0)); + pub static s_side_effect: Value = Rc::new(RefCell::new(0)); ); pub fn observe_0(v: i32) -> i32 { let v: Value = Rc::new(RefCell::new(v)); - (*side_effect.with(Value::clone).borrow_mut()).prefix_inc(); + (*s_side_effect.with(Value::clone).borrow_mut()).prefix_inc(); return (*v.borrow()); } pub fn returns_one_1() -> i32 { @@ -70,7 +70,7 @@ fn main_0() -> i32 { { assert!((1 != 0)); } - (*side_effect.with(Value::clone).borrow_mut()) = 0; + (*s_side_effect.with(Value::clone).borrow_mut()) = 0; if (((((*zero.borrow()) != 0) && (({ let _v: i32 = 1; @@ -80,7 +80,7 @@ fn main_0() -> i32 { { assert!((0 != 0)); } - assert!(((((*side_effect.with(Value::clone).borrow()) == 0) as i32) != 0)); + assert!(((((*s_side_effect.with(Value::clone).borrow()) == 0) as i32) != 0)); if (((((*n.borrow()) != 0) || (({ let _v: i32 = 1; @@ -90,7 +90,7 @@ fn main_0() -> i32 { { assert!((1 != 0)); } - assert!(((((*side_effect.with(Value::clone).borrow()) == 0) as i32) != 0)); + assert!(((((*s_side_effect.with(Value::clone).borrow()) == 0) as i32) != 0)); let x: Value = Rc::new(RefCell::new(5)); let y: Value = Rc::new(RefCell::new(3)); let flags: Value = Rc::new(RefCell::new(2_u32)); diff --git a/tests/unit/out/refcount/default_in_statics.rs b/tests/unit/out/refcount/default_in_statics.rs index dc58f95d..3149da8c 100644 --- a/tests/unit/out/refcount/default_in_statics.rs +++ b/tests/unit/out/refcount/default_in_statics.rs @@ -99,18 +99,18 @@ impl Default for Foo { } impl ByteRepr for Foo {} thread_local!( - pub static static_fn: Value i32>> = Rc::new(RefCell::new(FnPtr::null())); + pub static s_static_fn: Value i32>> = Rc::new(RefCell::new(FnPtr::null())); ); thread_local!( - pub static static_outer: Value = Rc::new(RefCell::new(::default())); + pub static s_static_outer: Value = Rc::new(RefCell::new(::default())); ); thread_local!( - pub static static_inner_array: Value> = Rc::new(RefCell::new( + pub static s_static_inner_array: Value> = Rc::new(RefCell::new( (0..2).map(|_| ::default()).collect::>(), )); ); thread_local!( - pub static static_foo: Value = Rc::new(RefCell::new(Foo { + pub static s_static_foo: Value = Rc::new(RefCell::new(Foo { s1: Rc::new(RefCell::new(Ptr::from_string_literal("hello"))), s2: Rc::new(RefCell::new(Ptr::::null())), fn1: Rc::new(RefCell::new(FnPtr::null())), @@ -119,7 +119,7 @@ thread_local!( })); ); thread_local!( - pub static static_foo_array: Value> = Rc::new(RefCell::new(Box::new([ + pub static s_static_foo_array: Value> = Rc::new(RefCell::new(Box::new([ Foo { s1: Rc::new(RefCell::new(Ptr::from_string_literal("first"))), s2: Rc::new(RefCell::new(Ptr::::null())), @@ -138,38 +138,39 @@ thread_local!( ); pub fn check_local_static_0() { thread_local!( - static local_outer: Value = Rc::new(RefCell::new(::default())); + static s_local_outer: Value = Rc::new(RefCell::new(::default())); ); thread_local!( - static local_fn: Value i32>> = Rc::new(RefCell::new(FnPtr::null())); + static s_local_fn: Value i32>> = Rc::new(RefCell::new(FnPtr::null())); ); thread_local!( - static local_p: Value> = Rc::new(RefCell::new(Ptr::::null())); + static s_local_p: Value> = Rc::new(RefCell::new(Ptr::::null())); ); - assert!((*(*local_outer.with(Value::clone).borrow()).p1.borrow()).is_null()); - assert!((*(*local_outer.with(Value::clone).borrow()).fn_.borrow()).is_null()); - assert!((*local_fn.with(Value::clone).borrow()).is_null()); - assert!((*local_p.with(Value::clone).borrow()).is_null()); + assert!((*(*s_local_outer.with(Value::clone).borrow()).p1.borrow()).is_null()); + assert!((*(*s_local_outer.with(Value::clone).borrow()).fn_.borrow()).is_null()); + assert!((*s_local_fn.with(Value::clone).borrow()).is_null()); + assert!((*s_local_p.with(Value::clone).borrow()).is_null()); } pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - assert!((*static_fn.with(Value::clone).borrow()).is_null()); - assert!((*(*static_outer.with(Value::clone).borrow()).p1.borrow()).is_null()); - assert!((*(*static_outer.with(Value::clone).borrow()).p2.borrow()).is_null()); - assert!((*(*static_outer.with(Value::clone).borrow()).cp.borrow()).is_null()); - assert!((*(*static_outer.with(Value::clone).borrow()).pp.borrow()).is_null()); - assert!((*(*static_outer.with(Value::clone).borrow()).fn_.borrow()).is_null()); + assert!((*s_static_fn.with(Value::clone).borrow()).is_null()); + assert!((*(*s_static_outer.with(Value::clone).borrow()).p1.borrow()).is_null()); + assert!((*(*s_static_outer.with(Value::clone).borrow()).p2.borrow()).is_null()); + assert!((*(*s_static_outer.with(Value::clone).borrow()).cp.borrow()).is_null()); + assert!((*(*s_static_outer.with(Value::clone).borrow()).pp.borrow()).is_null()); + assert!((*(*s_static_outer.with(Value::clone).borrow()).fn_.borrow()).is_null()); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 3) { - assert!(((*(*static_outer.with(Value::clone).borrow()).arr.borrow()) - [(*i.borrow()) as usize]) - .is_null()); + assert!( + ((*(*s_static_outer.with(Value::clone).borrow()).arr.borrow())[(*i.borrow()) as usize]) + .is_null() + ); (*i.borrow_mut()).prefix_inc(); } assert!( - (*(*(*static_outer.with(Value::clone).borrow()).inner.borrow()) + (*(*(*s_static_outer.with(Value::clone).borrow()).inner.borrow()) .name .borrow()) .is_null() @@ -177,33 +178,33 @@ fn main_0() -> i32 { let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 2) { assert!( - (*(*static_inner_array.with(Value::clone).borrow())[(*i.borrow()) as usize] + (*(*s_static_inner_array.with(Value::clone).borrow())[(*i.borrow()) as usize] .name .borrow()) .is_null() ); (*i.borrow_mut()).prefix_inc(); } - assert!((*(*static_foo.with(Value::clone).borrow()).s2.borrow()).is_null()); - assert!((*(*static_foo.with(Value::clone).borrow()).fn1.borrow()).is_null()); - assert!((*(*static_foo.with(Value::clone).borrow()).fn2.borrow()).is_null()); - assert!(((*(*static_foo.with(Value::clone).borrow()).n.borrow()) == 42)); + assert!((*(*s_static_foo.with(Value::clone).borrow()).s2.borrow()).is_null()); + assert!((*(*s_static_foo.with(Value::clone).borrow()).fn1.borrow()).is_null()); + assert!((*(*s_static_foo.with(Value::clone).borrow()).fn2.borrow()).is_null()); + assert!(((*(*s_static_foo.with(Value::clone).borrow()).n.borrow()) == 42)); let i: Value = Rc::new(RefCell::new(0)); 'loop_: while ((*i.borrow()) < 2) { assert!( - (*(*static_foo_array.with(Value::clone).borrow())[(*i.borrow()) as usize] + (*(*s_static_foo_array.with(Value::clone).borrow())[(*i.borrow()) as usize] .s2 .borrow()) .is_null() ); assert!( - (*(*static_foo_array.with(Value::clone).borrow())[(*i.borrow()) as usize] + (*(*s_static_foo_array.with(Value::clone).borrow())[(*i.borrow()) as usize] .fn1 .borrow()) .is_null() ); assert!( - (*(*static_foo_array.with(Value::clone).borrow())[(*i.borrow()) as usize] + (*(*s_static_foo_array.with(Value::clone).borrow())[(*i.borrow()) as usize] .fn2 .borrow()) .is_null() diff --git a/tests/unit/out/refcount/enum_int_interop.rs b/tests/unit/out/refcount/enum_int_interop.rs index 4426a475..c4510f95 100644 --- a/tests/unit/out/refcount/enum_int_interop.rs +++ b/tests/unit/out/refcount/enum_int_interop.rs @@ -80,16 +80,16 @@ impl Clone for Entry { } impl ByteRepr for Entry {} thread_local!( - pub static global_color: Value = Rc::new(RefCell::new(Color::GREEN)); + pub static s_global_color: Value = Rc::new(RefCell::new(Color::GREEN)); ); thread_local!( - pub static global_opt: Value