Skip to content

Commit 3618f55

Browse files
author
bors-servo
authored
Auto merge of rust-lang#161 - upsuper:safe-canonical-type, r=emilio
Use safe_canonical_type for parse time This should fix rust-lang#160. r? @emilio
2 parents f7b5fae + ececf40 commit 3618f55

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/ir/ty.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,10 @@ impl Type {
122122
}
123123

124124
/// Is this an integer type?
125-
pub fn is_integer(&self, ctx: &BindgenContext) -> bool {
125+
pub fn is_integer(&self) -> bool {
126126
match self.kind {
127-
TypeKind::UnresolvedTypeRef(..) => false,
128-
_ => match self.canonical_type(ctx).kind {
129-
TypeKind::Int(..) => true,
130-
_ => false,
131-
}
127+
TypeKind::Int(..) => true,
128+
_ => false,
132129
}
133130
}
134131

@@ -311,12 +308,17 @@ impl Type {
311308
}
312309
}
313310

311+
/// See safe_canonical_type.
312+
pub fn canonical_type<'tr>(&'tr self, ctx: &'tr BindgenContext) -> &'tr Type {
313+
self.safe_canonical_type(ctx).expect("Should have been resolved after parsing!")
314+
}
315+
314316
/// Returns the canonical type of this type, that is, the "inner type".
315317
///
316318
/// For example, for a `typedef`, the canonical type would be the
317319
/// `typedef`ed type, for a template specialization, would be the template
318-
/// its specializing, and so on.
319-
pub fn canonical_type<'tr>(&'tr self, ctx: &'tr BindgenContext) -> &'tr Type {
320+
/// its specializing, and so on. Return None if the type is unresolved.
321+
pub fn safe_canonical_type<'tr>(&'tr self, ctx: &'tr BindgenContext) -> Option<&'tr Type> {
320322
match self.kind {
321323
TypeKind::Named(..) |
322324
TypeKind::Array(..) |
@@ -329,16 +331,15 @@ impl Type {
329331
TypeKind::Void |
330332
TypeKind::NullPtr |
331333
TypeKind::BlockPointer |
332-
TypeKind::Pointer(..) => self,
334+
TypeKind::Pointer(..) => Some(self),
333335

334336
TypeKind::ResolvedTypeRef(inner) |
335337
TypeKind::Alias(_, inner) |
336338
TypeKind::TemplateAlias(inner, _) |
337339
TypeKind::TemplateRef(inner, _)
338-
=> ctx.resolve_type(inner).canonical_type(ctx),
340+
=> ctx.resolve_type(inner).safe_canonical_type(ctx),
339341

340-
TypeKind::UnresolvedTypeRef(..)
341-
=> unreachable!("Should have been resolved after parsing!"),
342+
TypeKind::UnresolvedTypeRef(..) => None,
342343
}
343344
}
344345
}

src/ir/var.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,14 @@ impl ClangSubItemParser for Var {
119119
// tests/headers/inner_const.hpp
120120
//
121121
// That's fine because in that case we know it's not a literal.
122-
let value = context.safe_resolve_type(ty).map_or(None, |t| {
123-
if t.is_integer(context) {
124-
get_integer_literal_from_cursor(&cursor, context.translation_unit())
125-
} else {
126-
None
127-
}
128-
});
122+
let value = context.safe_resolve_type(ty)
123+
.and_then(|t| t.safe_canonical_type(context)).and_then(|t| {
124+
if t.is_integer() {
125+
get_integer_literal_from_cursor(&cursor, context.translation_unit())
126+
} else {
127+
None
128+
}
129+
});
129130

130131
let mangling = cursor_mangling(&cursor);
131132

0 commit comments

Comments
 (0)