Skip to content

Commit 63e59b1

Browse files
authored
Allow default initialization in static variables (#5)
Use 0 rather than default::default()
1 parent 5d84938 commit 63e59b1

28 files changed

Lines changed: 83 additions & 118 deletions

cpp2rust/converter/converter.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,15 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
383383
return false;
384384
}
385385
StrCat(AccessSpecifierAsString(decl->getAccess()), keyword::kStatic);
386+
if (!qual_type.isConstQualified()) {
387+
StrCat(keyword_mut_);
388+
}
386389
ENSURE(decl_ids_.insert(GetID(decl)).second);
387390
} else if (decl->isStaticLocal()) {
388391
StrCat(keyword::kStatic);
392+
if (!qual_type.isConstQualified()) {
393+
StrCat(keyword_mut_);
394+
}
389395
} else if (decl->isLocalVarDecl()) {
390396
StrCat(keyword::kLet);
391397
}
@@ -2679,18 +2685,18 @@ std::string Converter::GetDefaultAsString(clang::QualType qual_type) {
26792685
}
26802686

26812687
std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) {
2682-
static llvm::DenseMap<unsigned, std::string> default_for_type = {
2683-
{clang::BuiltinType::Char_U, "0_u8"},
2684-
{clang::BuiltinType::SChar, "0_i8"},
2685-
{clang::BuiltinType::UChar, "0_u8"},
2686-
};
2687-
26882688
qual_type = qual_type.getUnqualifiedType().getCanonicalType();
2689-
if (auto builtin = qual_type->getAs<clang::BuiltinType>()) {
2690-
auto it = default_for_type.find(builtin->getKind());
2691-
if (it != default_for_type.end()) {
2692-
return it->second;
2693-
}
2689+
2690+
if (qual_type->isBooleanType()) {
2691+
return "false";
2692+
}
2693+
2694+
if (qual_type->isIntegerType() && !qual_type->isEnumeralType()) {
2695+
return std::format("0_{}", ToString(qual_type));
2696+
}
2697+
2698+
if (qual_type->isFloatingType()) {
2699+
return std::format("0.0_{}", ToString(qual_type));
26942700
}
26952701

26962702
return std::format("<{}>::default()", ToString(qual_type));

tests/benchmarks/out/unsafe/bfs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub unsafe fn BFS_0(graph: *const Graph, mut start_vertex: u32) -> *mut u32 {
6262
let mut Q: Queue = Queue {
6363
elems: Box::leak(
6464
(0..((*graph).V as u64))
65-
.map(|_| <u32>::default())
65+
.map(|_| 0_u32)
6666
.collect::<Box<[u32]>>(),
6767
)
6868
.as_mut_ptr(),
@@ -72,13 +72,13 @@ pub unsafe fn BFS_0(graph: *const Graph, mut start_vertex: u32) -> *mut u32 {
7272
};
7373
let mut visited: *mut bool = Box::leak(
7474
(0..((*graph).V as u64))
75-
.map(|_| <bool>::default())
75+
.map(|_| false)
7676
.collect::<Box<[bool]>>(),
7777
)
7878
.as_mut_ptr();
7979
let mut pred: *mut u32 = Box::leak(
8080
(0..((*graph).V as u64))
81-
.map(|_| <u32>::default())
81+
.map(|_| 0_u32)
8282
.collect::<Box<[u32]>>(),
8383
)
8484
.as_mut_ptr();

tests/ub/out/unsafe/ub10.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ pub fn main() {
1313
}
1414
}
1515
unsafe fn main_0() -> i32 {
16-
let mut arr: *mut i32 = Box::leak(
17-
(0..10_u64)
18-
.map(|_| <i32>::default())
19-
.collect::<Box<[i32]>>(),
20-
)
21-
.as_mut_ptr();
16+
let mut arr: *mut i32 =
17+
Box::leak((0..10_u64).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr();
2218
let mut ptr: *mut i32 = arr.offset((1) as isize);
2319
let mut out: i32 = (*ptr);
2420

tests/ub/out/unsafe/ub14.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ pub fn main() {
1313
}
1414
}
1515
unsafe fn main_0() -> i32 {
16-
let mut arr1: *mut i32 = Box::leak(
17-
(0..100_u64)
18-
.map(|_| <i32>::default())
19-
.collect::<Box<[i32]>>(),
20-
)
21-
.as_mut_ptr();
16+
let mut arr1: *mut i32 =
17+
Box::leak((0..100_u64).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr();
2218
(*arr1.offset((100) as isize)) = 1;
2319

2420
::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut(

tests/ub/out/unsafe/ub15.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ pub fn main() {
1313
}
1414
}
1515
unsafe fn main_0() -> i32 {
16-
let mut arr: *mut i32 = Box::leak(
17-
(0..15_u64)
18-
.map(|_| <i32>::default())
19-
.collect::<Box<[i32]>>(),
20-
)
21-
.as_mut_ptr();
16+
let mut arr: *mut i32 =
17+
Box::leak((0..15_u64).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr();
2218
let mut ptr: *mut i32 = arr.offset((15) as isize);
2319
let mut out: i32 = (*ptr);
2420

tests/ub/out/unsafe/ub16.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ pub fn main() {
1616
}
1717
}
1818
unsafe fn main_0() -> i32 {
19-
let mut p1: *mut i32 = Box::leak(
20-
(0..10_u64)
21-
.map(|_| <i32>::default())
22-
.collect::<Box<[i32]>>(),
23-
)
24-
.as_mut_ptr();
19+
let mut p1: *mut i32 =
20+
Box::leak((0..10_u64).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr();
2521
let mut out: i32 = (*(unsafe {
2622
let _a: *mut i32 = (&mut (*p1.offset((1) as isize)) as *mut i32);
2723
foo_0(_a)

tests/ub/out/unsafe/ub20.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ pub fn main() {
1616
}
1717
}
1818
unsafe fn main_0() -> i32 {
19-
let mut x: *mut i32 = Box::leak(
20-
(0..10_u64)
21-
.map(|_| <i32>::default())
22-
.collect::<Box<[i32]>>(),
23-
)
24-
.as_mut_ptr();
19+
let mut x: *mut i32 =
20+
Box::leak((0..10_u64).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr();
2521
(unsafe {
2622
let _single: *mut i32 = x;
2723
foo_0(_single)

tests/ub/out/unsafe/ub9.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ pub fn main() {
1313
}
1414
}
1515
unsafe fn main_0() -> i32 {
16-
let mut arr: *mut i32 = Box::leak(
17-
(0..10_u64)
18-
.map(|_| <i32>::default())
19-
.collect::<Box<[i32]>>(),
20-
)
21-
.as_mut_ptr();
16+
let mut arr: *mut i32 =
17+
Box::leak((0..10_u64).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr();
2218
let mut out: i32 = (*arr.offset((10) as isize));
2319

2420
::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut(

tests/unit/out/refcount/static_local.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@ use std::io::{Read, Write};
88
use std::os::fd::AsFd;
99
use std::rc::{Rc, Weak};
1010
pub fn foo_0() -> i32 {
11+
thread_local!(
12+
static static_i: Value<i32> = <Value<i32>>::default();
13+
);
14+
thread_local!(
15+
static static_f: Value<f32> = <Value<f32>>::default();
16+
);
17+
thread_local!(
18+
static static_b: Value<bool> = <Value<bool>>::default();
19+
);
1120
thread_local!(
1221
static kX1: Value<i32> = Rc::new(RefCell::new(1));
1322
);
1423
thread_local!(
1524
static kX2: Value<i32> = Rc::new(RefCell::new(2));
1625
);
1726
(*kX1.with(Value::clone).borrow_mut()) += 1;
18-
return ((*kX1.with(Value::clone).borrow()) + (*kX2.with(Value::clone).borrow()));
27+
return (((*kX1.with(Value::clone).borrow()) + (*kX2.with(Value::clone).borrow()))
28+
+ (*static_i.with(Value::clone).borrow()));
1929
}
2030
pub fn main() {
2131
std::process::exit(main_0());

tests/unit/out/unsafe/06_new_array.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ pub fn main() {
1313
}
1414
}
1515
unsafe fn main_0() -> i32 {
16-
let mut e: *mut i32 =
17-
Box::leak((0..2_u64).map(|_| <i32>::default()).collect::<Box<[i32]>>()).as_mut_ptr();
16+
let mut e: *mut i32 = Box::leak((0..2_u64).map(|_| 0_i32).collect::<Box<[i32]>>()).as_mut_ptr();
1817
(*e.offset((0) as isize)) = 6;
1918
(*e.offset((1) as isize)) = 7;
2019

0 commit comments

Comments
 (0)