Skip to content

Commit d40e5ca

Browse files
committed
transpile: Add panicking variant of TypedAstContext::type_for_kind
1 parent 460b3a0 commit d40e5ca

4 files changed

Lines changed: 16 additions & 28 deletions

File tree

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ impl ConversionContext {
511511
for (_decl_id, located_kind) in self.typed_context.c_decls.iter() {
512512
if let kind @ CDeclKind::Function { .. } = &located_kind.kind {
513513
let new_kind = self.typed_context.fn_decl_ty_with_declared_args(kind);
514-
if self.typed_context.type_for_kind(&new_kind).is_none() {
514+
if self.typed_context.try_type_for_kind(&new_kind).is_none() {
515515
// Create and insert fn type
516516
let new_id = CTypeId(self.id_mapper.fresh_id());
517517
self.typed_context
@@ -2203,9 +2203,7 @@ impl ConversionContext {
22032203
}
22042204
};
22052205
log::trace!("Selected kind {kind} for typedef {name}");
2206-
Some(CQualTypeId::new(
2207-
self.typed_context.type_for_kind(&kind).unwrap(),
2208-
))
2206+
Some(CQualTypeId::new(self.typed_context.type_for_kind(&kind)))
22092207
})
22102208
.unwrap_or(typ);
22112209

@@ -2248,9 +2246,7 @@ impl ConversionContext {
22482246
}
22492247
};
22502248
log::trace!("Selected kind {kind} for typedef {name}");
2251-
Some(CQualTypeId::new(
2252-
self.typed_context.type_for_kind(&kind).unwrap(),
2253-
))
2249+
Some(CQualTypeId::new(self.typed_context.type_for_kind(&kind)))
22542250
};
22552251
let file = self
22562252
.typed_context

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -771,12 +771,17 @@ impl TypedAstContext {
771771
ty.map(|ty| (expr_id, ty))
772772
}
773773

774-
pub fn type_for_kind(&self, kind: &CTypeKind) -> Option<CTypeId> {
774+
pub fn try_type_for_kind(&self, kind: &CTypeKind) -> Option<CTypeId> {
775775
self.c_types
776776
.iter()
777777
.find_map(|(id, k)| if kind == &k.kind { Some(*id) } else { None })
778778
}
779779

780+
pub fn type_for_kind(&self, kind: &CTypeKind) -> CTypeId {
781+
self.try_type_for_kind(kind)
782+
.expect("could not find type for CTypeKind::{kind:?}")
783+
}
784+
780785
pub fn resolve_type_id(&self, typ: CTypeId) -> CTypeId {
781786
use CTypeKind::*;
782787
let ty = match self.index(typ).kind {
@@ -875,9 +880,7 @@ impl TypedAstContext {
875880
pub fn fn_declref_ty_with_declared_args(&self, func_expr: CExprId) -> Option<CQualTypeId> {
876881
if let Some(func_decl @ CDeclKind::Function { .. }) = self.fn_declref_decl(func_expr) {
877882
let kind_with_declared_args = self.fn_decl_ty_with_declared_args(func_decl);
878-
let specific_typ = self
879-
.type_for_kind(&kind_with_declared_args)
880-
.unwrap_or_else(|| panic!("no type for kind {kind_with_declared_args:?}"));
883+
let specific_typ = self.type_for_kind(&kind_with_declared_args);
881884
return Some(CQualTypeId::new(specific_typ));
882885
}
883886
None
@@ -1344,10 +1347,7 @@ impl TypedAstContext {
13441347
CUnTypeOp::AlignOf => CTypeKind::Size,
13451348
CUnTypeOp::PreferredAlignOf => CTypeKind::Size,
13461349
};
1347-
let ty = self
1348-
.ast_context
1349-
.type_for_kind(&kind)
1350-
.expect("CTypeKind::Size should be size_t");
1350+
let ty = self.ast_context.type_for_kind(&kind);
13511351
Some(CQualTypeId::new(ty))
13521352
}
13531353
_ => return,
@@ -2062,7 +2062,7 @@ impl CUnOp {
20622062
}
20632063
Not => {
20642064
return ast_context
2065-
.type_for_kind(&CTypeKind::Int)
2065+
.try_type_for_kind(&CTypeKind::Int)
20662066
.map(CQualTypeId::new)
20672067
}
20682068
Real | Imag => {

c2rust-transpile/src/translator/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3149,7 +3149,7 @@ impl<'c> Translation<'c> {
31493149

31503150
if let Some(ty) = self
31513151
.ast_context
3152-
.type_for_kind(&kind_with_declared_args)
3152+
.try_type_for_kind(&kind_with_declared_args)
31533153
.map(CQualTypeId::new)
31543154
{
31553155
let ty = self.convert_type(ty.ctype)?;
@@ -3334,14 +3334,10 @@ impl<'c> Translation<'c> {
33343334
// but the function's declaration will.
33353335
let kind_with_declared_args =
33363336
self.ast_context.fn_decl_ty_with_declared_args(func_decl);
3337-
let func_ty = self
3338-
.ast_context
3339-
.type_for_kind(&kind_with_declared_args)
3340-
.unwrap_or_else(|| panic!("no type for kind {kind_with_declared_args:?}"));
3337+
let func_ty = self.ast_context.type_for_kind(&kind_with_declared_args);
33413338
let func_ptr_ty = self
33423339
.ast_context
3343-
.type_for_kind(&CTypeKind::Pointer(CQualTypeId::new(func_ty)))
3344-
.unwrap_or_else(|| panic!("no type for kind {kind_with_declared_args:?}"));
3340+
.type_for_kind(&CTypeKind::Pointer(CQualTypeId::new(func_ty)));
33453341

33463342
CQualTypeId::new(func_ptr_ty)
33473343
} else {

c2rust-transpile/src/translator/operators.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,11 +642,7 @@ impl<'c> Translation<'c> {
642642

643643
let one_type_id =
644644
if let CTypeKind::Pointer(..) = self.ast_context.resolve_type(arg_type.ctype).kind {
645-
CQualTypeId::new(
646-
self.ast_context
647-
.type_for_kind(&CTypeKind::Int)
648-
.ok_or_else(|| format_err!("couldn't find type for CTypeKind::Int"))?,
649-
)
645+
CQualTypeId::new(self.ast_context.type_for_kind(&CTypeKind::Int))
650646
} else {
651647
arg_type
652648
};

0 commit comments

Comments
 (0)