Skip to content
Open
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: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ macrotest = "1.0"
prettyplease = { version = "0.2.37", features = ["verbatim"] }

[lints.rust]
stable_features = "allow"
non_ascii_idents = "deny"
unexpected_cfgs = { level = "warn", check-cfg = [
'cfg(UI_TESTS)',
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ actually does the initialization in the correct way. Here are the things to look
```rust
use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
use core::{
ptr::addr_of_mut,
marker::PhantomPinned,
cell::UnsafeCell,
pin::Pin,
Expand Down Expand Up @@ -199,7 +198,7 @@ impl RawFoo {
unsafe {
pin_init_from_closure(move |slot: *mut Self| {
// `slot` contains uninit memory, avoid creating a reference.
let foo = addr_of_mut!((*slot).foo);
let foo = &raw mut (*slot).foo;
let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();

// Initialize the `foo`
Expand Down
25 changes: 14 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use rustc_version::{version, Version};
use rustc_version::{version_meta, Channel, Version};

fn main() {
println!("cargo::rustc-check-cfg=cfg(RUSTC_LINT_REASONS_IS_STABLE)");
println!("cargo::rustc-check-cfg=cfg(RUSTC_NEW_UNINIT_IS_STABLE)");
println!("cargo::rustc-check-cfg=cfg(RUSTC_USE_FEATURE)");
println!("cargo::rustc-check-cfg=cfg(CONFIG_RUSTC_HAS_UNSAFE_PINNED)");
if version().unwrap() >= Version::parse("1.81.0").unwrap()
|| version().unwrap() >= Version::parse("1.81.0-nightly").unwrap()
{
println!("cargo:rustc-cfg=RUSTC_LINT_REASONS_IS_STABLE");
}
if version().unwrap() >= Version::parse("1.82.0").unwrap() {
println!("cargo:rustc-cfg=RUSTC_NEW_UNINIT_IS_STABLE");
println!("cargo:rustc-check-cfg=cfg(RUSTC_RAW_REF_OP_IS_STABLE)");

let meta = version_meta().unwrap();

let use_feature = meta.channel == Channel::Nightly || std::env::var("RUSTC_BOOTSTRAP").is_ok();
if use_feature {
println!("cargo:rustc-cfg=RUSTC_USE_FEATURE");
}
if version().unwrap() >= Version::parse("1.89.0-nightly").unwrap() {

if meta.semver >= Version::parse("1.89.0-nightly").unwrap() && use_feature {
println!("cargo:rustc-cfg=CONFIG_RUSTC_HAS_UNSAFE_PINNED");
}
if meta.semver >= Version::parse("1.82.0").unwrap() && use_feature {
println!("cargo:rustc-cfg=RUSTC_RAW_REF_OP_IS_STABLE");
}
}
2 changes: 2 additions & 0 deletions examples/big_struct_in_place.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![cfg_attr(not(RUSTC_RAW_REF_OP_IS_STABLE), feature(raw_ref_op))]

use pin_init::*;

// Struct with size over 1GiB
Expand Down
3 changes: 2 additions & 1 deletion examples/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#![allow(clippy::undocumented_unsafe_blocks)]
#![cfg_attr(feature = "alloc", feature(allocator_api))]
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]

use core::{
cell::Cell,
Expand Down
3 changes: 2 additions & 1 deletion examples/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#![allow(clippy::undocumented_unsafe_blocks)]
#![cfg_attr(feature = "alloc", feature(allocator_api))]
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]
#![allow(clippy::missing_safety_doc)]

use core::{
Expand Down
3 changes: 2 additions & 1 deletion examples/pthread_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// inspired by <https://github.com/nbdd0121/pin-init/blob/trunk/examples/pthread_mutex.rs>
#![allow(clippy::undocumented_unsafe_blocks)]
#![cfg_attr(feature = "alloc", feature(allocator_api))]
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]

#[cfg(not(windows))]
mod pthread_mtx {
Expand Down
3 changes: 2 additions & 1 deletion examples/static_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#![allow(clippy::undocumented_unsafe_blocks)]
#![cfg_attr(feature = "alloc", feature(allocator_api))]
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]
#![allow(unused_imports)]

use core::{
Expand Down
1 change: 1 addition & 0 deletions internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ syn = { version = "2.0.86", features = ["full", "parsing", "visit-mut"] }
rustc_version = "0.4"

[lints.rust]
stable_features = "allow"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(kernel)'] }
14 changes: 8 additions & 6 deletions internal/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use rustc_version::{version, Version};
use rustc_version::{version_meta, Channel};

fn main() {
println!("cargo::rustc-check-cfg=cfg(RUSTC_LINT_REASONS_IS_STABLE)");
if version().unwrap() >= Version::parse("1.81.0").unwrap()
|| version().unwrap() >= Version::parse("1.81.0-nightly").unwrap()
{
println!("cargo:rustc-cfg=RUSTC_LINT_REASONS_IS_STABLE");
println!("cargo::rustc-check-cfg=cfg(RUSTC_USE_FEATURE)");

let meta = version_meta().unwrap();

let use_feature = meta.channel == Channel::Nightly || std::env::var("RUSTC_BOOTSTRAP").is_ok();
if use_feature {
println!("cargo:rustc-cfg=RUSTC_USE_FEATURE");
}
}
8 changes: 4 additions & 4 deletions internal/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fn init_fields(
{
#value_prep
// SAFETY: TODO
unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
unsafe { #write(&raw mut (*#slot).#ident, #value_ident) };
}
#accessor
}
Expand All @@ -301,7 +301,7 @@ fn init_fields(
// return when an error/panic occurs.
// - We also use `#data` to require the correct trait (`Init` or `PinInit`)
// for `#ident`.
unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
unsafe { #data.#ident(&raw mut (*#slot).#ident, #init)? };
},
quote! {
// SAFETY: TODO
Expand All @@ -316,7 +316,7 @@ fn init_fields(
unsafe {
::pin_init::Init::__init(
#init,
::core::ptr::addr_of_mut!((*#slot).#ident),
&raw mut (*#slot).#ident,
)?
};
},
Expand Down Expand Up @@ -361,7 +361,7 @@ fn init_fields(
// SAFETY: We forget the guard later when initialization has succeeded.
let #guard = unsafe {
::pin_init::__internal::DropGuard::new(
::core::ptr::addr_of_mut!((*slot).#ident)
&raw mut (*slot).#ident
)
};
});
Expand Down
2 changes: 1 addition & 1 deletion internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

//! `pin-init` proc macros.

#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
// Documentation is done in the pin-init crate instead.
#![allow(missing_docs)]

Expand Down
15 changes: 6 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@
//! # #![feature(extern_types)]
//! use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
//! use core::{
//! ptr::addr_of_mut,
//! marker::PhantomPinned,
//! cell::UnsafeCell,
//! pin::Pin,
Expand Down Expand Up @@ -211,7 +210,7 @@
//! unsafe {
//! pin_init_from_closure(move |slot: *mut Self| {
//! // `slot` contains uninit memory, avoid creating a reference.
//! let foo = addr_of_mut!((*slot).foo);
//! let foo = &raw mut (*slot).foo;
//! let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();
//!
//! // Initialize the `foo`
Expand Down Expand Up @@ -264,12 +263,10 @@
//! [`impl Init<T, E>`]: crate::Init
//! [Rust-for-Linux]: https://rust-for-linux.com/

#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]
#![cfg_attr(
all(
any(feature = "alloc", feature = "std"),
not(RUSTC_NEW_UNINIT_IS_STABLE)
),
all(any(feature = "alloc", feature = "std"), RUSTC_USE_FEATURE),
feature(new_uninit)
)]
#![forbid(missing_docs, unsafe_op_in_unsafe_fn)]
Expand Down Expand Up @@ -755,7 +752,7 @@ macro_rules! stack_try_pin_init {
///
/// ```rust
/// # use pin_init::*;
/// # use core::{ptr::addr_of_mut, marker::PhantomPinned};
/// # use core::marker::PhantomPinned;
/// #[pin_data]
/// #[derive(Zeroable)]
/// struct Buf {
Expand All @@ -769,7 +766,7 @@ macro_rules! stack_try_pin_init {
/// let init = pin_init!(&this in Buf {
/// buf: [0; 64],
/// // SAFETY: TODO.
/// ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() },
/// ptr: unsafe { (&raw mut (*this.as_ptr()).buf).cast() },
/// pin: PhantomPinned,
/// });
/// let init = pin_init!(Buf {
Expand Down
2 changes: 1 addition & 1 deletion tests/alloc_fail.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg_attr(feature = "alloc", feature(allocator_api))]
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]

#[test]
#[cfg(feature = "alloc")]
Expand Down
3 changes: 2 additions & 1 deletion tests/cfgs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]

use pin_init::{pin_data, pin_init, PinInit};

Expand Down
3 changes: 2 additions & 1 deletion tests/const-generic-default.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]

use pin_init::*;

Expand Down
3 changes: 2 additions & 1 deletion tests/init-scope.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(dead_code)]
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]

