Skip to content

Commit 24bb8e1

Browse files
author
Aurélien Normand
committed
rework for elem_type() which now returns Option<Type> instead of Type
1 parent 4ab18e6 commit 24bb8e1

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/clang.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,12 @@ impl Type {
673673

674674
/// Given that this type is an array, vector, or complex type, return the
675675
/// type of its elements.
676-
pub fn elem_type(&self) -> Type {
677-
unsafe {
678-
Type { x: clang_getElementType(self.x) }
676+
pub fn elem_type(&self) -> Option<Type> {
677+
let current_type = Type { x: unsafe { clang_getElementType(self.x) } };
678+
if current_type.kind() != CXType_Invalid {
679+
Some(current_type)
680+
} else {
681+
None
679682
}
680683
}
681684

src/ir/ty.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,9 @@ impl Type {
662662
CXType_VariableArray |
663663
CXType_DependentSizedArray |
664664
CXType_IncompleteArray => {
665-
let inner = Item::from_ty(&ty.elem_type(), location, parent_id, ctx)
665+
let inner = Item::from_ty(ty.elem_type().as_ref()
666+
.expect("Not an appropriate type?"),
667+
location, parent_id, ctx)
666668
.expect("Not able to resolve array element?");
667669
TypeKind::Pointer(inner)
668670
}
@@ -694,14 +696,18 @@ impl Type {
694696
// That being said, that should be fixed eventually.
695697
CXType_Vector |
696698
CXType_ConstantArray => {
697-
let inner = Item::from_ty(&ty.elem_type(), location, parent_id, ctx)
699+
let inner = Item::from_ty(ty.elem_type().as_ref()
700+
.expect("Not an appropriate type?"),
701+
location, parent_id, ctx)
698702
.expect("Not able to resolve array element?");
699703
TypeKind::Array(inner, ty.num_elements().unwrap())
700704
}
701705
// A complex number is always a real and an imaginary part, so
702706
// represent that as a two-item array.
703707
CXType_Complex => {
704-
let inner = Item::from_ty(&ty.elem_type(), location, parent_id, ctx)
708+
let inner = Item::from_ty(ty.elem_type().as_ref()
709+
.expect("Not an appropriate type?"),
710+
location, parent_id, ctx)
705711
.expect("Not able to resolve array element?");
706712
TypeKind::Array(inner, 2)
707713
}

0 commit comments

Comments
 (0)