@@ -299,16 +299,11 @@ impl AppendImplicitTemplateParams for quote::Tokens {
299
299
_ => { } ,
300
300
}
301
301
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 ( ) {
312
307
self . append_all ( quote ! {
313
308
< #( #params ) , * >
314
309
} ) ;
@@ -479,11 +474,8 @@ impl CodeGenerator for Var {
479
474
// We can't generate bindings to static variables of templates. The
480
475
// number of actual variables for a single declaration are open ended
481
476
// 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 ;
487
479
}
488
480
489
481
let ty = self . ty ( ) . to_rust_ty_or_opaque ( ctx, & ( ) ) ;
@@ -636,15 +628,10 @@ impl CodeGenerator for Type {
636
628
return ;
637
629
}
638
630
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) ;
645
632
646
633
let inner_rust_type = if item. is_opaque ( ctx, & ( ) ) {
647
- outer_params = None ;
634
+ outer_params = vec ! [ ] ;
648
635
self . to_opaque ( ctx, item)
649
636
} else {
650
637
// Its possible that we have better layout information than
@@ -699,7 +686,7 @@ impl CodeGenerator for Type {
699
686
'A' ...'Z' | 'a' ...'z' | '0' ...'9' | ':' | '_' | ' ' => true ,
700
687
_ => false ,
701
688
} ) &&
702
- outer_params. is_none ( ) &&
689
+ outer_params. is_empty ( ) &&
703
690
inner_item. expect_type ( ) . canonical_type ( ctx) . is_enum ( )
704
691
{
705
692
tokens. append_all ( quote ! {
@@ -718,25 +705,23 @@ impl CodeGenerator for Type {
718
705
pub type #rust_name
719
706
} ) ;
720
707
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 ( ) ;
739
723
724
+ if !params. is_empty ( ) {
740
725
tokens. append_all ( quote ! {
741
726
< #( #params ) , * >
742
727
} ) ;
@@ -1418,8 +1403,6 @@ impl CodeGenerator for CompInfo {
1418
1403
return ;
1419
1404
}
1420
1405
1421
- let used_template_params = item. used_template_params ( ctx) ;
1422
-
1423
1406
let ty = item. expect_type ( ) ;
1424
1407
let layout = ty. layout ( ctx) ;
1425
1408
let mut packed = self . is_packed ( ctx, & layout) ;
@@ -1601,21 +1584,19 @@ impl CodeGenerator for CompInfo {
1601
1584
1602
1585
let mut generic_param_names = vec ! [ ] ;
1603
1586
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 ( ) ) ;
1610
1592
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
+ } ) ;
1619
1600
}
1620
1601
1621
1602
let generics = if !generic_param_names. is_empty ( ) {
@@ -1668,11 +1649,13 @@ impl CodeGenerator for CompInfo {
1668
1649
ctx. options ( ) . derive_default && !self . is_forward_declaration ( ) ;
1669
1650
}
1670
1651
1652
+ let all_template_params = item. all_template_params ( ctx) ;
1653
+
1671
1654
if item. can_derive_copy ( ctx) && !item. annotations ( ) . disallow_copy ( ) {
1672
1655
derives. push ( "Copy" ) ;
1673
1656
1674
1657
if ctx. options ( ) . rust_features ( ) . builtin_clone_impls ||
1675
- used_template_params . is_some ( )
1658
+ !all_template_params . is_empty ( )
1676
1659
{
1677
1660
// FIXME: This requires extra logic if you have a big array in a
1678
1661
// templated struct. The reason for this is that the magic:
@@ -1754,7 +1737,7 @@ impl CodeGenerator for CompInfo {
1754
1737
) ;
1755
1738
}
1756
1739
1757
- if used_template_params . is_none ( ) {
1740
+ if all_template_params . is_empty ( ) {
1758
1741
if !is_opaque {
1759
1742
for var in self . inner_vars ( ) {
1760
1743
ctx. resolve_item ( * var) . codegen ( ctx, result, & ( ) ) ;
@@ -3023,7 +3006,6 @@ impl TryToRustTy for Type {
3023
3006
TypeKind :: TemplateAlias ( ..) |
3024
3007
TypeKind :: Alias ( ..) => {
3025
3008
let template_params = item. used_template_params ( ctx)
3026
- . unwrap_or ( vec ! [ ] )
3027
3009
. into_iter ( )
3028
3010
. filter ( |param| param. is_template_param ( ctx, & ( ) ) )
3029
3011
. collect :: < Vec < _ > > ( ) ;
@@ -3042,9 +3024,9 @@ impl TryToRustTy for Type {
3042
3024
}
3043
3025
}
3044
3026
TypeKind :: Comp ( ref info) => {
3045
- let template_params = item. used_template_params ( ctx) ;
3027
+ let template_params = item. all_template_params ( ctx) ;
3046
3028
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 ( ) )
3048
3030
{
3049
3031
return self . try_to_opaque ( ctx, item) ;
3050
3032
}
@@ -3138,18 +3120,16 @@ impl TryToRustTy for TemplateInstantiation {
3138
3120
let def_path = def. namespace_aware_canonical_path ( ctx) ;
3139
3121
ty. append_separated ( def_path. into_iter ( ) . map ( |p| ctx. rust_ident ( p) ) , Term :: new ( "::" , Span :: call_site ( ) ) ) ;
3140
3122
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
+ }
3153
3133
3154
3134
// TODO: If the definition type is a template class/struct
3155
3135
// definition's member template definition, it could rely on
@@ -3242,11 +3222,8 @@ impl CodeGenerator for Function {
3242
3222
// generate bindings to template functions, because the set of
3243
3223
// instantiations is open ended and we have no way of knowing which
3244
3224
// 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 ;
3250
3227
}
3251
3228
3252
3229
let name = self . name ( ) ;
0 commit comments