Skip to content

Commit 3eb8f70

Browse files
author
bors-servo
authored
Auto merge of #689 - fitzgen:Kowasaki-master, r=fitzgen
Emit `PhantomData<UnsafeCell<T>>` members for all generic parameters This makes generated generic structs lifetime invariant, since we cannot know the C++ type's true variance. Fixes #506 Rebased + squashed version of #635; cc @Kowasaki To resolve the conflicts and rebase and squash, I did essentially these commands: ``` $ git fetch servo $ git checkout -b Kowasaki-master servo/master $ git merge --squash --edit Kowasaki/master $ cargo test $ cd tests/expectations $ cargo test $ cd - $ git commit --author="Kowasaki" $ git push fitzgen Kowasaki-master ``` Hopefully that is useful for you in the future :) Thanks for fixing this issue!
2 parents 66b9c3d + c779297 commit 3eb8f70

39 files changed

+628
-548
lines changed

src/codegen/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1465,11 +1465,20 @@ impl CodeGenerator for CompInfo {
14651465
let mut generics = aster::AstBuilder::new().generics();
14661466

14671467
if let Some(ref params) = used_template_params {
1468-
for ty in params.iter() {
1468+
for (idx, ty) in params.iter().enumerate() {
14691469
let param = ctx.resolve_type(*ty);
14701470
let name = param.name().unwrap();
14711471
let ident = ctx.rust_ident(name);
1472+
14721473
generics = generics.ty_param_id(ident);
1474+
1475+
let prefix = ctx.trait_prefix();
1476+
let phantom_ty = quote_ty!(
1477+
ctx.ext_cx(),
1478+
::$prefix::marker::PhantomData<::$prefix::cell::UnsafeCell<$ident>>);
1479+
let phantom_field = StructFieldBuilder::named(format!("_phantom_{}", idx))
1480+
.build_ty(phantom_ty);
1481+
fields.push(phantom_field);
14731482
}
14741483
}
14751484

tests/expectations/tests/anonymous-template-types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#[derive(Debug, Copy, Clone)]
99
pub struct Foo<T> {
1010
pub t_member: T,
11+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
1112
}
1213
impl <T> Default for Foo<T> {
1314
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
@@ -21,6 +22,7 @@ pub struct Bar {
2122
#[derive(Debug, Copy, Clone)]
2223
pub struct Quux<V> {
2324
pub v_member: V,
25+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<V>>,
2426
}
2527
impl <V> Default for Quux<V> {
2628
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/class_nested.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl Clone for A_C {
5353
#[derive(Debug, Copy, Clone)]
5454
pub struct A_D<T> {
5555
pub foo: T,
56+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
5657
}
5758
impl <T> Default for A_D<T> {
5859
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
@@ -115,11 +116,13 @@ impl Clone for D {
115116
#[derive(Debug, Copy, Clone)]
116117
pub struct Templated<T> {
117118
pub member: T,
119+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
118120
}
119121
#[repr(C)]
120122
#[derive(Debug, Copy, Clone)]
121123
pub struct Templated_Templated_inner<T> {
122124
pub member_ptr: *mut T,
125+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
123126
}
124127
impl <T> Default for Templated_Templated_inner<T> {
125128
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/class_with_dtor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#[derive(Debug)]
99
pub struct HandleWithDtor<T> {
1010
pub ptr: *mut T,
11+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
1112
}
1213
impl <T> Default for HandleWithDtor<T> {
1314
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/const_tparam.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
pub struct C<T> {
1010
pub foo: *const T,
1111
pub bar: *mut T,
12+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
1213
}
1314
impl <T> Default for C<T> {
1415
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/default-template-parameter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
pub struct Foo<T, U> {
1010
pub t: T,
1111
pub u: U,
12+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
13+
_phantom_1: ::std::marker::PhantomData<::std::cell::UnsafeCell<U>>,
1214
}
1315
impl <T, U> Default for Foo<T, U> {
1416
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/forward-declaration-autoptr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct Foo {
1313
#[derive(Debug, Copy, Clone)]
1414
pub struct RefPtr<T> {
1515
pub m_inner: *mut T,
16+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
1617
}
1718
impl <T> Default for RefPtr<T> {
1819
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/forward-inherit-struct-with-fields.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#[derive(Debug, Copy, Clone)]
99
pub struct Rooted<T> {
1010
pub _base: js_RootedBase<T>,
11+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
1112
}
1213
impl <T> Default for Rooted<T> {
1314
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
@@ -17,6 +18,7 @@ impl <T> Default for Rooted<T> {
1718
pub struct js_RootedBase<T> {
1819
pub foo: *mut T,
1920
pub next: *mut Rooted<T>,
21+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
2022
}
2123
impl <T> Default for js_RootedBase<T> {
2224
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/inherit_named.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct Wohoo {
1313
#[derive(Debug, Copy, Clone)]
1414
pub struct Weeee<T> {
1515
pub _base: T,
16+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
1617
}
1718
impl <T> Default for Weeee<T> {
1819
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

tests/expectations/tests/issue-584-stylo-template-analysis-panic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl Clone for A {
4343
#[derive(Debug, Copy, Clone)]
4444
pub struct e<c> {
4545
pub d: RefPtr<c>,
46+
_phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<c>>,
4647
}
4748
impl <c> Default for e<c> {
4849
fn default() -> Self { unsafe { ::std::mem::zeroed() } }

0 commit comments

Comments
 (0)