Skip to content

Commit daa9ab7

Browse files
author
bors-servo
authored
Auto merge of rust-lang#178 - Incognitas:master, r=fitzgen
Fix for rust-lang#138 : rework for elem_type() which now returns Option<Type> instead of Type Fixes rust-lang#138 Since I am new to Rust, I hope I didn't do things too badly
2 parents 63fc240 + 24bb8e1 commit daa9ab7

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
@@ -663,7 +663,9 @@ impl Type {
663663
CXType_VariableArray |
664664
CXType_DependentSizedArray |
665665
CXType_IncompleteArray => {
666-
let inner = Item::from_ty(&ty.elem_type(), location, parent_id, ctx)
666+
let inner = Item::from_ty(ty.elem_type().as_ref()
667+
.expect("Not an appropriate type?"),
668+
location, parent_id, ctx)
667669
.expect("Not able to resolve array element?");
668670
TypeKind::Pointer(inner)
669671
}
@@ -695,14 +697,18 @@ impl Type {
695697
// That being said, that should be fixed eventually.
696698
CXType_Vector |
697699
CXType_ConstantArray => {
698-
let inner = Item::from_ty(&ty.elem_type(), location, parent_id, ctx)
700+
let inner = Item::from_ty(ty.elem_type().as_ref()
701+
.expect("Not an appropriate type?"),
702+
location, parent_id, ctx)
699703
.expect("Not able to resolve array element?");
700704
TypeKind::Array(inner, ty.num_elements().unwrap())
701705
}
702706
// A complex number is always a real and an imaginary part, so
703707
// represent that as a two-item array.
704708
CXType_Complex => {
705-
let inner = Item::from_ty(&ty.elem_type(), location, parent_id, ctx)
709+
let inner = Item::from_ty(ty.elem_type().as_ref()
710+
.expect("Not an appropriate type?"),
711+
location, parent_id, ctx)
706712
.expect("Not able to resolve array element?");
707713
TypeKind::Array(inner, 2)
708714
}

0 commit comments

Comments
 (0)