Skip to content

Commit b03316a

Browse files
committed
Return Vec on TemplateParameters
The Option<T> returned in this trait was removed to return Vec. Fixes rust-lang#960.
1 parent 3379a90 commit b03316a

17 files changed

+498
-235
lines changed

src/codegen/mod.rs

+44-56
Original file line numberDiff line numberDiff line change
@@ -299,20 +299,20 @@ impl AppendImplicitTemplateParams for quote::Tokens {
299299
_ => {},
300300
}
301301

302-
if let Some(params) = item.used_template_params(ctx) {
303-
if params.is_empty() {
304-
return;
305-
}
302+
let params = item.used_template_params(ctx);
306303

307-
let params = params.into_iter().map(|p| {
308-
p.try_to_rust_ty(ctx, &())
309-
.expect("template params cannot fail to be a rust type")
310-
});
311-
312-
self.append(quote! {
313-
< #( #params ),* >
314-
});
304+
if params.is_empty() {
305+
return;
315306
}
307+
308+
let params = params.into_iter().map(|p| {
309+
p.try_to_rust_ty(ctx, &())
310+
.expect("template params cannot fail to be a rust type")
311+
});
312+
313+
self.append(quote! {
314+
< #( #params ),* >
315+
});
316316
}
317317
}
318318

