Skip to content

Commit 2d9f48c

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 2d9f48c

File tree

8 files changed

+120
-146
lines changed

8 files changed

+120
-146
lines changed

src/codegen/mod.rs

+44-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,8 @@ 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 used_params = item.used_template_params(ctx);
639+
let mut outer_params = if used_params.is_empty() { None } else { Some(used_params) };
646640

647641
let inner_rust_type = if item.is_opaque(ctx, &()) {
648642
outer_params = None;
@@ -1598,21 +1592,20 @@ impl CodeGenerator for CompInfo {
15981592

15991593
let mut generic_param_names = vec![];
16001594

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());
16071595

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

16181611
let generics = if !generic_param_names.is_empty() {
@@ -1657,7 +1650,7 @@ impl CodeGenerator for CompInfo {
16571650
derives.push("Copy");
16581651

16591652
if ctx.options().rust_features().builtin_clone_impls() ||
1660-
used_template_params.is_some()
1653+
!used_template_params.is_empty()
16611654
{
16621655
// FIXME: This requires extra logic if you have a big array in a
16631656
// templated struct. The reason for this is that the magic:
@@ -1739,7 +1732,7 @@ impl CodeGenerator for CompInfo {
17391732
);
17401733
}
17411734

1742-
if used_template_params.is_none() {
1735+
if used_template_params.is_empty() {
17431736
if !is_opaque {
17441737
for var in self.inner_vars() {
17451738
ctx.resolve_item(*var).codegen(ctx, result, &());
@@ -2951,7 +2944,6 @@ impl TryToRustTy for Type {
29512944
TypeKind::TemplateAlias(..) |
29522945
TypeKind::Alias(..) => {
29532946
let template_params = item.used_template_params(ctx)
2954-
.unwrap_or(vec![])
29552947
.into_iter()
29562948
.filter(|param| param.is_template_param(ctx, &()))
29572949
.collect::<Vec<_>>();
@@ -2972,7 +2964,7 @@ impl TryToRustTy for Type {
29722964
TypeKind::Comp(ref info) => {
29732965
let template_params = item.used_template_params(ctx);
29742966
if info.has_non_type_template_params() ||
2975-
(item.is_opaque(ctx, &()) && template_params.is_some())
2967+
(item.is_opaque(ctx, &()) && !template_params.is_empty())
29762968
{
29772969
return self.try_to_opaque(ctx, item);
29782970
}
@@ -3066,18 +3058,16 @@ impl TryToRustTy for TemplateInstantiation {
30663058
let def_path = def.namespace_aware_canonical_path(ctx);
30673059
ty.append_separated(def_path.into_iter().map(|p| ctx.rust_ident(p)), proc_macro2::Term::intern("::"));
30683060

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-
};
3061+
let def_params = def.self_template_params(ctx);
3062+
if def_params.is_empty() {
3063+
// This can happen if we generated an opaque type for a partial
3064+
// template specialization, and we've hit an instantiation of
3065+
// that partial specialization.
3066+
extra_assert!(
3067+
def.is_opaque(ctx, &())
3068+
);
3069+
return Err(error::Error::InstantiationOfOpaqueType);
3070+
}
30813071

30823072
// TODO: If the definition type is a template class/struct
30833073
// definition's member template definition, it could rely on
@@ -3171,10 +3161,8 @@ impl CodeGenerator for Function {
31713161
// instantiations is open ended and we have no way of knowing which
31723162
// monomorphizations actually exist.
31733163
let type_params = item.all_template_params(ctx);
3174-
if let Some(params) = type_params {
3175-
if !params.is_empty() {
3176-
return;
3177-
}
3164+
if !type_params.is_empty() {
3165+
return;
31783166
}
31793167

31803168
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

+30-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,17 @@ 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_template_params = template_decl_id
1530+
.num_self_template_params(self);
1531+
if num_template_params == 0 {
1532+
None
1533+
} else {
1534+
Some((
1535+
*canon_decl.cursor(),
1536+
template_decl_id.into(),
1537+
num_template_params,
1538+
))
1539+
}
15401540
},
15411541
)
15421542
})
@@ -1557,15 +1557,16 @@ impl BindgenContext {
15571557
.cloned()
15581558
})
15591559
.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-
)
1560+
let num_template_params = template_decl.num_self_template_params(self);
1561+
if num_template_params == 0 {
1562+
None
1563+
} else {
1564+
Some((
1565+
*template_decl.decl(),
1566+
template_decl.id(),
1567+
num_template_params,
1568+
))
1569+
}
15691570
})
15701571
})
15711572
}
@@ -1614,8 +1615,8 @@ impl BindgenContext {
16141615

16151616
let num_expected_args = match self.resolve_type(template)
16161617
.num_self_template_params(self) {
1617-
Some(n) => n,
1618-
None => {
1618+
n if n > 0 => n,
1619+
_ => {
16191620
warn!(
16201621
"Tried to instantiate a template for which we could not \
16211622
determine any template parameters"
@@ -2622,13 +2623,13 @@ impl TemplateParameters for PartialType {
26222623
fn self_template_params(
26232624
&self,
26242625
_ctx: &BindgenContext,
2625-
) -> Option<Vec<TypeId>> {
2626+
) -> Vec<TypeId> {
26262627
// Maybe at some point we will eagerly parse named types, but for now we
26272628
// don't and this information is unavailable.
2628-
None
2629+
vec![]
26292630
}
26302631

2631-
fn num_self_template_params(&self, _ctx: &BindgenContext) -> Option<usize> {
2632+
fn num_self_template_params(&self, _ctx: &BindgenContext) -> usize {
26322633
// Wouldn't it be nice if libclang would reliably give us this
26332634
// information‽
26342635
match self.decl().kind() {
@@ -2647,9 +2648,9 @@ impl TemplateParameters for PartialType {
26472648
};
26482649
clang_sys::CXChildVisit_Continue
26492650
});
2650-
Some(num_params)
2651+
num_params
26512652
}
2652-
_ => None,
2653+
_ => 0,
26532654
}
26542655
}
26552656
}

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)