Skip to content

Commit 54256c4

Browse files
committed
TemplateParameters.self_template_params doesn't return Option
1 parent 8fe4d63 commit 54256c4

File tree

8 files changed

+47
-56
lines changed

8 files changed

+47
-56
lines changed

src/codegen/mod.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -3138,18 +3138,16 @@ impl TryToRustTy for TemplateInstantiation {
31383138
let def_path = def.namespace_aware_canonical_path(ctx);
31393139
ty.append_separated(def_path.into_iter().map(|p| ctx.rust_ident(p)), Term::new("::", Span::call_site()));
31403140

3141-
let def_params = match def.self_template_params(ctx) {
3142-
Some(params) => params,
3143-
None => {
3144-
// This can happen if we generated an opaque type for a partial
3145-
// template specialization, and we've hit an instantiation of
3146-
// that partial specialization.
3147-
extra_assert!(
3148-
def.is_opaque(ctx, &())
3149-
);
3150-
return Err(error::Error::InstantiationOfOpaqueType);
3151-
}
3152-
};
3141+
let def_params = def.self_template_params(ctx);
3142+
if def_params.is_empty() {
3143+
// This can happen if we generated an opaque type for a partial
3144+
// template specialization, and we've hit an instantiation of
3145+
// that partial specialization.
3146+
extra_assert!(
3147+
def.is_opaque(ctx, &())
3148+
);
3149+
return Err(error::Error::InstantiationOfOpaqueType);
3150+
}
31533151

31543152
// TODO: If the definition type is a template class/struct
31553153
// definition's member template definition, it could rely on

src/ir/analysis/derive_copy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> {
246246
}
247247

248248
// https://github.com/rust-lang/rust/issues/36640
249-
if info.self_template_params(self.ctx).is_some() ||
249+
if !info.self_template_params(self.ctx).is_empty() ||
250250
item.used_template_params(self.ctx).is_some()
251251
{
252252
trace!(

src/ir/analysis/template_params.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl<'ctx> UsedTemplateParameters<'ctx> {
275275
let decl = self.ctx.resolve_type(instantiation.template_definition());
276276
let args = instantiation.template_arguments();
277277

278-
let params = decl.self_template_params(self.ctx).unwrap_or(vec![]);
278+
let params = decl.self_template_params(self.ctx);
279279

280280
debug_assert!(this_id != instantiation.template_definition());
281281
let used_by_def = self.used
@@ -419,7 +419,7 @@ impl<'ctx> MonotoneFramework for UsedTemplateParameters<'ctx> {
419419
// template parameters, there is a single exception:
420420
// opaque templates. Hence the unwrap_or.
421421
let params =
422-
decl.self_template_params(ctx).unwrap_or(vec![]);
422+
decl.self_template_params(ctx);
423423

424424
for (arg, param) in args.iter().zip(params.iter()) {
425425
let arg = arg.into_resolver()

src/ir/comp.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1669,12 +1669,8 @@ impl TemplateParameters for CompInfo {
16691669
fn self_template_params(
16701670
&self,
16711671
_ctx: &BindgenContext,
1672-
) -> Option<Vec<TypeId>> {
1673-
if self.template_params.is_empty() {
1674-
None
1675-
} else {
1676-
Some(self.template_params.clone())
1677-
}
1672+
) -> Vec<TypeId> {
1673+
self.template_params.clone()
16781674
}
16791675
}
16801676

src/ir/context.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1366,10 +1366,7 @@ impl BindgenContext {
13661366
let mut used_params = HashMap::new();
13671367
for &id in self.whitelisted_items() {
13681368
used_params.entry(id).or_insert(
1369-
id.self_template_params(self).map_or(
1370-
Default::default(),
1371-
|params| params.into_iter().map(|p| p.into()).collect(),
1372-
),
1369+
id.self_template_params(self).into_iter().map(|p| p.into()).collect()
13731370
);
13741371
}
13751372
self.used_template_parameters = Some(used_params);
@@ -2647,10 +2644,10 @@ impl TemplateParameters for PartialType {
26472644
fn self_template_params(
26482645
&self,
26492646
_ctx: &BindgenContext,
2650-
) -> Option<Vec<TypeId>> {
2647+
) -> Vec<TypeId> {
26512648
// Maybe at some point we will eagerly parse named types, but for now we
26522649
// don't and this information is unavailable.
2653-
None
2650+
vec![]
26542651
}
26552652

26562653
fn num_self_template_params(&self, _ctx: &BindgenContext) -> Option<usize> {

src/ir/item.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1112,8 +1112,8 @@ where
11121112
fn self_template_params(
11131113
&self,
11141114
ctx: &BindgenContext,
1115-
) -> Option<Vec<TypeId>> {
1116-
ctx.resolve_item_fallible(*self).and_then(|item| {
1115+
) -> Vec<TypeId> {
1116+
ctx.resolve_item_fallible(*self).map_or(vec![], |item| {
11171117
item.self_template_params(ctx)
11181118
})
11191119
}
@@ -1123,7 +1123,7 @@ impl TemplateParameters for Item {
11231123
fn self_template_params(
11241124
&self,
11251125
ctx: &BindgenContext,
1126-
) -> Option<Vec<TypeId>> {
1126+
) -> Vec<TypeId> {
11271127
self.kind.self_template_params(ctx)
11281128
}
11291129
}
@@ -1132,15 +1132,15 @@ impl TemplateParameters for ItemKind {
11321132
fn self_template_params(
11331133
&self,
11341134
ctx: &BindgenContext,
1135-
) -> Option<Vec<TypeId>> {
1135+
) -> Vec<TypeId> {
11361136
match *self {
11371137
ItemKind::Type(ref ty) => ty.self_template_params(ctx),
11381138
// If we start emitting bindings to explicitly instantiated
11391139
// functions, then we'll need to check ItemKind::Function for
11401140
// template params.
11411141
ItemKind::Function(_) |
11421142
ItemKind::Module(_) |
1143-
ItemKind::Var(_) => None,
1143+
ItemKind::Var(_) => vec![],
11441144
}
11451145
}
11461146
}

src/ir/template.rs

+20-20
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 | Some([T, U]) | Some(2) | Some([T, U]) | ...
85-
/// |Bar | Some([V]) | Some(1) | Some([T, U, V]) | ...
86-
/// |Inner | None | None | Some([T, U]) | ...
87-
/// |Lol | Some([W]) | Some(1) | Some([T, U, W]) | ...
88-
/// |Wtf | Some([X]) | Some(1) | Some([T, U, X]) | ...
89-
/// |Qux | None | None | None | ...
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 | ...
9090
/// +------+----------------------+--------------------------+------------------------+----
9191
///
9292
/// ----+------+-----+----------------------+
@@ -109,7 +109,7 @@ pub trait TemplateParameters {
109109
/// anything but types, so we must treat them as opaque, and avoid
110110
/// instantiating them.
111111
fn self_template_params(&self, ctx: &BindgenContext)
112-
-> Option<Vec<TypeId>>;
112+
-> Vec<TypeId>;
113113

114114
/// Get the number of free template parameters this template declaration
115115
/// has.
@@ -119,7 +119,12 @@ pub trait TemplateParameters {
119119
/// partial information about the template declaration, such as when we are
120120
/// in the middle of parsing it.
121121
fn num_self_template_params(&self, ctx: &BindgenContext) -> Option<usize> {
122-
self.self_template_params(ctx).map(|params| params.len())
122+
let len = self.self_template_params(ctx).len();
123+
if len > 0 {
124+
Some(len)
125+
} else {
126+
None
127+
}
123128
}
124129

125130
/// Get the complete set of template parameters that can affect this
@@ -140,19 +145,14 @@ pub trait TemplateParameters {
140145
where
141146
Self: ItemAncestors,
142147
{
143-
let each_self_params: Vec<Vec<_>> = self.ancestors(ctx)
144-
.filter_map(|id| id.self_template_params(ctx))
145-
.collect();
146-
if each_self_params.is_empty() {
147-
None
148+
let ancestors: Vec<_> = self.ancestors(ctx).collect();
149+
let all_template_params: Vec<_> = ancestors.into_iter().rev().flat_map(|id| {
150+
id.self_template_params(ctx).into_iter()
151+
}).collect();
152+
if all_template_params.len() > 0 {
153+
Some(all_template_params)
148154
} else {
149-
Some(
150-
each_self_params
151-
.into_iter()
152-
.rev()
153-
.flat_map(|params| params)
154-
.collect(),
155-
)
155+
None
156156
}
157157
}
158158

src/ir/ty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ impl TemplateParameters for Type {
540540
fn self_template_params(
541541
&self,
542542
ctx: &BindgenContext,
543-
) -> Option<Vec<TypeId>> {
543+
) -> Vec<TypeId> {
544544
self.kind.self_template_params(ctx)
545545
}
546546
}
@@ -549,13 +549,13 @@ impl TemplateParameters for TypeKind {
549549
fn self_template_params(
550550
&self,
551551
ctx: &BindgenContext,
552-
) -> Option<Vec<TypeId>> {
552+
) -> Vec<TypeId> {
553553
match *self {
554554
TypeKind::ResolvedTypeRef(id) => {
555555
ctx.resolve_type(id).self_template_params(ctx)
556556
}
557557
TypeKind::Comp(ref comp) => comp.self_template_params(ctx),
558-
TypeKind::TemplateAlias(_, ref args) => Some(args.clone()),
558+
TypeKind::TemplateAlias(_, ref args) => args.clone(),
559559

560560
TypeKind::Opaque |
561561
TypeKind::TemplateInstantiation(..) |
@@ -575,7 +575,7 @@ impl TemplateParameters for TypeKind {
575575
TypeKind::Alias(_) |
576576
TypeKind::ObjCId |
577577
TypeKind::ObjCSel |
578-
TypeKind::ObjCInterface(_) => None,
578+
TypeKind::ObjCInterface(_) => vec![],
579579
}
580580
}
581581
}

0 commit comments

Comments
 (0)