use pin_init::*;

Expand Down
2 changes: 1 addition & 1 deletion tests/many_generics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![allow(dead_code)]

use core::{marker::PhantomPinned, pin::Pin};
Expand Down
17 changes: 6 additions & 11 deletions tests/ring_buf.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#![allow(clippy::undocumented_unsafe_blocks)]
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]
#![cfg_attr(feature = "alloc", feature(allocator_api))]

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::sync::Arc;
use core::{
convert::Infallible,
marker::PhantomPinned,
mem::MaybeUninit,
pin::Pin,
ptr::{self, addr_of_mut},
};
use core::{convert::Infallible, marker::PhantomPinned, mem::MaybeUninit, pin::Pin, ptr};
use pin_init::*;
#[cfg(feature = "std")]
use std::sync::Arc;
Expand Down Expand Up @@ -50,8 +45,8 @@ impl<T, const SIZE: usize> RingBuffer<T, SIZE> {
// SAFETY: The elements of the array can be uninitialized.
buffer <- unsafe { init_from_closure(|_| Ok::<_, Infallible>(())) },
// SAFETY: `this` is a valid pointer.
head: unsafe { addr_of_mut!((*this.as_ptr()).buffer).cast::<T>() },
tail: unsafe { addr_of_mut!((*this.as_ptr()).buffer).cast::<T>() },
head: unsafe { (&raw mut (*this.as_ptr()).buffer).cast::<T>() },
tail: unsafe { (&raw mut (*this.as_ptr()).buffer).cast::<T>() },
_pin: PhantomPinned,
})
}
Expand Down Expand Up @@ -112,7 +107,7 @@ impl<T, const SIZE: usize> RingBuffer<T, SIZE> {
unsafe fn advance(&mut self, ptr: *mut T) -> *mut T {
// SAFETY: ptr's offset from buffer is < SIZE
let ptr = unsafe { ptr.add(1) };
let origin: *mut _ = addr_of_mut!(self.buffer);
let origin: *mut _ = &raw mut (self.buffer);
let origin = origin.cast::<T>();
let offset = unsafe { ptr.offset_from(origin) };
if offset >= SIZE as isize {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]

#[test]
#[cfg_attr(not(UI_TESTS), ignore)]
Expand Down
23 changes: 0 additions & 23 deletions tests/ui/compile-fail/pin_data/no_pin_on_phantompinned.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,3 @@ error: The field `pin4` of type `PhantomPinned` only has an effect if it has the
|
9 | pin4: ::core::marker::PhantomPinned,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0425]: cannot find type `PhantomPinned` in this scope
--> tests/ui/compile-fail/pin_data/no_pin_on_phantompinned.rs:6:11
|
6 | pin1: PhantomPinned,
| ^^^^^^^^^^^^^ not found in this scope
|
help: consider importing this struct
|
1 + use std::marker::PhantomPinned;
|

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `marker`
--> tests/ui/compile-fail/pin_data/no_pin_on_phantompinned.rs:7:11
|
7 | pin2: marker::PhantomPinned,
| ^^^^^^ use of unresolved module or unlinked crate `marker`
|
= help: if you wanted to use a crate named `marker`, use `cargo add marker` to add it to your `Cargo.toml`
help: consider importing this module
|
1 + use std::marker;
|
1 change: 0 additions & 1 deletion tests/ui/expand/many_generics.expanded.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(lint_reasons)]
#![allow(dead_code)]
use core::{marker::PhantomPinned, pin::Pin};
use pin_init::*;
Expand Down
2 changes: 2 additions & 0 deletions tests/underscore.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(not(RUSTC_RAW_REF_OP_IS_STABLE), feature(raw_ref_op))]

use pin_init::{init, Init};

pub struct Foo {
Expand Down
7 changes: 4 additions & 3 deletions tests/zeroing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(lint_reasons))]
#![cfg_attr(RUSTC_USE_FEATURE, feature(raw_ref_op))]

use core::{marker::PhantomPinned, ptr::addr_of_mut};
use std::marker::PhantomPinned;

use pin_init::*;

Expand All @@ -22,7 +23,7 @@ impl Foo {
marks: {
let ptr = this.as_ptr();
// SAFETY: project from the NonNull<Foo> to the buf field
let ptr = unsafe { addr_of_mut!((*ptr).buf) }.cast::<u8>();
let ptr = unsafe { &raw mut (*ptr).buf }.cast::<u8>();
[ptr; MARKS]},
..Zeroable::init_zeroed()
})
Expand Down
Loading