Skip to content

Commit f85ace1

Browse files
committed
HACK: transpile: use dashmap to enable interior mutability for TypedAstContext::c_types
note: this is a hack because it adds a memory leak in the `Index` impl
1 parent 10d2cfb commit f85ace1

4 files changed

Lines changed: 23 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

c2rust-transpile/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ c2rust-ast-printer = { path = "../c2rust-ast-printer", version = "0.22.1" }
1818
c2rust-bitfields = { path = "../c2rust-bitfields", version = "0.22.1" }
1919
c2rust-rust-tools = { path = "../c2rust-rust-tools", version = "0.22.1" }
2020
colored = "2.0"
21+
dashmap = "4.0"
2122
dtoa = "1.0"
2223
failure = "0.1.5"
2324
fern = { version = "0.6", features = ["colored"] }

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub type CEnumConstantId = CDeclId; // Enum's need to point to child 'DeclKind::
4747
pub struct TypedAstContext {
4848
main_file: PathBuf,
4949

50-
c_types: HashMap<CTypeId, CType>,
50+
c_types: dashmap::DashMap<CTypeId, CType>,
5151
c_exprs: HashMap<CExprId, CExpr>,
5252
c_stmts: HashMap<CStmtId, CStmt>,
5353

@@ -1050,11 +1050,16 @@ impl TypedAstContext {
10501050
use SomeId::*;
10511051
match some_id {
10521052
Type(type_id) => {
1053-
if let CTypeKind::Elaborated(decl_type_id) = self.c_types[&type_id].kind {
1053+
if let CTypeKind::Elaborated(decl_type_id) =
1054+
self.c_types.get(&type_id).unwrap().kind
1055+
{
10541056
// This is a reference to a previously declared type. If we look
10551057
// through it we should(?) get something that looks like a declaration,
10561058
// which we can mark as wanted.
1057-
let decl_id = self.c_types[&decl_type_id]
1059+
let decl_id = self
1060+
.c_types
1061+
.get(&decl_type_id)
1062+
.unwrap()
10581063
.kind
10591064
.as_decl_or_typedef()
10601065
.expect("target of CTypeKind::Elaborated isn't a decl?");
@@ -1388,7 +1393,7 @@ impl Index<CTypeId> for TypedAstContext {
13881393
fn index(&self, index: CTypeId) -> &CType {
13891394
match self.c_types.get(&index) {
13901395
None => panic!("Could not find {:?} in TypedAstContext", index),
1391-
Some(ty) => ty,
1396+
Some(ty) => Box::leak(Box::new(ty.clone())),
13921397
}
13931398
}
13941399
}

c2rust-transpile/src/c_ast/print.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,10 +697,10 @@ impl<W: Write> Printer<W> {
697697
let ty = context
698698
.c_types
699699
.get(&type_id)
700-
.map(|l| &l.kind)
700+
.map(|l| l.kind.clone())
701701
.unwrap_or_else(|| panic!("Could not find type with ID {:?}", type_id));
702702
use CTypeKind::*;
703-
match ty {
703+
match &ty {
704704
Pointer(ref qual_ty) => {
705705
self.print_qtype(*qual_ty, None, context)?;
706706
self.writer.write_all(b"*")?;

0 commit comments

Comments
 (0)