diff options
author | Tamir Duberstein <tamird@gmail.com> | 2018-04-07 07:48:23 -0400 |
---|---|---|
committer | Tamir Duberstein <tamird@gmail.com> | 2018-04-08 18:34:26 -0400 |
commit | 8660c0916fa56de0fce3a0a6d670eb254fcfb42c (patch) | |
tree | 9b52ab32520f0ee52e5a3401ca9e3ebddf1db192 | |
parent | e28cb8a13ecacf0dc4aeab6119ca9fea8d0a1cc9 (diff) |
TemplateParameters.used_template_params doesn't return Option
-rw-r--r-- | src/codegen/mod.rs | 99 | ||||
-rw-r--r-- | src/ir/analysis/derive_copy.rs | 2 | ||||
-rw-r--r-- | src/ir/template.rs | 26 |
3 files changed, 54 insertions, 73 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index e2a8b33f..04e28db2 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -299,16 +299,11 @@ impl AppendImplicitTemplateParams for quote::Tokens { _ => {}, } - if let Some(params) = item.used_template_params(ctx) { - if params.is_empty() { - return; - } - - let params = params.into_iter().map(|p| { - p.try_to_rust_ty(ctx, &()) - .expect("template params cannot fail to be a rust type") - }); - + let params: Vec<_> = item.used_template_params(ctx).iter().map(|p| { + p.try_to_rust_ty(ctx, &()) + .expect("template params cannot fail to be a rust type") + }).collect(); + if !params.is_empty() { self.append_all(quote! { < #( #params ),* > }); @@ -633,15 +628,10 @@ impl CodeGenerator for Type { return; } - let mut outer_params = item.used_template_params(ctx) - .and_then(|ps| if ps.is_empty() { - None - } else { - Some(ps) - }); + let mut outer_params = item.used_template_params(ctx); let inner_rust_type = if item.is_opaque(ctx, &()) { - outer_params = None; + outer_params = vec![]; self.to_opaque(ctx, item) } else { // Its possible that we have better layout information than @@ -696,7 +686,7 @@ impl CodeGenerator for Type { 'A'...'Z' | 'a'...'z' | '0'...'9' | ':' | '_' | ' ' => true, _ => false, }) && - outer_params.is_none() && + outer_params.is_empty() && inner_item.expect_type().canonical_type(ctx).is_enum() { tokens.append_all(quote! { @@ -715,25 +705,23 @@ impl CodeGenerator for Type { pub type #rust_name }); - if let Some(params) = outer_params { - let params: Vec<_> = params.into_iter() - .filter_map(|p| p.as_template_param(ctx, &())) - .collect(); - if params.iter().any(|p| ctx.resolve_type(*p).is_invalid_type_param()) { - warn!( - "Item contained invalid template \ - parameter: {:?}", - item - ); - return; - } - - let params = params.iter() - .map(|p| { - p.try_to_rust_ty(ctx, &()) - .expect("type parameters can always convert to rust ty OK") - }); + let params: Vec<_> = outer_params.into_iter() + .filter_map(|p| p.as_template_param(ctx, &())) + .collect(); + if params.iter().any(|p| ctx.resolve_type(*p).is_invalid_type_param()) { + warn!( + "Item contained invalid template \ + parameter: {:?}", + item + ); + return; + } + let params: Vec<_> = params.iter().map(|p| { + p.try_to_rust_ty(ctx, &()) + .expect("type parameters can always convert to rust ty OK") + }).collect(); + if !params.is_empty() { tokens.append_all(quote! { < #( #params ),* > }); @@ -1415,8 +1403,6 @@ impl CodeGenerator for CompInfo { return; } - let used_template_params = item.used_template_params(ctx); - let ty = item.expect_type(); let layout = ty.layout(ctx); let mut packed = self.is_packed(ctx, &layout); @@ -1598,21 +1584,19 @@ impl CodeGenerator for CompInfo { let mut generic_param_names = vec![]; - if let Some(ref params) = used_template_params { - for (idx, ty) in params.iter().enumerate() { - let param = ctx.resolve_type(*ty); - let name = param.name().unwrap(); - let ident = ctx.rust_ident(name); - generic_param_names.push(ident.clone()); + for (idx, ty) in item.used_template_params(ctx).iter().enumerate() { + let param = ctx.resolve_type(*ty); + let name = param.name().unwrap(); + let ident = ctx.rust_ident(name); + generic_param_names.push(ident.clone()); - let prefix = ctx.trait_prefix(); - let field_name = ctx.rust_ident(format!("_phantom_{}", idx)); - fields.push(quote! { - pub #field_name : ::#prefix::marker::PhantomData< - ::#prefix::cell::UnsafeCell<#ident> - > , - }); - } + let prefix = ctx.trait_prefix(); + let field_name = ctx.rust_ident(format!("_phantom_{}", idx)); + fields.push(quote! { + pub #field_name : ::#prefix::marker::PhantomData< + ::#prefix::cell::UnsafeCell<#ident> + > , + }); } let generics = if !generic_param_names.is_empty() { @@ -1665,11 +1649,13 @@ impl CodeGenerator for CompInfo { ctx.options().derive_default && !self.is_forward_declaration(); } + let all_template_params = item.all_template_params(ctx); + if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() { derives.push("Copy"); if ctx.options().rust_features().builtin_clone_impls || - used_template_params.is_some() + !all_template_params.is_empty() { // FIXME: This requires extra logic if you have a big array in a // templated struct. The reason for this is that the magic: @@ -1751,7 +1737,7 @@ impl CodeGenerator for CompInfo { ); } - if used_template_params.is_none() { + if all_template_params.is_empty() { if !is_opaque { for var in self.inner_vars() { ctx.resolve_item(*var).codegen(ctx, result, &()); @@ -3020,7 +3006,6 @@ impl TryToRustTy for Type { TypeKind::TemplateAlias(..) | TypeKind::Alias(..) => { let template_params = item.used_template_params(ctx) - .unwrap_or(vec![]) .into_iter() .filter(|param| param.is_template_param(ctx, &())) .collect::<Vec<_>>(); @@ -3039,9 +3024,9 @@ impl TryToRustTy for Type { } } TypeKind::Comp(ref info) => { - let template_params = item.used_template_params(ctx); + let template_params = item.all_template_params(ctx); if info.has_non_type_template_params() || - (item.is_opaque(ctx, &()) && template_params.is_some()) + (item.is_opaque(ctx, &()) && !template_params.is_empty()) { return self.try_to_opaque(ctx, item); } diff --git a/src/ir/analysis/derive_copy.rs b/src/ir/analysis/derive_copy.rs index 59eb469b..1e8dc819 100644 --- a/src/ir/analysis/derive_copy.rs +++ b/src/ir/analysis/derive_copy.rs @@ -247,7 +247,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> { // https://github.com/rust-lang/rust/issues/36640 if !info.self_template_params(self.ctx).is_empty() || - item.used_template_params(self.ctx).is_some() + !item.all_template_params(self.ctx).is_empty() { trace!( " comp cannot derive copy because issue 36640" diff --git a/src/ir/template.rs b/src/ir/template.rs index cdea85cf..61654fe8 100644 --- a/src/ir/template.rs +++ b/src/ir/template.rs @@ -92,12 +92,12 @@ use parse::ClangItemParser; /// ----+------+-----+----------------------+ /// ... |Decl. | ... | used_template_params | /// ----+------+-----+----------------------+ -/// ... |Foo | ... | Some([T, U]) | -/// ... |Bar | ... | Some([V]) | -/// ... |Inner | ... | None | -/// ... |Lol | ... | Some([T]) | -/// ... |Wtf | ... | Some([T]) | -/// ... |Qux | ... | None | +/// ... |Foo | ... | [T, U] | +/// ... |Bar | ... | [V] | +/// ... |Inner | ... | [] | +/// ... |Lol | ... | [T] | +/// ... |Wtf | ... | [T] | +/// ... |Qux | ... | [] | /// ----+------+-----+----------------------+ pub trait TemplateParameters { /// Get the set of `ItemId`s that make up this template declaration's free @@ -144,7 +144,7 @@ pub trait TemplateParameters { /// Get only the set of template parameters that this item uses. This is a /// subset of `all_template_params` and does not necessarily contain any of /// `self_template_params`. - fn used_template_params(&self, ctx: &BindgenContext) -> Option<Vec<TypeId>> + fn used_template_params(&self, ctx: &BindgenContext) -> Vec<TypeId> where Self: AsRef<ItemId>, { @@ -154,14 +154,10 @@ pub trait TemplateParameters { ); let id = *self.as_ref(); - let all_template_params: Vec<_> = ctx.resolve_item(id).all_template_params(ctx); - if all_template_params.len() > 0 { - Some(all_template_params.into_iter() - .filter(|p| ctx.uses_template_parameter(id, *p)) - .collect()) - } else { - None - } + ctx.resolve_item(id).all_template_params(ctx) + .into_iter() + .filter(|p| ctx.uses_template_parameter(id, *p)) + .collect() } } |