Skip to content

Commit f6bb89e

Browse files
Change return type of TemplateParameters' methods from Option to just Vec
1 parent 4e74c39 commit f6bb89e

File tree

8 files changed

+148
-153
lines changed

8 files changed

+148
-153
lines changed

src/codegen/mod.rs

+60-58
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,21 @@ impl AppendImplicitTemplateParams for quote::Tokens {
282282
_ => {},
283283
}
284284

285-
if let Some(params) = item.used_template_params(ctx) {
286-
if params.is_empty() {
287-
return;
288-
}
285+
let params = item.used_template_params(ctx);
289286

290-
let params = params.into_iter().map(|p| {
291-
p.try_to_rust_ty(ctx, &())
292-
.expect("template params cannot fail to be a rust type")
293-
});
294-
295-
self.append(quote! {
296-
< #( #params ),* >
297-
});
287+
if params.is_empty() {
288+
return;
298289
}
290+
291+
let params = params.into_iter().map(|p| {
292+
p.try_to_rust_ty(ctx, &())
293+
.expect("template params cannot fail to be a rust type")
294+
});
295+
296+
self.append(quote! {
297+
< #( #params ),* >
298+
});
299+
299300
}
300301
}
301302

@@ -459,11 +460,9 @@ impl CodeGenerator for Var {
459460
// We can't generate bindings to static variables of templates. The
460461
// number of actual variables for a single declaration are open ended
461462
// and we don't know what instantiations do or don't exist.
462-
let type_params = item.all_template_params(ctx);
463-
if let Some(params) = type_params {
464-
if !params.is_empty() {
465-
return;
466-
}
463+
464+
if !item.all_template_params(ctx).is_empty() {
465+
return
467466
}
468467

469468
let ty = self.ty().to_rust_ty_or_opaque(ctx, &());
@@ -605,15 +604,15 @@ impl CodeGenerator for Type {
605604
return;
606605
}
607606

608-
let mut outer_params = item.used_template_params(ctx)
609-
.and_then(|ps| if ps.is_empty() {
610-
None
607+
let mut outer_params =
608+
if !item.used_template_params(ctx).is_empty() {
609+
item.used_template_params(ctx).clone()
611610
} else {
612-
Some(ps)
613-
});
611+
Vec::new()
612+
};
614613

615614
let inner_rust_type = if item.is_opaque(ctx, &()) {
616-
outer_params = None;
615+
outer_params = Vec::new();
617616
self.to_opaque(ctx, item)
618617
} else {
619618
// Its possible that we have better layout information than
@@ -668,7 +667,7 @@ impl CodeGenerator for Type {
668667
'A'...'Z' | 'a'...'z' | '0'...'9' | ':' | '_' | ' ' => true,
669668
_ => false,
670669
}) &&
671-
outer_params.is_none() &&
670+
outer_params.is_empty() &&
672671
inner_item.expect_type().canonical_type(ctx).is_enum()
673672
{
674673
tokens.append(quote! {
@@ -687,8 +686,8 @@ impl CodeGenerator for Type {
687686
pub type #rust_name
688687
});
689688

690-
if let Some(params) = outer_params {
691-
let params: Vec<_> = params.into_iter()
689+
if !outer_params.is_empty() {
690+
let params: Vec<_> = outer_params.into_iter()
692691
.filter_map(|p| p.as_template_param(ctx, &()))
693692
.collect();
694693
if params.iter().any(|p| ctx.resolve_type(*p).is_invalid_type_param()) {
@@ -1405,7 +1404,7 @@ impl CodeGenerator for CompInfo {
14051404
//
14061405
// NB: We generate a proper struct to avoid struct/function name
14071406
// collisions.
1408-
if self.is_forward_declaration() && used_template_params.is_none() {
1407+
if self.is_forward_declaration() && used_template_params.is_empty() {
14091408
let struct_name = item.canonical_name(ctx);
14101409
let struct_name = ctx.rust_ident_raw(struct_name);
14111410
let tuple_struct = quote! {
@@ -1450,7 +1449,7 @@ impl CodeGenerator for CompInfo {
14501449
ctx.options().derive_copy
14511450
{
14521451
derives.push("Copy");
1453-
if used_template_params.is_some() {
1452+
if used_template_params.is_empty() {
14541453
// FIXME: This requires extra logic if you have a big array in a
14551454
// templated struct. The reason for this is that the magic:
14561455
// fn clone(&self) -> Self { *self }
@@ -1478,8 +1477,8 @@ impl CodeGenerator for CompInfo {
14781477
if item.can_derive_partialeq(ctx) {
14791478
derives.push("PartialEq");
14801479
} else {
1481-
needs_partialeq_impl =
1482-
ctx.options().derive_partialeq &&
1480+
needs_partialeq_impl =
1481+
ctx.options().derive_partialeq &&
14831482
ctx.options().impl_partialeq &&
14841483
ctx.lookup_can_derive_partialeq_or_partialord(item.id())
14851484
.map_or(true, |x| {
@@ -1668,7 +1667,9 @@ impl CodeGenerator for CompInfo {
16681667

16691668
let mut generic_param_names = vec![];
16701669

1671-
if let Some(ref params) = used_template_params {
1670+
let ref params = used_template_params;
1671+
1672+
if !params.is_empty() {
16721673
for (idx, ty) in params.iter().enumerate() {
16731674
let param = ctx.resolve_type(*ty);
16741675
let name = param.name().unwrap();
@@ -1678,13 +1679,14 @@ impl CodeGenerator for CompInfo {
16781679
let prefix = ctx.trait_prefix();
16791680
let field_name = ctx.rust_ident(format!("_phantom_{}", idx));
16801681
fields.push(quote! {
1681-
pub #field_name : ::#prefix::marker::PhantomData<
1682-
::#prefix::cell::UnsafeCell<#ident>
1683-
> ,
1684-
});
1682+
pub #field_name : ::#prefix::marker::PhantomData<
1683+
::#prefix::cell::UnsafeCell<#ident>
1684+
> ,
1685+
});
16851686
}
16861687
}
16871688

1689+
16881690
let generics = if !generic_param_names.is_empty() {
16891691
let generic_param_names = generic_param_names.clone();
16901692
quote! {
@@ -1721,7 +1723,7 @@ impl CodeGenerator for CompInfo {
17211723
);
17221724
}
17231725

1724-
if used_template_params.is_none() {
1726+
if used_template_params.is_empty() {
17251727
if !is_opaque {
17261728
for var in self.inner_vars() {
17271729
ctx.resolve_item(*var).codegen(ctx, result, &());
@@ -1898,7 +1900,7 @@ impl CodeGenerator for CompInfo {
18981900

18991901
if needs_partialeq_impl {
19001902
if let Some(impl_) = impl_partialeq::gen_partialeq_impl(ctx, self, item, &ty_for_impl) {
1901-
1903+
19021904
let partialeq_bounds = if !generic_param_names.is_empty() {
19031905
let bounds = generic_param_names.iter().map(|t| {
19041906
quote! { #t: PartialEq }
@@ -2919,10 +2921,10 @@ impl TryToRustTy for Type {
29192921
TypeKind::TemplateAlias(..) |
29202922
TypeKind::Alias(..) => {
29212923
let template_params = item.used_template_params(ctx)
2922-
.unwrap_or(vec![])
2923-
.into_iter()
2924-
.filter(|param| param.is_template_param(ctx, &()))
2925-
.collect::<Vec<_>>();
2924+
2925+
.into_iter()
2926+
.filter(|param| param.is_template_param(ctx, &()))
2927+
.collect::<Vec<_>>();
29262928

29272929
let spelling = self.name().expect("Unnamed alias?");
29282930
if item.is_opaque(ctx, &()) && !template_params.is_empty() {
@@ -2940,7 +2942,7 @@ impl TryToRustTy for Type {
29402942
TypeKind::Comp(ref info) => {
29412943
let template_params = item.used_template_params(ctx);
29422944
if info.has_non_type_template_params() ||
2943-
(item.is_opaque(ctx, &()) && template_params.is_some())
2945+
(item.is_opaque(ctx, &()) && !template_params.is_empty())
29442946
{
29452947
return self.try_to_opaque(ctx, item);
29462948
}
@@ -3034,18 +3036,18 @@ impl TryToRustTy for TemplateInstantiation {
30343036
let def_path = def.namespace_aware_canonical_path(ctx);
30353037
ty.append_separated(def_path.into_iter().map(|p| ctx.rust_ident(p)), "::");
30363038

3037-
let def_params = match def.self_template_params(ctx) {
3038-
Some(params) => params,
3039-
None => {
3040-
// This can happen if we generated an opaque type for a partial
3041-
// template specialization, and we've hit an instantiation of
3042-
// that partial specialization.
3043-
extra_assert!(
3044-
def.is_opaque(ctx, &())
3045-
);
3046-
return Err(error::Error::InstantiationOfOpaqueType);
3047-
}
3048-
};
3039+
let def_params = def.self_template_params(ctx);
3040+
3041+
if def_params.is_empty() {
3042+
// This can happen if we generated an opaque type for a partial
3043+
// template specialization, and we've hit an instantiation of
3044+
// that partial specialization.
3045+
extra_assert!(
3046+
def.is_opaque(ctx, &())
3047+
);
3048+
return Err(error::Error::InstantiationOfOpaqueType);
3049+
3050+
}
30493051

30503052
// TODO: If the definition type is a template class/struct
30513053
// definition's member template definition, it could rely on
@@ -3115,12 +3117,12 @@ impl CodeGenerator for Function {
31153117
// instantiations is open ended and we have no way of knowing which
31163118
// monomorphizations actually exist.
31173119
let type_params = item.all_template_params(ctx);
3118-
if let Some(params) = type_params {
3119-
if !params.is_empty() {
3120-
return;
3121-
}
3120+
3121+
if !type_params.is_empty() {
3122+
return;
31223123
}
31233124

3125+
31243126
let name = self.name();
31253127
let mut canonical_name = item.canonical_name(ctx);
31263128
let mangled_name = self.mangled_name();

src/ir/analysis/derive_copy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> {
241241
}
242242

243243
// https://github.com/rust-lang/rust/issues/36640
244-
if info.self_template_params(self.ctx).is_some() ||
245-
item.used_template_params(self.ctx).is_some()
244+
if !info.self_template_params(self.ctx).is_empty() ||
245+
!item.used_template_params(self.ctx).is_empty()
246246
{
247247
trace!(
248248
" comp cannot derive copy because issue 36640"

src/ir/analysis/template_params.rs

+2-3
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
@@ -418,8 +418,7 @@ impl<'ctx> MonotoneFramework for UsedTemplateParameters<'ctx> {
418418
// Although template definitions should always have
419419
// template parameters, there is a single exception:
420420
// opaque templates. Hence the unwrap_or.
421-
let params =
422-
decl.self_template_params(ctx).unwrap_or(vec![]);
421+
let params = decl.self_template_params(ctx);
423422

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

src/ir/comp.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1489,12 +1489,8 @@ impl TemplateParameters for CompInfo {
14891489
fn self_template_params(
14901490
&self,
14911491
_ctx: &BindgenContext,
1492-
) -> Option<Vec<TypeId>> {
1493-
if self.template_params.is_empty() {
1494-
None
1495-
} else {
1496-
Some(self.template_params.clone())
1497-
}
1492+
) -> Vec<TypeId> {
1493+
self.template_params.clone()
14981494
}
14991495
}
15001496

@@ -1505,7 +1501,7 @@ impl Trace for CompInfo {
15051501
where
15061502
T: Tracer,
15071503
{
1508-
let params = item.all_template_params(context).unwrap_or(vec![]);
1504+
let params = item.all_template_params(context);
15091505
for p in params {
15101506
tracer.visit_kind(p.into(), EdgeKind::TemplateParameterDefinition);
15111507
}

0 commit comments

Comments
 (0)