Skip to content

Commit 724aa06

Browse files
author
bors-servo
authored
Auto merge of rust-lang#171 - jeanphilippeD:issue135, r=emilio
clang::Type::num_template_args return Option<u32> fix rust-lang#135 Fix rust-lang#135
2 parents a960fb8 + 7cc08b1 commit 724aa06

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

src/clang.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -645,19 +645,22 @@ impl Type {
645645
}
646646

647647
/// If this type is a class template specialization, return its number of
648-
/// template arguments. Otherwise, return -1.
649-
pub fn num_template_args(&self) -> c_int {
650-
unsafe {
651-
clang_Type_getNumTemplateArguments(self.x)
648+
/// template arguments. Otherwise, return None.
649+
pub fn num_template_args(&self) -> Option<u32> {
650+
let n = unsafe { clang_Type_getNumTemplateArguments(self.x) };
651+
if n >= 0 {
652+
Some(n as u32)
653+
} else {
654+
debug_assert_eq!(n, -1);
655+
None
652656
}
653657
}
654658

655659
/// Get the type of the `i`th template argument for this template
656660
/// specialization.
657-
pub fn template_arg_type(&self, i: c_int) -> Type {
658-
unsafe {
659-
Type { x: clang_Type_getTemplateArgumentAsType(self.x, i) }
660-
}
661+
pub fn template_arg_type(&self, i: u32) -> Type {
662+
let n = i as c_int;
663+
Type { x: unsafe { clang_Type_getTemplateArgumentAsType(self.x, n) } }
661664
}
662665

663666
/// Given that this type is a pointer type, return the type that it points

src/ir/comp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,8 @@ impl CompInfo {
492492
ci.template_args = match ty.num_template_args() {
493493
// In forward declarations and not specializations, etc, they are in
494494
// the ast, we'll meet them in CXCursor_TemplateTypeParameter
495-
-1 => vec![],
496-
len => {
495+
None => vec![],
496+
Some(len) => {
497497
let mut list = Vec::with_capacity(len as usize);
498498
for i in 0..len {
499499
let arg_type = ty.template_arg_type(i);

src/ir/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ impl Type {
506506
TypeKind::Function(signature)
507507
// Same here, with template specialisations we can safely assume
508508
// this is a Comp(..)
509-
} else if ty.num_template_args() > 0 {
509+
} else if ty.num_template_args().unwrap_or(0) > 0 {
510510
debug!("Template specialization: {:?}", ty);
511511
let complex =
512512
CompInfo::from_ty(potential_id, ty, location, ctx)

0 commit comments

Comments
 (0)