Skip to content

Commit 3e8f3b4

Browse files
Change return type of TemplateParameters' methods from Option to just Vec
1 parent 8582a90 commit 3e8f3b4

File tree

8 files changed

+145
-150
lines changed

8 files changed

+145
-150
lines changed

src/codegen/mod.rs

+57-55
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,21 @@ impl AppendImplicitTemplateParams for quote::Tokens {
284284
_ => {},
285285
}
286286

287-
if let Some(params) = item.used_template_params(ctx) {
288-
if params.is_empty() {
289-
return;
290-
}
287+
let params = item.used_template_params(ctx);
291288

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

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

471470
let ty = self.ty().to_rust_ty_or_opaque(ctx, &());
@@ -619,15 +618,15 @@ impl CodeGenerator for Type {
619618
return;
620619
}
621620

622-
let mut outer_params = item.used_template_params(ctx)
623-
.and_then(|ps| if ps.is_empty() {
624-
None
621+
let mut outer_params =
622+
if !item.used_template_params(ctx).is_empty() {
623+
item.used_template_params(ctx).clone()
625624
} else {
626-
Some(ps)
627-
});
625+
Vec::new()
626+
};
628627

629628
let inner_rust_type = if item.is_opaque(ctx, &()) {
630-
outer_params = None;
629+
outer_params = Vec::new();
631630
self.to_opaque(ctx, item)
632631
} else {
633632
// Its possible that we have better layout information than
@@ -682,7 +681,7 @@ impl CodeGenerator for Type {
682681
'A'...'Z' | 'a'...'z' | '0'...'9' | ':' | '_' | ' ' => true,
683682
_ => false,
684683
}) &&
685-
outer_params.is_none() &&
684+
outer_params.is_empty() &&
686685
inner_item.expect_type().canonical_type(ctx).is_enum()
687686
{
688687
tokens.append(quote! {
@@ -701,8 +700,8 @@ impl CodeGenerator for Type {
701700
pub type #rust_name
702701
});
703702

704-
if let Some(params) = outer_params {
705-
let params: Vec<_> = params.into_iter()
703+
if !outer_params.is_empty() {
704+
let params: Vec<_> = outer_params.into_iter()
706705
.filter_map(|p| p.as_template_param(ctx, &()))
707706
.collect();
708707
if params.iter().any(|p| ctx.resolve_type(*p).is_invalid_type_param()) {
@@ -1401,7 +1400,7 @@ impl CodeGenerator for CompInfo {
14011400
//
14021401
// NB: We generate a proper struct to avoid struct/function name
14031402
// collisions.
1404-
if self.is_forward_declaration() && used_template_params.is_none() {
1403+
if self.is_forward_declaration() && used_template_params.is_empty() {
14051404
let struct_name = item.canonical_name(ctx);
14061405
let struct_name = ctx.rust_ident_raw(struct_name);
14071406
let tuple_struct = quote! {
@@ -1584,7 +1583,9 @@ impl CodeGenerator for CompInfo {
15841583

15851584
let mut generic_param_names = vec![];
15861585

1587-
if let Some(ref params) = used_template_params {
1586+
let ref params = used_template_params;
1587+
1588+
if !params.is_empty() {
15881589
for (idx, ty) in params.iter().enumerate() {
15891590
let param = ctx.resolve_type(*ty);
15901591
let name = param.name().unwrap();
@@ -1594,13 +1595,14 @@ impl CodeGenerator for CompInfo {
15941595
let prefix = ctx.trait_prefix();
15951596
let field_name = ctx.rust_ident(format!("_phantom_{}", idx));
15961597
fields.push(quote! {
1597-
pub #field_name : ::#prefix::marker::PhantomData<
1598-
::#prefix::cell::UnsafeCell<#ident>
1599-
> ,
1600-
});
1598+
pub #field_name : ::#prefix::marker::PhantomData<
1599+
::#prefix::cell::UnsafeCell<#ident>
1600+
> ,
1601+
});
16011602
}
16021603
}
16031604

1605+
16041606
let generics = if !generic_param_names.is_empty() {
16051607
let generic_param_names = generic_param_names.clone();
16061608
quote! {
@@ -1642,7 +1644,7 @@ impl CodeGenerator for CompInfo {
16421644
ctx.options().derive_copy
16431645
{
16441646
derives.push("Copy");
1645-
if used_template_params.is_some() {
1647+
if !used_template_params.is_empty() {
16461648
// FIXME: This requires extra logic if you have a big array in a
16471649
// templated struct. The reason for this is that the magic:
16481650
// fn clone(&self) -> Self { *self }
@@ -1726,7 +1728,7 @@ impl CodeGenerator for CompInfo {
17261728
);
17271729
}
17281730

1729-
if used_template_params.is_none() {
1731+
if used_template_params.is_empty() {
17301732
if !is_opaque {
17311733
for var in self.inner_vars() {
17321734
ctx.resolve_item(*var).codegen(ctx, result, &());
@@ -2928,10 +2930,10 @@ impl TryToRustTy for Type {
29282930
TypeKind::TemplateAlias(..) |
29292931
TypeKind::Alias(..) => {
29302932
let template_params = item.used_template_params(ctx)
2931-
.unwrap_or(vec![])
2932-
.into_iter()
2933-
.filter(|param| param.is_template_param(ctx, &()))
2934-
.collect::<Vec<_>>();
2933+
2934+
.into_iter()
2935+
.filter(|param| param.is_template_param(ctx, &()))
2936+
.collect::<Vec<_>>();
29352937

29362938
let spelling = self.name().expect("Unnamed alias?");
29372939
if item.is_opaque(ctx, &()) && !template_params.is_empty() {
@@ -2949,7 +2951,7 @@ impl TryToRustTy for Type {
29492951
TypeKind::Comp(ref info) => {
29502952
let template_params = item.used_template_params(ctx);
29512953
if info.has_non_type_template_params() ||
2952-
(item.is_opaque(ctx, &()) && template_params.is_some())
2954+
(item.is_opaque(ctx, &()) && !template_params.is_empty())
29532955
{
29542956
return self.try_to_opaque(ctx, item);
29552957
}
@@ -3043,18 +3045,18 @@ impl TryToRustTy for TemplateInstantiation {
30433045
let def_path = def.namespace_aware_canonical_path(ctx);
30443046
ty.append_separated(def_path.into_iter().map(|p| ctx.rust_ident(p)), "::");
30453047

3046-
let def_params = match def.self_template_params(ctx) {
3047-
Some(params) => params,
3048-
None => {
3049-
// This can happen if we generated an opaque type for a partial
3050-
// template specialization, and we've hit an instantiation of
3051-
// that partial specialization.
3052-
extra_assert!(
3053-
def.is_opaque(ctx, &())
3054-
);
3055-
return Err(error::Error::InstantiationOfOpaqueType);
3056-
}
3057-
};
3048+
let def_params = def.self_template_params(ctx);
3049+
3050+
if def_params.is_empty() {
3051+
// This can happen if we generated an opaque type for a partial
3052+
// template specialization, and we've hit an instantiation of
3053+
// that partial specialization.
3054+
extra_assert!(
3055+
def.is_opaque(ctx, &())
3056+
);
3057+
return Err(error::Error::InstantiationOfOpaqueType);
3058+
3059+
}
30583060

30593061
// TODO: If the definition type is a template class/struct
30603062
// definition's member template definition, it could rely on
@@ -3132,12 +3134,12 @@ impl CodeGenerator for Function {
31323134
// instantiations is open ended and we have no way of knowing which
31333135
// monomorphizations actually exist.
31343136
let type_params = item.all_template_params(ctx);
3135-
if let Some(params) = type_params {
3136-
if !params.is_empty() {
3137-
return;
3138-
}
3137+
3138+
if !type_params.is_empty() {
3139+
return;
31393140
}
31403141

3142+
31413143
let name = self.name();
31423144
let mut canonical_name = item.canonical_name(ctx);
31433145
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
@@ -1585,12 +1585,8 @@ impl TemplateParameters for CompInfo {
15851585
fn self_template_params(
15861586
&self,
15871587
_ctx: &BindgenContext,
1588-
) -> Option<Vec<TypeId>> {
1589-
if self.template_params.is_empty() {
1590-
None
1591-
} else {
1592-
Some(self.template_params.clone())
1593-
}
1588+
) -> Vec<TypeId> {
1589+
self.template_params.clone()
15941590
}
15951591
}
15961592

@@ -1601,7 +1597,7 @@ impl Trace for CompInfo {
16011597
where
16021598
T: Tracer,
16031599
{
1604-
let params = item.all_template_params(context).unwrap_or(vec![]);
1600+
let params = item.all_template_params(context);
16051601
for p in params {
16061602
tracer.visit_kind(p.into(), EdgeKind::TemplateParameterDefinition);
16071603
}

src/ir/context.rs

+37-35
Original file line numberDiff line numberDiff line change
@@ -1306,10 +1306,14 @@ impl BindgenContext {
13061306
let mut used_params = HashMap::new();
13071307
for &id in self.whitelisted_items() {
13081308
used_params.entry(id).or_insert(
1309-
id.self_template_params(self).map_or(
1310-
Default::default(),
1311-
|params| params.into_iter().map(|p| p.into()).collect(),
1312-
),
1309+
if !id.self_template_params(self).is_empty() {
1310+
id.self_template_params(self)
1311+
.into_iter()
1312+
.map(|p| p.into())
1313+
.collect()
1314+
} else {
1315+
Default::default()
1316+
}
13131317
);
13141318
}
13151319
self.used_template_parameters = Some(used_params);
@@ -1492,14 +1496,13 @@ impl BindgenContext {
14921496
.and_then(|canon_decl| {
14931497
self.get_resolved_type(&canon_decl).and_then(
14941498
|template_decl_id| {
1495-
template_decl_id.num_self_template_params(self).map(
1496-
|num_params| {
1497-
(
1498-
*canon_decl.cursor(),
1499-
template_decl_id.into(),
1500-
num_params,
1501-
)
1502-
},
1499+
Some(
1500+
(
1501+
*canon_decl.cursor(),
1502+
template_decl_id.into(),
1503+
template_decl_id
1504+
.num_self_template_params(self),
1505+
)
15031506
)
15041507
},
15051508
)
@@ -1521,14 +1524,13 @@ impl BindgenContext {
15211524
.cloned()
15221525
})
15231526
.and_then(|template_decl| {
1524-
template_decl.num_self_template_params(self).map(
1525-
|num_template_params| {
1526-
(
1527-
*template_decl.decl(),
1528-
template_decl.id(),
1529-
num_template_params,
1530-
)
1531-
},
1527+
Some(
1528+
(
1529+
*template_decl.decl(),
1530+
template_decl.id(),
1531+
template_decl
1532+
.num_self_template_params(self),
1533+
)
15321534
)
15331535
})
15341536
})
@@ -1576,16 +1578,16 @@ impl BindgenContext {
15761578
) -> Option<TypeId> {
15771579
use clang_sys;
15781580

1579-
let num_expected_args = match self.resolve_type(template)
1580-
.num_self_template_params(self) {
1581-
Some(n) => n,
1582-
None => {
1583-
warn!(
1584-
"Tried to instantiate a template for which we could not \
1585-
determine any template parameters"
1586-
);
1587-
return None;
1588-
}
1581+
let num_expected_args = self.resolve_type(template)
1582+
.num_self_template_params(self);
1583+
1584+
if num_expected_args == 0 {
1585+
warn!(
1586+
"Tried to instantiate a template for which we could not \
1587+
determine any template parameters"
1588+
);
1589+
1590+
return None
15891591
};
15901592

15911593
let mut args = vec![];
@@ -2562,13 +2564,13 @@ impl TemplateParameters for PartialType {
25622564
fn self_template_params(
25632565
&self,
25642566
_ctx: &BindgenContext,
2565-
) -> Option<Vec<TypeId>> {
2567+
) -> Vec<TypeId> {
25662568
// Maybe at some point we will eagerly parse named types, but for now we
25672569
// don't and this information is unavailable.
2568-
None
2570+
Vec::new()
25692571
}
25702572

2571-
fn num_self_template_params(&self, _ctx: &BindgenContext) -> Option<usize> {
2573+
fn num_self_template_params(&self, _ctx: &BindgenContext) -> usize {
25722574
// Wouldn't it be nice if libclang would reliably give us this
25732575
// information‽
25742576
match self.decl().kind() {
@@ -2587,9 +2589,9 @@ impl TemplateParameters for PartialType {
25872589
};
25882590
clang_sys::CXChildVisit_Continue
25892591
});
2590-
Some(num_params)
2592+
num_params
25912593
}
2592-
_ => None,
2594+
_ => 0,
25932595
}
25942596
}
25952597
}

0 commit comments

Comments
 (0)