@@ -480,10 +480,8 @@ impl CodeGenerator for Var {
480480
// number of actual variables for a single declaration are open ended
481481
// and we don't know what instantiations do or don't exist.
482482
let type_params = item.all_template_params(ctx);
483-
if let Some(params) = type_params {
484-
if !params.is_empty() {
485-
return;
486-
}
483+
if !type_params.is_empty() {
484+
return;
487485
}
488486

489487
let ty = self.ty().to_rust_ty_or_opaque(ctx, &());
@@ -636,12 +634,8 @@ impl CodeGenerator for Type {
636634
return;
637635
}
638636

639-
let mut outer_params = item.used_template_params(ctx)
640-
.and_then(|ps| if ps.is_empty() {
641-
None
642-
} else {
643-
Some(ps)
644-
});
637+
let used_params = item.used_template_params(ctx);
638+
let mut outer_params = if used_params.is_empty() { None } else { Some(used_params) };
645639

646640
let inner_rust_type = if item.is_opaque(ctx, &()) {
647641
outer_params = None;
@@ -1597,21 +1591,20 @@ impl CodeGenerator for CompInfo {
15971591

15981592
let mut generic_param_names = vec![];
15991593

1600-
if let Some(ref params) = used_template_params {
1601-
for (idx, ty) in params.iter().enumerate() {
1602-
let param = ctx.resolve_type(*ty);
1603-
let name = param.name().unwrap();
1604-
let ident = ctx.rust_ident(name);
1605-
generic_param_names.push(ident.clone());
16061594

1607-
let prefix = ctx.trait_prefix();
1608-
let field_name = ctx.rust_ident(format!("_phantom_{}", idx));
1609-
fields.push(quote! {
1610-
pub #field_name : ::#prefix::marker::PhantomData<
1611-
::#prefix::cell::UnsafeCell<#ident>
1612-
> ,
1613-
});
1614-
}
1595+
for (idx, ty) in used_template_params.iter().enumerate() {
1596+
let param = ctx.resolve_type(*ty);
1597+
let name = param.name().unwrap();
1598+
let ident = ctx.rust_ident(name);
1599+
generic_param_names.push(ident.clone());
1600+
1601+
let prefix = ctx.trait_prefix();
1602+
let field_name = ctx.rust_ident(format!("_phantom_{}", idx));
1603+
fields.push(quote! {
1604+
pub #field_name : ::#prefix::marker::PhantomData<
1605+
::#prefix::cell::UnsafeCell<#ident>
1606+
> ,
1607+
});
16151608
}
16161609

16171610
let generics = if !generic_param_names.is_empty() {
@@ -1656,7 +1649,7 @@ impl CodeGenerator for CompInfo {
16561649
derives.push("Copy");
16571650

16581651
if ctx.options().rust_features().builtin_clone_impls() ||
1659-
used_template_params.is_some()
1652+
!used_template_params.is_empty()
16601653
{
16611654
// FIXME: This requires extra logic if you have a big array in a
16621655
// templated struct. The reason for this is that the magic:
@@ -1738,7 +1731,7 @@ impl CodeGenerator for CompInfo {
17381731
);
17391732
}
17401733

1741-
if used_template_params.is_none() {
1734+
if used_template_params.is_empty() {
17421735
if !is_opaque {
17431736
for var in self.inner_vars() {
17441737
ctx.resolve_item(*var).codegen(ctx, result, &());
@@ -2993,7 +2986,6 @@ impl TryToRustTy for Type {
29932986
TypeKind::TemplateAlias(..) |
29942987
TypeKind::Alias(..) => {
29952988
let template_params = item.used_template_params(ctx)
2996-
.unwrap_or(vec![])
29972989
.into_iter()
29982990
.filter(|param| param.is_template_param(ctx, &()))
29992991
.collect::<Vec<_>>();
@@ -3014,7 +3006,7 @@ impl TryToRustTy for Type {
30143006
TypeKind::Comp(ref info) => {
30153007
let template_params = item.used_template_params(ctx);
30163008
if info.has_non_type_template_params() ||
3017-
(item.is_opaque(ctx, &()) && template_params.is_some())
3009+
(item.is_opaque(ctx, &()) && !template_params.is_empty())
30183010
{
30193011
return self.try_to_opaque(ctx, item);
30203012
}
@@ -3108,18 +3100,16 @@ impl TryToRustTy for TemplateInstantiation {
31083100
let def_path = def.namespace_aware_canonical_path(ctx);
31093101
ty.append_separated(def_path.into_iter().map(|p| ctx.rust_ident(p)), "::");
31103102

3111-
let def_params = match def.self_template_params(ctx) {
3112-
Some(params) => params,
3113-
None => {
3114-
// This can happen if we generated an opaque type for a partial
3115-
// template specialization, and we've hit an instantiation of
3116-
// that partial specialization.
3117-
extra_assert!(
3118-
def.is_opaque(ctx, &())
3119-
);
3120-
return Err(error::Error::InstantiationOfOpaqueType);
3121-
}
3122-
};
3103+
let def_params = def.self_template_params(ctx);
3104+
if def_params.is_empty() {
3105+
// This can happen if we generated an opaque type for a partial
3106+
// template specialization, and we've hit an instantiation of
3107+
// that partial specialization.
3108+
extra_assert!(
3109+
def.is_opaque(ctx, &())
3110+
);
3111+
return Err(error::Error::InstantiationOfOpaqueType);
3112+
}
31233113

31243114
// TODO: If the definition type is a template class/struct
31253115
// definition's member template definition, it could rely on
@@ -3213,10 +3203,8 @@ impl CodeGenerator for Function {
32133203
// instantiations is open ended and we have no way of knowing which
32143204
// monomorphizations actually exist.
32153205
let type_params = item.all_template_params(ctx);
3216-
if let Some(params) = type_params {
3217-
if !params.is_empty() {
3218-
return;
3219-
}
3206+
if !type_params.is_empty() {
3207+
return;
32203208
}
32213209

32223210
let name = self.name();

src/ir/analysis/derive_copy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ 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() ||
250-
item.used_template_params(self.ctx).is_some()
249+
if !info.self_template_params(self.ctx).is_empty() ||
250+
!item.used_template_params(self.ctx).is_empty()
251251
{
252252
trace!(
253253
" 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
@@ -1668,12 +1668,8 @@ impl TemplateParameters for CompInfo {
16681668
fn self_template_params(
16691669
&self,
16701670
_ctx: &BindgenContext,
1671-
) -> Option<Vec<TypeId>> {
1672-
if self.template_params.is_empty() {
1673-
None
1674-
} else {
1675-
Some(self.template_params.clone())
1676-
}
1671+
) -> Vec<TypeId> {
1672+
self.template_params.clone()
16771673
}
16781674
}
16791675

@@ -1684,7 +1680,7 @@ impl Trace for CompInfo {
16841680
where
16851681
T: Tracer,
16861682
{
1687-
let params = item.all_template_params(context).unwrap_or(vec![]);
1683+
let params = item.all_template_params(context);
16881684
for p in params {
16891685
tracer.visit_kind(p.into(), EdgeKind::TemplateParameterDefinition);
16901686
}

src/ir/context.rs

+21-38
Original file line numberDiff line numberDiff line change
@@ -1342,10 +1342,8 @@ impl BindgenContext {
13421342
let mut used_params = HashMap::new();
13431343
for &id in self.whitelisted_items() {
13441344
used_params.entry(id).or_insert(
1345-
id.self_template_params(self).map_or(
1346-
Default::default(),
1347-
|params| params.into_iter().map(|p| p.into()).collect(),
1348-
),
1345+
id.self_template_params(self)
1346+
.into_iter().map(|p| p.into()).collect()
13491347
);
13501348
}
13511349
self.used_template_parameters = Some(used_params);
@@ -1528,15 +1526,12 @@ impl BindgenContext {
15281526
.and_then(|canon_decl| {
15291527
self.get_resolved_type(&canon_decl).and_then(
15301528
|template_decl_id| {
1531-
template_decl_id.num_self_template_params(self).map(
1532-
|num_params| {
1533-
(
1534-
*canon_decl.cursor(),
1535-
template_decl_id.into(),
1536-
num_params,
1537-
)
1538-
},
1539-
)
1529+
let num_params = template_decl_id.num_self_template_params(self);
1530+
Some((
1531+
*canon_decl.cursor(),
1532+
template_decl_id.into(),
1533+
num_params,
1534+
))
15401535
},
15411536
)
15421537
})
@@ -1557,15 +1552,12 @@ impl BindgenContext {
15571552
.cloned()
15581553
})
15591554
.and_then(|template_decl| {
1560-
template_decl.num_self_template_params(self).map(
1561-
|num_template_params| {
1562-
(
1563-
*template_decl.decl(),
1564-
template_decl.id(),
1565-
num_template_params,
1566-
)
1567-
},
1568-
)
1555+
let num_template_params = template_decl.num_self_template_params(self);
1556+
Some((
1557+
*template_decl.decl(),
1558+
template_decl.id(),
1559+
num_template_params,
1560+
))
15691561
})
15701562
})
15711563
}
@@ -1612,17 +1604,8 @@ impl BindgenContext {
16121604
) -> Option<TypeId> {
16131605
use clang_sys;
16141606

1615-
let num_expected_args = match self.resolve_type(template)
1616-
.num_self_template_params(self) {
1617-
Some(n) => n,
1618-
None => {
1619-
warn!(
1620-
"Tried to instantiate a template for which we could not \
1621-
determine any template parameters"
1622-
);
1623-
return None;
1624-
}
1625-
};
1607+
let num_expected_args = self.resolve_type(template)
1608+
.num_self_template_params(self);
16261609

16271610
let mut args = vec![];
16281611
let mut found_const_arg = false;
@@ -2622,13 +2605,13 @@ impl TemplateParameters for PartialType {
26222605
fn self_template_params(
26232606
&self,
26242607
_ctx: &BindgenContext,
2625-
) -> Option<Vec<TypeId>> {
2608+
) -> Vec<TypeId> {
26262609
// Maybe at some point we will eagerly parse named types, but for now we
26272610
// don't and this information is unavailable.
2628-
None
2611+
vec![]
26292612
}
26302613

2631-
fn num_self_template_params(&self, _ctx: &BindgenContext) -> Option<usize> {
2614+
fn num_self_template_params(&self, _ctx: &BindgenContext) -> usize {
26322615
// Wouldn't it be nice if libclang would reliably give us this
26332616
// information‽
26342617
match self.decl().kind() {
@@ -2647,9 +2630,9 @@ impl TemplateParameters for PartialType {
26472630
};
26482631
clang_sys::CXChildVisit_Continue
26492632
});
2650-
Some(num_params)
2633+
num_params
26512634
}
2652-
_ => None,
2635+
_ => 0,
26532636
}
26542637
}
26552638
}

src/ir/item.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1112,18 +1112,17 @@ where
11121112
fn self_template_params(
11131113
&self,
11141114
ctx: &BindgenContext,
1115-
) -> Option<Vec<TypeId>> {
1116-
ctx.resolve_item_fallible(*self).and_then(|item| {
1117-
item.self_template_params(ctx)
1118-
})
1115+
) -> Vec<TypeId> {
1116+
ctx.resolve_item_fallible(*self)
1117+
.map_or(vec![], |item| item.self_template_params(ctx))
11191118
}
11201119
}
11211120

11221121
impl TemplateParameters for Item {
11231122
fn self_template_params(
11241123
&self,
11251124
ctx: &BindgenContext,
1126-
) -> Option<Vec<TypeId>> {
1125+
) -> Vec<TypeId> {
11271126
self.kind.self_template_params(ctx)
11281127
}
11291128
}
@@ -1132,15 +1131,15 @@ impl TemplateParameters for ItemKind {
11321131
fn self_template_params(
11331132
&self,
11341133
ctx: &BindgenContext,
1135-
) -> Option<Vec<TypeId>> {
1134+
) -> Vec<TypeId> {
11361135
match *self {
11371136
ItemKind::Type(ref ty) => ty.self_template_params(ctx),
11381137
// If we start emitting bindings to explicitly instantiated
11391138
// functions, then we'll need to check ItemKind::Function for
11401139
// template params.
11411140
ItemKind::Function(_) |
11421141
ItemKind::Module(_) |
1143-
ItemKind::Var(_) => None,
1142+
ItemKind::Var(_) => vec![],
11441143
}
11451144
}
11461145
}

0 commit comments

Comments
 (0)