Skip to content

Commit 518ffd5

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 92b86c5 commit 518ffd5

File tree

8 files changed

+123
-145
lines changed

8 files changed

+123
-145
lines changed

src/codegen/mod.rs

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

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

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_all(quote! {
314-
< #( #params ),* >
315-
});
305+
if params.is_empty() {
306+
return;
316307
}
308+
309+
let params = params.into_iter().map(|p| {
310+
p.try_to_rust_ty(ctx, &())
311+
.expect("template params cannot fail to be a rust type")
312+
});
313+
314+
self.append_all(quote! {
315+
< #( #params ),* >
316+
});
317317
}
318318
}
319319

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

490488
let ty = self.ty().to_rust_ty_or_opaque(ctx, &());
@@ -637,12 +635,10 @@ impl CodeGenerator for Type {
637635
return;
638636
}
639637

640-
let mut outer_params = item.used_template_params(ctx)
641-
.and_then(|ps| if ps.is_empty() {
642-
None
643-
} else {
644-
Some(ps)
645-
});
638+
let mut outer_params = match item.used_template_params(ctx).len() {
639+
x if x > 0 => Some(item.used_template_params(ctx)),
640+
_ => None,
641+
};
646642

647643
let inner_rust_type = if item.is_opaque(ctx, &()) {
648644
outer_params = None;
@@ -1598,21 +1594,20 @@ impl CodeGenerator for CompInfo {
15981594

15991595
let mut generic_param_names = vec![];
16001596

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

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

16181613
let generics = if !generic_param_names.is_empty() {
@@ -1657,7 +1652,7 @@ impl CodeGenerator for CompInfo {
16571652
derives.push("Copy");
16581653

16591654
if ctx.options().rust_features().builtin_clone_impls() ||
1660-
used_template_params.is_some()
1655+
used_template_params.len() > 0
16611656
{
16621657
// FIXME: This requires extra logic if you have a big array in a
16631658
// templated struct. The reason for this is that the magic:
@@ -1739,7 +1734,7 @@ impl CodeGenerator for CompInfo {
17391734
);
17401735
}
17411736

1742-
if used_template_params.is_none() {
1737+
if used_template_params.is_empty() {
17431738
if !is_opaque {
17441739
for var in self.inner_vars() {
17451740
ctx.resolve_item(*var).codegen(ctx, result, &());
@@ -2951,7 +2946,6 @@ impl TryToRustTy for Type {
29512946
TypeKind::TemplateAlias(..) |
29522947
TypeKind::Alias(..) => {
29532948
let template_params = item.used_template_params(ctx)
2954-
.unwrap_or(vec![])
29552949
.into_iter()
29562950
.filter(|param| param.is_template_param(ctx, &()))
29572951
.collect::<Vec<_>>();
@@ -2972,7 +2966,7 @@ impl TryToRustTy for Type {
29722966
TypeKind::Comp(ref info) => {
29732967
let template_params = item.used_template_params(ctx);
29742968
if info.has_non_type_template_params() ||
2975-
(item.is_opaque(ctx, &()) && template_params.is_some())
2969+
(item.is_opaque(ctx, &()) && template_params.len() > 0)
29762970
{
29772971
return self.try_to_opaque(ctx, item);
29782972
}
@@ -3066,18 +3060,16 @@ impl TryToRustTy for TemplateInstantiation {
30663060
let def_path = def.namespace_aware_canonical_path(ctx);
30673061
ty.append_separated(def_path.into_iter().map(|p| ctx.rust_ident(p)), proc_macro2::Term::intern("::"));
30683062

3069-
let def_params = match def.self_template_params(ctx) {
3070-
Some(params) => params,
3071-
None => {
3072-
// This can happen if we generated an opaque type for a partial
3073-
// template specialization, and we've hit an instantiation of
3074-
// that partial specialization.
3075-
extra_assert!(
3076-
def.is_opaque(ctx, &())
3077-
);
3078-
return Err(error::Error::InstantiationOfOpaqueType);
3079-
}
3080-
};
3063+
let def_params = def.self_template_params(ctx);
3064+
if def_params.is_empty() {
3065+
// This can happen if we generated an opaque type for a partial
3066+
// template specialization, and we've hit an instantiation of
3067+
// that partial specialization.
3068+
extra_assert!(
3069+
def.is_opaque(ctx, &())
3070+
);
3071+
return Err(error::Error::InstantiationOfOpaqueType);
3072+
}
30813073

30823074
// TODO: If the definition type is a template class/struct
30833075
// definition's member template definition, it could rely on
@@ -3171,10 +3163,8 @@ impl CodeGenerator for Function {
31713163
// instantiations is open ended and we have no way of knowing which
31723164
// monomorphizations actually exist.
31733165
let type_params = item.all_template_params(ctx);
3174-
if let Some(params) = type_params {
3175-
if !params.is_empty() {
3176-
return;
3177-
}
3166+
if !type_params.is_empty() {
3167+
return;
31783168
}
31793169

31803170
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

+28-29
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,11 @@ 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+
Some((
1530+
*canon_decl.cursor(),
1531+
template_decl_id.into(),
1532+
template_decl_id.num_self_template_params(self),
1533+
))
15401534
},
15411535
)
15421536
})
@@ -1557,15 +1551,20 @@ impl BindgenContext {
15571551
.cloned()
15581552
})
15591553
.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-
)
1554+
Some((
1555+
*template_decl.decl(),
1556+
template_decl.id(),
1557+
template_decl.num_self_template_params(self),
1558+
))
1559+
// template_decl.num_self_template_params(self).map(
1560+
// |num_template_params| {
1561+
// (
1562+
// *template_decl.decl(),
1563+
// template_decl.id(),
1564+
// num_template_params,
1565+
// )
1566+
// },
1567+
// )
15691568
})
15701569
})
15711570
}
@@ -1614,8 +1613,8 @@ impl BindgenContext {
16141613

16151614
let num_expected_args = match self.resolve_type(template)
16161615
.num_self_template_params(self) {
1617-
Some(n) => n,
1618-
None => {
1616+
n if n > 0 => n,
1617+
_ => {
16191618
warn!(
16201619
"Tried to instantiate a template for which we could not \
16211620
determine any template parameters"
@@ -2622,13 +2621,13 @@ impl TemplateParameters for PartialType {
26222621
fn self_template_params(
26232622
&self,
26242623
_ctx: &BindgenContext,
2625-
) -> Option<Vec<TypeId>> {
2624+
) -> Vec<TypeId> {
26262625
// Maybe at some point we will eagerly parse named types, but for now we
26272626
// don't and this information is unavailable.
2628-
None
2627+
vec![]
26292628
}
26302629

2631-
fn num_self_template_params(&self, _ctx: &BindgenContext) -> Option<usize> {
2630+
fn num_self_template_params(&self, _ctx: &BindgenContext) -> usize {
26322631
// Wouldn't it be nice if libclang would reliably give us this
26332632
// information‽
26342633
match self.decl().kind() {
@@ -2647,9 +2646,9 @@ impl TemplateParameters for PartialType {
26472646
};
26482647
clang_sys::CXChildVisit_Continue
26492648
});
2650-
Some(num_params)
2649+
num_params
26512650
}
2652-
_ => None,
2651+
_ => 0,
26532652
}
26542653
}
26552654
}

src/ir/item.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1112,18 +1112,18 @@ where
11121112
fn self_template_params(
11131113
&self,
11141114
ctx: &BindgenContext,
1115-
) -> Option<Vec<TypeId>> {
1115+
) -> Vec<TypeId> {
11161116
ctx.resolve_item_fallible(*self).and_then(|item| {
1117-
item.self_template_params(ctx)
1118-
})
1117+
Some(item.self_template_params(ctx))
1118+
}).unwrap_or(vec![])
11191119
}
11201120
}
11211121

11221122
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
}

0 commit comments

Comments
 (0)