Skip to content

Commit 65c0865

Browse files
committed
transpile: mint new CTypeIds when necessary in TypedAstContext::type_for_kind
1 parent f85ace1 commit 65c0865

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

  • c2rust-transpile/src/c_ast

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,26 @@ impl TypedAstContext {
716716
}
717717

718718
pub fn type_for_kind(&self, kind: &CTypeKind) -> Option<CTypeId> {
719-
self.c_types
719+
let ro = self.c_types.clone().into_read_only();
720+
if let Some(ty) = ro
720721
.iter()
721722
.find_map(|(id, k)| if kind == &k.kind { Some(*id) } else { None })
723+
{
724+
return Some(ty);
725+
}
726+
let max_type_id = ro
727+
.keys()
728+
.max_by_key(|id| id.0)
729+
.expect("expected non-empty AstContext::c_types");
730+
let new_id = CTypeId(max_type_id.0 + 1);
731+
self.c_types.insert(
732+
new_id,
733+
Located {
734+
loc: None,
735+
kind: kind.clone(),
736+
},
737+
);
738+
Some(new_id)
722739
}
723740

724741
pub fn resolve_type_id(&self, typ: CTypeId) -> CTypeId {

0 commit comments

Comments
 (0)