Skip to content

remove blanket-Freeze for PhantomData #114559

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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 0 additions & 1 deletion compiler/rustc_codegen_cranelift/example/mini_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ unsafe impl Sync for [u8; 16] {}
#[lang = "freeze"]
unsafe auto trait Freeze {}

unsafe impl<T: ?Sized> Freeze for PhantomData<T> {}
unsafe impl<T: ?Sized> Freeze for *const T {}
unsafe impl<T: ?Sized> Freeze for *mut T {}
unsafe impl<T: ?Sized> Freeze for &T {}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_codegen_gcc/example/mini_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ unsafe impl Sync for [u8; 16] {}
#[lang = "freeze"]
unsafe auto trait Freeze {}

unsafe impl<T: ?Sized> Freeze for PhantomData<T> {}
unsafe impl<T: ?Sized> Freeze for *const T {}
unsafe impl<T: ?Sized> Freeze for *mut T {}
unsafe impl<T: ?Sized> Freeze for &T {}
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ pub struct Rc<
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
> {
ptr: NonNull<RcBox<T>>,
phantom: PhantomData<RcBox<T>>,
phantom: PhantomData<Box<RcBox<T>>>,
alloc: A,
}

Expand Down Expand Up @@ -3400,7 +3400,7 @@ fn data_offset_align(align: usize) -> usize {
#[derive(Debug)]
pub struct UniqueRc<T> {
ptr: NonNull<RcBox<T>>,
phantom: PhantomData<RcBox<T>>,
phantom: PhantomData<Box<RcBox<T>>>,
}

impl<T> UniqueRc<T> {
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ pub struct Arc<
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
> {
ptr: NonNull<ArcInner<T>>,
phantom: PhantomData<ArcInner<T>>,
phantom: PhantomData<Box<ArcInner<T>>>,
alloc: A,
}

Expand Down
1 change: 0 additions & 1 deletion library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,6 @@ pub(crate) unsafe auto trait Freeze {}
impl<T: ?Sized> !Freeze for UnsafeCell<T> {}
marker_impls! {
unsafe Freeze for
{T: ?Sized} PhantomData<T>,
{T: ?Sized} *const T,
{T: ?Sized} *mut T,
{T: ?Sized} &T,
Expand Down
6 changes: 5 additions & 1 deletion library/core/src/ptr/unique.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::convert::From;
use crate::fmt;
use crate::marker::{PhantomData, Unsize};
use crate::marker::{Freeze, PhantomData, Unsize};
use crate::ops::{CoerceUnsized, DispatchFromDyn};
use crate::ptr::NonNull;

Expand Down Expand Up @@ -58,6 +58,10 @@ unsafe impl<T: Send + ?Sized> Send for Unique<T> {}
#[unstable(feature = "ptr_internals", issue = "none")]
unsafe impl<T: Sync + ?Sized> Sync for Unique<T> {}

/// `Unique` pointers are always `Freeze` since any interior mutability that might exist in `T` is
/// masked by the pointer indirection.
unsafe impl<T: ?Sized> Freeze for Unique<T> {}

#[unstable(feature = "ptr_internals", issue = "none")]
impl<T: Sized> Unique<T> {
/// Creates a new `Unique` that is dangling, but well-aligned.
Expand Down
2 changes: 1 addition & 1 deletion src/tools/tidy/src/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
const ENTRY_LIMIT: usize = 900;
// FIXME: The following limits should be reduced eventually.
const ISSUES_ENTRY_LIMIT: usize = 1891;
const ROOT_ENTRY_LIMIT: usize = 866;
const ROOT_ENTRY_LIMIT: usize = 867; // this includes files *and* folders!

const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
"rs", // test source files
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/unsafe-cell/phantom-data-not-freeze.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::cell::UnsafeCell;
use std::marker::PhantomData;

// This checks that `PhantomData` does not entirely mask interior mutability.

trait Trait {
const C: (u32, PhantomData<UnsafeCell<u32>>);
}

fn bar<T: Trait>() {
let x: &'static (u32, PhantomData<UnsafeCell<u32>>) = &T::C;
//~^ ERROR: dropped while borrowed
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/unsafe-cell/phantom-data-not-freeze.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/phantom-data-not-freeze.rs:11:60
|
LL | let x: &'static (u32, PhantomData<UnsafeCell<u32>>) = &T::C;
| -------------------------------------------- ^^^^ creates a temporary value which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
LL |
LL | }
| - temporary value is freed at the end of this statement

error: aborting due to previous error

For more information about this error, try `rustc --explain E0716`.