Skip to content

Emit PhantomData<UnsafeCell<T>> members for all generic parameters #689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1465,11 +1465,20 @@ impl CodeGenerator for CompInfo {
let mut generics = aster::AstBuilder::new().generics();

if let Some(ref params) = used_template_params {
for ty in params.iter() {
for (idx, ty) in params.iter().enumerate() {
let param = ctx.resolve_type(*ty);
let name = param.name().unwrap();
let ident = ctx.rust_ident(name);

generics = generics.ty_param_id(ident);

let prefix = ctx.trait_prefix();
let phantom_ty = quote_ty!(
ctx.ext_cx(),
::$prefix::marker::PhantomData<::$prefix::cell::UnsafeCell<$ident>>);
let phantom_field = StructFieldBuilder::named(format!("_phantom_{}", idx))
.build_ty(phantom_ty);
fields.push(phantom_field);
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/anonymous-template-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct Foo<T> {
pub t_member: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for Foo<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -21,6 +22,7 @@ pub struct Bar {
#[derive(Debug, Copy, Clone)]
pub struct Quux<V> {
pub v_member: V,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<V>>,
}
impl <V> Default for Quux<V> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
3 changes: 3 additions & 0 deletions tests/expectations/tests/class_nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Clone for A_C {
#[derive(Debug, Copy, Clone)]
pub struct A_D<T> {
pub foo: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for A_D<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down Expand Up @@ -115,11 +116,13 @@ impl Clone for D {
#[derive(Debug, Copy, Clone)]
pub struct Templated<T> {
pub member: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Templated_Templated_inner<T> {
pub member_ptr: *mut T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for Templated_Templated_inner<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/class_with_dtor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug)]
pub struct HandleWithDtor<T> {
pub ptr: *mut T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for HandleWithDtor<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/const_tparam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pub struct C<T> {
pub foo: *const T,
pub bar: *mut T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for C<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/default-template-parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
pub struct Foo<T, U> {
pub t: T,
pub u: U,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
_phantom_1: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
}
impl <T, U> Default for Foo<T, U> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/forward-declaration-autoptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Foo {
#[derive(Debug, Copy, Clone)]
pub struct RefPtr<T> {
pub m_inner: *mut T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for RefPtr<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct Rooted<T> {
pub _base: js_RootedBase<T>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for Rooted<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -17,6 +18,7 @@ impl <T> Default for Rooted<T> {
pub struct js_RootedBase<T> {
pub foo: *mut T,
pub next: *mut Rooted<T>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for js_RootedBase<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/inherit_named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Wohoo {
#[derive(Debug, Copy, Clone)]
pub struct Weeee<T> {
pub _base: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for Weeee<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl Clone for A {
#[derive(Debug, Copy, Clone)]
pub struct e<c> {
pub d: RefPtr<c>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<c>>,
}
impl <c> Default for e<c> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct RefPtr<T> {
pub use_of_t: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for RefPtr<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -16,6 +17,7 @@ impl <T> Default for RefPtr<T> {
#[derive(Debug, Copy, Clone)]
pub struct UsesRefPtrWithAliasedTypeParam<U> {
pub member: RefPtr<UsesRefPtrWithAliasedTypeParam_V<U>>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
}
pub type UsesRefPtrWithAliasedTypeParam_V<U> = U;
impl <U> Default for UsesRefPtrWithAliasedTypeParam<U> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#[derive(Debug, Copy, Clone)]
pub struct HasRefPtr<T> {
pub refptr_member: RefPtr<HasRefPtr_TypedefOfT<T>>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
pub type HasRefPtr_TypedefOfT<T> = T;
impl <T> Default for HasRefPtr<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct RefPtr<T> {
pub a: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for RefPtr<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -16,6 +17,7 @@ impl <T> Default for RefPtr<T> {
#[derive(Debug, Copy, Clone)]
pub struct nsMainThreadPtrHolder<T> {
pub a: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for nsMainThreadPtrHolder<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -24,6 +26,7 @@ impl <T> Default for nsMainThreadPtrHolder<T> {
#[derive(Debug, Copy, Clone)]
pub struct nsMainThreadPtrHandle<T> {
pub mPtr: RefPtr<nsMainThreadPtrHolder<T>>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for nsMainThreadPtrHandle<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/issue-662-part-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#[derive(Debug, Copy, Clone)]
pub struct nsMainThreadPtrHolder<T> {
pub a: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for nsMainThreadPtrHolder<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -17,6 +18,7 @@ impl <T> Default for nsMainThreadPtrHolder<T> {
#[derive(Debug, Copy, Clone)]
pub struct nsMainThreadPtrHandle<U> {
pub mPtr: RefPtr<nsMainThreadPtrHolder<U>>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
}
impl <U> Default for nsMainThreadPtrHandle<U> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub mod root {
pub m_c: T,
pub m_c_ptr: *mut T,
pub m_c_arr: [T; 10usize],
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for C<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -81,6 +82,7 @@ pub mod root {
#[derive(Debug)]
pub struct D<T> {
pub m_c: root::C<T>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for D<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/nsStyleAutoArray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct nsTArray<T> {
pub mBuff: *mut T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for nsTArray<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -17,6 +18,7 @@ impl <T> Default for nsTArray<T> {
pub struct nsStyleAutoArray<T> {
pub mFirstElement: T,
pub mOtherElements: nsTArray<T>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/replace_template_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub type JS_detail_MaybeWrapped<T> = T;
#[derive(Debug, Copy, Clone)]
pub struct JS_Rooted<T> {
pub ptr: JS_detail_MaybeWrapped<T>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for JS_Rooted<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/replaces_double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct Rooted<T> {
pub ptr: Rooted_MaybeWrapped<T>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
/**
* <div rustbindgen replaces="Rooted_MaybeWrapped"></div>
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/template-param-usage-0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct UsesTemplateParameter<T> {
pub t: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for UsesTemplateParameter<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
4 changes: 4 additions & 0 deletions tests/expectations/tests/template-param-usage-10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#[derive(Debug, Copy, Clone)]
pub struct DoublyIndirectUsage<T, U> {
pub doubly_indirect: DoublyIndirectUsage_IndirectUsage<T, U>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
_phantom_1: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
}
pub type DoublyIndirectUsage_Aliased<T> = T;
pub type DoublyIndirectUsage_Typedefed<U> = U;
Expand All @@ -16,6 +18,8 @@ pub type DoublyIndirectUsage_Typedefed<U> = U;
pub struct DoublyIndirectUsage_IndirectUsage<T, U> {
pub member: DoublyIndirectUsage_Aliased<T>,
pub another: DoublyIndirectUsage_Typedefed<U>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
_phantom_1: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
}
impl <T, U> Default for DoublyIndirectUsage_IndirectUsage<T, U> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/template-param-usage-12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct BaseUsesT<T> {
pub t: *mut T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for BaseUsesT<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand All @@ -17,6 +18,7 @@ impl <T> Default for BaseUsesT<T> {
pub struct CrtpUsesU<U> {
pub _base: BaseUsesT<CrtpUsesU<U>>,
pub usage: U,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
}
impl <U> Default for CrtpUsesU<U> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/template-param-usage-13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct BaseIgnoresT {
pub struct CrtpUsesU<U> {
pub _base: BaseIgnoresT,
pub usage: U,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
}
impl <U> Default for CrtpUsesU<U> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/template-param-usage-15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct BaseUsesT<T> {
pub usage: *mut T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for BaseUsesT<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/template-param-usage-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#[derive(Debug, Copy, Clone)]
pub struct UsesTemplateParameter<T> {
pub t: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct UsesTemplateParameter_AlsoUsesTemplateParameter<T> {
pub also: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
impl <T> Default for UsesTemplateParameter_AlsoUsesTemplateParameter<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
3 changes: 3 additions & 0 deletions tests/expectations/tests/template-param-usage-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#[derive(Debug, Copy, Clone)]
pub struct UsesTemplateParameter<T> {
pub t: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct UsesTemplateParameter_AlsoUsesTemplateParameterAndMore<T, U> {
pub also: T,
pub more: U,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
_phantom_1: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
}
impl <T, U> Default for
UsesTemplateParameter_AlsoUsesTemplateParameterAndMore<T, U> {
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/template-param-usage-4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct UsesTemplateParameter<T> {
pub t: T,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/tests/template-param-usage-5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[derive(Debug, Copy, Clone)]
pub struct IndirectlyUsesTemplateParameter<T> {
pub aliased: IndirectlyUsesTemplateParameter_Aliased<T>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
}
pub type IndirectlyUsesTemplateParameter_Aliased<T> = T;
impl <T> Default for IndirectlyUsesTemplateParameter<T> {
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/template-param-usage-7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
pub struct DoesNotUseU<T, V> {
pub t: T,
pub v: V,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
_phantom_1: ::std::marker::PhantomData<::std::cell::UnsafeCell<V>>,
}
impl <T, V> Default for DoesNotUseU<T, V> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/template-param-usage-8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
pub struct IndirectUsage<T, U> {
pub member1: IndirectUsage_Typedefed<T>,
pub member2: IndirectUsage_Aliased<U>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
_phantom_1: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
}
pub type IndirectUsage_Typedefed<T> = T;
pub type IndirectUsage_Aliased<U> = U;
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/tests/template-param-usage-9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub type DoesNotUse_Typedefed<U> = U;
pub struct DoesNotUse_IndirectUsage<T, U> {
pub member: DoesNotUse_Aliased<T>,
pub another: DoesNotUse_Typedefed<U>,
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
_phantom_1: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
}
impl <T, U> Default for DoesNotUse_IndirectUsage<T, U> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
Expand Down
Loading