Skip to content

Commit 1f9bcbe

Browse files
author
bors-servo
authored
Auto merge of #1305 - tamird:remove-option, r=emilio
TemplateParameters do not return Option Fixes #960. Closes #1245. I found it useful to do this incrementally, changing each method in a separate commit and ensuring the tests continue to pass unchanged. r? @emilio /cc @ericho
2 parents 8fe4d63 + 9b69f3e commit 1f9bcbe

File tree

8 files changed

+128
-179
lines changed

8 files changed

+128
-179
lines changed

src/codegen/mod.rs

+56-79
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,11 @@ 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-
}
306-
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-
302+
let params: Vec<_> = item.used_template_params(ctx).iter().map(|p| {
303+
p.try_to_rust_ty(ctx, &())
304+
.expect("template params cannot fail to be a rust type")
305+
}).collect();
306+
if !params.is_empty() {
312307
self.append_all(quote! {
313308
< #( #params ),* >
314309
});
@@ -479,11 +474,8 @@ impl CodeGenerator for Var {
479474
// We can't generate bindings to static variables of templates. The
480475
// number of actual variables for a single declaration are open ended
481476
// and we don't know what instantiations do or don't exist.
482-
let type_params = item.all_template_params(ctx);
483-
if let Some(params) = type_params {
484-
if !params.is_empty() {
485-
return;
486-
}
477+
if !item.all_template_params(ctx).is_empty() {
478+
return;
487479
}
488480

489481
let ty = self.ty().to_rust_ty_or_opaque(ctx, &());
@@ -636,15 +628,10 @@ impl CodeGenerator for Type {
636628
return;
637629
}
638630

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-
});
631+
let mut outer_params = item.used_template_params(ctx);
645632

646633
let inner_rust_type = if item.is_opaque(ctx, &()) {
647-
outer_params = None;
634+
outer_params = vec![];
648635
self.to_opaque(ctx, item)
649636
} else {
650637
// Its possible that we have better layout information than
@@ -699,7 +686,7 @@ impl CodeGenerator for Type {
699686
'A'...'Z' | 'a'...'z' | '0'...'9' | ':' | '_' | ' ' => true,
700687
_ => false,
701688
}) &&
702-
outer_params.is_none() &&
689+
outer_params.is_empty() &&
703690
inner_item.expect_type().canonical_type(ctx).is_enum()
704691
{
705692
tokens.append_all(quote! {
@@ -718,25 +705,23 @@ impl CodeGenerator for Type {
718705
pub type #rust_name
719706
});
720707

721-
if let Some(params) = outer_params {
722-
let params: Vec<_> = params.into_iter()
723-
.filter_map(|p| p.as_template_param(ctx, &()))
724-
.collect();
725-
if params.iter().any(|p| ctx.resolve_type(*p).is_invalid_type_param()) {
726-
warn!(
727-
"Item contained invalid template \
728-
parameter: {:?}",
729-
item
730-
);
731-
return;
732-
}
733-
734-
let params = params.iter()
735-
.map(|p| {
736-
p.try_to_rust_ty(ctx, &())
737-
.expect("type parameters can always convert to rust ty OK")
738-
});
708+
let params: Vec<_> = outer_params.into_iter()
709+
.filter_map(|p| p.as_template_param(ctx, &()))
710+
.collect();
711+
if params.iter().any(|p| ctx.resolve_type(*p).is_invalid_type_param()) {
712+
warn!(
713+
"Item contained invalid template \
714+
parameter: {:?}",
715+
item
716+
);
717+
return;
718+
}
719+
let params: Vec<_> = params.iter().map(|p| {
720+
p.try_to_rust_ty(ctx, &())
721+
.expect("type parameters can always convert to rust ty OK")
722+
}).collect();
739723

724+
if !params.is_empty() {
740725
tokens.append_all(quote! {
741726
< #( #params ),* >
742727
});
@@ -1418,8 +1403,6 @@ impl CodeGenerator for CompInfo {
14181403
return;
14191404
}
14201405

1421-
let used_template_params = item.used_template_params(ctx);
1422-
14231406
let ty = item.expect_type();
14241407
let layout = ty.layout(ctx);
14251408
let mut packed = self.is_packed(ctx, &layout);
@@ -1601,21 +1584,19 @@ impl CodeGenerator for CompInfo {
16011584

16021585
let mut generic_param_names = vec![];
16031586

1604-
if let Some(ref params) = used_template_params {
1605-
for (idx, ty) in params.iter().enumerate() {
1606-
let param = ctx.resolve_type(*ty);
1607-
let name = param.name().unwrap();
1608-
let ident = ctx.rust_ident(name);
1609-
generic_param_names.push(ident.clone());
1587+
for (idx, ty) in item.used_template_params(ctx).iter().enumerate() {
1588+
let param = ctx.resolve_type(*ty);
1589+
let name = param.name().unwrap();
1590+
let ident = ctx.rust_ident(name);
1591+
generic_param_names.push(ident.clone());
16101592

1611-
let prefix = ctx.trait_prefix();
1612-
let field_name = ctx.rust_ident(format!("_phantom_{}", idx));
1613-
fields.push(quote! {
1614-
pub #field_name : ::#prefix::marker::PhantomData<
1615-
::#prefix::cell::UnsafeCell<#ident>
1616-
> ,
1617-
});
1618-
}
1593+
let prefix = ctx.trait_prefix();
1594+
let field_name = ctx.rust_ident(format!("_phantom_{}", idx));
1595+
fields.push(quote! {
1596+
pub #field_name : ::#prefix::marker::PhantomData<
1597+
::#prefix::cell::UnsafeCell<#ident>
1598+
> ,
1599+
});
16191600
}
16201601

16211602
let generics = if !generic_param_names.is_empty() {
@@ -1668,11 +1649,13 @@ impl CodeGenerator for CompInfo {
16681649
ctx.options().derive_default && !self.is_forward_declaration();
16691650
}
16701651

1652+
let all_template_params = item.all_template_params(ctx);
1653+
16711654
if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() {
16721655
derives.push("Copy");
16731656

16741657
if ctx.options().rust_features().builtin_clone_impls ||
1675-
used_template_params.is_some()
1658+
!all_template_params.is_empty()
16761659
{
16771660
// FIXME: This requires extra logic if you have a big array in a
16781661
// templated struct. The reason for this is that the magic:
@@ -1754,7 +1737,7 @@ impl CodeGenerator for CompInfo {
17541737
);
17551738
}
17561739

1757-
if used_template_params.is_none() {
1740+
if all_template_params.is_empty() {
17581741
if !is_opaque {
17591742
for var in self.inner_vars() {
17601743
ctx.resolve_item(*var).codegen(ctx, result, &());
@@ -3023,7 +3006,6 @@ impl TryToRustTy for Type {
30233006
TypeKind::TemplateAlias(..) |
30243007
TypeKind::Alias(..) => {
30253008
let template_params = item.used_template_params(ctx)
3026-
.unwrap_or(vec![])
30273009
.into_iter()
30283010
.filter(|param| param.is_template_param(ctx, &()))
30293011
.collect::<Vec<_>>();
@@ -3042,9 +3024,9 @@ impl TryToRustTy for Type {
30423024
}
30433025
}
30443026
TypeKind::Comp(ref info) => {
3045-
let template_params = item.used_template_params(ctx);
3027+
let template_params = item.all_template_params(ctx);
30463028
if info.has_non_type_template_params() ||
3047-
(item.is_opaque(ctx, &()) && template_params.is_some())
3029+
(item.is_opaque(ctx, &()) && !template_params.is_empty())
30483030
{
30493031
return self.try_to_opaque(ctx, item);
30503032
}
@@ -3138,18 +3120,16 @@ impl TryToRustTy for TemplateInstantiation {
31383120
let def_path = def.namespace_aware_canonical_path(ctx);
31393121
ty.append_separated(def_path.into_iter().map(|p| ctx.rust_ident(p)), Term::new("::", Span::call_site()));
31403122

3141-
let def_params = match def.self_template_params(ctx) {
3142-
Some(params) => params,
3143-
None => {
3144-
// This can happen if we generated an opaque type for a partial
3145-
// template specialization, and we've hit an instantiation of
3146-
// that partial specialization.
3147-
extra_assert!(
3148-
def.is_opaque(ctx, &())
3149-
);
3150-
return Err(error::Error::InstantiationOfOpaqueType);
3151-
}
3152-
};
3123+
let def_params = def.self_template_params(ctx);
3124+
if def_params.is_empty() {
3125+
// This can happen if we generated an opaque type for a partial
3126+
// template specialization, and we've hit an instantiation of
3127+
// that partial specialization.
3128+
extra_assert!(
3129+
def.is_opaque(ctx, &())
3130+
);
3131+
return Err(error::Error::InstantiationOfOpaqueType);
3132+
}
31533133

31543134
// TODO: If the definition type is a template class/struct
31553135
// definition's member template definition, it could rely on
@@ -3242,11 +3222,8 @@ impl CodeGenerator for Function {
32423222
// generate bindings to template functions, because the set of
32433223
// instantiations is open ended and we have no way of knowing which
32443224
// monomorphizations actually exist.
3245-
let type_params = item.all_template_params(ctx);
3246-
if let Some(params) = type_params {
3247-
if !params.is_empty() {
3248-
return;
3249-
}
3225+
if !item.all_template_params(ctx).is_empty() {
3226+
return;
32503227
}
32513228

32523229
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.all_template_params(self.ctx).is_empty()
251251
{
252252
trace!(
253253
" comp cannot derive copy because issue 36640"

src/ir/analysis/template_params.rs

+2-2
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
@@ -419,7 +419,7 @@ impl<'ctx> MonotoneFramework for UsedTemplateParameters<'ctx> {
419419
// template parameters, there is a single exception:
420420
// opaque templates. Hence the unwrap_or.
421421
let params =
422-
decl.self_template_params(ctx).unwrap_or(vec![]);
422+
decl.self_template_params(ctx);
423423

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

src/ir/comp.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1669,12 +1669,8 @@ impl TemplateParameters for CompInfo {
16691669
fn self_template_params(
16701670
&self,
16711671
_ctx: &BindgenContext,
1672-
) -> Option<Vec<TypeId>> {
1673-
if self.template_params.is_empty() {
1674-
None
1675-
} else {
1676-
Some(self.template_params.clone())
1677-
}
1672+
) -> Vec<TypeId> {
1673+
self.template_params.clone()
16781674
}
16791675
}
16801676

@@ -1685,8 +1681,7 @@ impl Trace for CompInfo {
16851681
where
16861682
T: Tracer,
16871683
{
1688-
let params = item.all_template_params(context).unwrap_or(vec![]);
1689-
for p in params {
1684+
for p in item.all_template_params(context) {
16901685
tracer.visit_kind(p.into(), EdgeKind::TemplateParameterDefinition);
16911686
}
16921687

0 commit comments

Comments
 (0)