Skip to content

Commit 85b1a3a

Browse files
committed
TemplateParameters.num_self_template_params doesn't return Option
1 parent cd37739 commit 85b1a3a

File tree

2 files changed

+39
-50
lines changed

2 files changed

+39
-50
lines changed

src/ir/context.rs

+31-32
Original file line numberDiff line numberDiff line change
@@ -1549,15 +1549,16 @@ impl BindgenContext {
15491549
.and_then(|canon_decl| {
15501550
self.get_resolved_type(&canon_decl).and_then(
15511551
|template_decl_id| {
1552-
template_decl_id.num_self_template_params(self).map(
1553-
|num_params| {
1554-
(
1555-
*canon_decl.cursor(),
1556-
template_decl_id.into(),
1557-
num_params,
1558-
)
1559-
},
1560-
)
1552+
let num_params = template_decl_id.num_self_template_params(self);
1553+
if num_params == 0 {
1554+
None
1555+
} else {
1556+
Some((
1557+
*canon_decl.cursor(),
1558+
template_decl_id.into(),
1559+
num_params,
1560+
))
1561+
}
15611562
},
15621563
)
15631564
})
@@ -1578,15 +1579,16 @@ impl BindgenContext {
15781579
.cloned()
15791580
})
15801581
.and_then(|template_decl| {
1581-
template_decl.num_self_template_params(self).map(
1582-
|num_template_params| {
1583-
(
1584-
*template_decl.decl(),
1585-
template_decl.id(),
1586-
num_template_params,
1587-
)
1588-
},
1589-
)
1582+
let num_template_params = template_decl.num_self_template_params(self);
1583+
if num_template_params == 0 {
1584+
None
1585+
} else {
1586+
Some((
1587+
*template_decl.decl(),
1588+
template_decl.id(),
1589+
num_template_params,
1590+
))
1591+
}
15901592
})
15911593
})
15921594
}
@@ -1633,17 +1635,14 @@ impl BindgenContext {
16331635
) -> Option<TypeId> {
16341636
use clang_sys;
16351637

1636-
let num_expected_args = match self.resolve_type(template)
1637-
.num_self_template_params(self) {
1638-
Some(n) => n,
1639-
None => {
1640-
warn!(
1641-
"Tried to instantiate a template for which we could not \
1642-
determine any template parameters"
1643-
);
1644-
return None;
1645-
}
1646-
};
1638+
let num_expected_args = self.resolve_type(template).num_self_template_params(self);
1639+
if num_expected_args == 0 {
1640+
warn!(
1641+
"Tried to instantiate a template for which we could not \
1642+
determine any template parameters"
1643+
);
1644+
return None;
1645+
}
16471646

16481647
let mut args = vec![];
16491648
let mut found_const_arg = false;
@@ -2650,7 +2649,7 @@ impl TemplateParameters for PartialType {
26502649
vec![]
26512650
}
26522651

2653-
fn num_self_template_params(&self, _ctx: &BindgenContext) -> Option<usize> {
2652+
fn num_self_template_params(&self, _ctx: &BindgenContext) -> usize {
26542653
// Wouldn't it be nice if libclang would reliably give us this
26552654
// information‽
26562655
match self.decl().kind() {
@@ -2669,9 +2668,9 @@ impl TemplateParameters for PartialType {
26692668
};
26702669
clang_sys::CXChildVisit_Continue
26712670
});
2672-
Some(num_params)
2671+
num_params
26732672
}
2674-
_ => None,
2673+
_ => 0,
26752674
}
26762675
}
26772676
}

src/ir/template.rs

+8-18
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ use parse::ClangItemParser;
8181
/// +------+----------------------+--------------------------+------------------------+----
8282
/// |Decl. | self_template_params | num_self_template_params | all_template_parameters| ...
8383
/// +------+----------------------+--------------------------+------------------------+----
84-
/// |Foo | [T, U] | Some(2) | Some([T, U]) | ...
85-
/// |Bar | [V] | Some(1) | Some([T, U, V]) | ...
86-
/// |Inner | [] | None | Some([T, U]) | ...
87-
/// |Lol | [W] | Some(1) | Some([T, U, W]) | ...
88-
/// |Wtf | [X] | Some(1) | Some([T, U, X]) | ...
89-
/// |Qux | [] | None | None | ...
84+
/// |Foo | [T, U] | 2 | Some([T, U]) | ...
85+
/// |Bar | [V] | 1 | Some([T, U, V]) | ...
86+
/// |Inner | [] | 0 | Some([T, U]) | ...
87+
/// |Lol | [W] | 1 | Some([T, U, W]) | ...
88+
/// |Wtf | [X] | 1 | Some([T, U, X]) | ...
89+
/// |Qux | [] | 0 | None | ...
9090
/// +------+----------------------+--------------------------+------------------------+----
9191
///
9292
/// ----+------+-----+----------------------+
@@ -113,18 +113,8 @@ pub trait TemplateParameters {
113113

114114
/// Get the number of free template parameters this template declaration
115115
/// has.
116-
///
117-
/// Implementations *may* return `Some` from this method when
118-
/// `template_params` returns `None`. This is useful when we only have
119-
/// partial information about the template declaration, such as when we are
120-
/// in the middle of parsing it.
121-
fn num_self_template_params(&self, ctx: &BindgenContext) -> Option<usize> {
122-
let len = self.self_template_params(ctx).len();
123-
if len > 0 {
124-
Some(len)
125-
} else {
126-
None
127-
}
116+
fn num_self_template_params(&self, ctx: &BindgenContext) -> usize {
117+
self.self_template_params(ctx).len()
128118
}
129119

130120
/// Get the complete set of template parameters that can affect this

0 commit comments

Comments
 (0)