-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Make Unique::as_ptr
, NonNull::dangling
and NonNull::cast
const
#58750
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
+136
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// run-pass | ||
|
||
#![feature(const_ptr_nonnull)] | ||
|
||
use std::ptr::NonNull; | ||
|
||
const DANGLING: NonNull<u32> = NonNull::dangling(); | ||
const CASTED: NonNull<u32> = NonNull::cast(NonNull::<i32>::dangling()); | ||
|
||
fn ident<T>(ident: T) -> T { | ||
ident | ||
} | ||
|
||
pub fn main() { | ||
assert_eq!(DANGLING, ident(NonNull::dangling())); | ||
assert_eq!(CASTED, ident(NonNull::dangling())); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// run-pass | ||
|
||
#![feature(ptr_internals)] | ||
|
||
use std::ptr::Unique; | ||
|
||
const PTR: *mut u32 = Unique::empty().as_ptr(); | ||
|
||
fn ident<T>(ident: T) -> T { | ||
ident | ||
} | ||
|
||
pub fn main() { | ||
assert_eq!(PTR, ident(Unique::<u32>::empty().as_ptr())); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/const-ptr-nonnull.rs:4:37 | ||
| | ||
LL | let x: &'static NonNull<u32> = &(NonNull::dangling()); | ||
| --------------------- ^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use | ||
| | | ||
| type annotation requires that borrow lasts for `'static` | ||
... | ||
LL | } | ||
| - temporary value is freed at the end of this statement | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/const-ptr-nonnull.rs:9:37 | ||
| | ||
LL | let x: &'static NonNull<u32> = &(non_null.cast()); | ||
| --------------------- ^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use | ||
| | | ||
| type annotation requires that borrow lasts for `'static` | ||
LL | //~^ ERROR borrowed value does not live long enough | ||
LL | } | ||
| - temporary value is freed at the end of this statement | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0716`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use std::ptr::NonNull; | ||
|
||
fn main() { | ||
let x: &'static NonNull<u32> = &(NonNull::dangling()); | ||
//~^ ERROR borrowed value does not live long enough | ||
|
||
let mut i: i32 = 10; | ||
let non_null = NonNull::new(&mut i).unwrap(); | ||
let x: &'static NonNull<u32> = &(non_null.cast()); | ||
//~^ ERROR borrowed value does not live long enough | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error[E0597]: borrowed value does not live long enough | ||
--> $DIR/const-ptr-nonnull.rs:4:37 | ||
| | ||
LL | let x: &'static NonNull<u32> = &(NonNull::dangling()); | ||
| ^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough | ||
... | ||
LL | } | ||
| - temporary value only lives until here | ||
| | ||
= note: borrowed value must be valid for the static lifetime... | ||
|
||
error[E0597]: borrowed value does not live long enough | ||
--> $DIR/const-ptr-nonnull.rs:9:37 | ||
| | ||
LL | let x: &'static NonNull<u32> = &(non_null.cast()); | ||
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough | ||
LL | //~^ ERROR borrowed value does not live long enough | ||
LL | } | ||
| - temporary value only lives until here | ||
| | ||
= note: borrowed value must be valid for the static lifetime... | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0597`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/const-ptr-unique.rs:8:33 | ||
| | ||
LL | let x: &'static *mut u32 = &(unique.as_ptr()); | ||
| ----------------- ^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use | ||
| | | ||
| type annotation requires that borrow lasts for `'static` | ||
LL | //~^ ERROR borrowed value does not live long enough | ||
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`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(ptr_internals)] | ||
|
||
use std::ptr::Unique; | ||
|
||
fn main() { | ||
let mut i: u32 = 10; | ||
let unique = Unique::new(&mut i).unwrap(); | ||
let x: &'static *mut u32 = &(unique.as_ptr()); | ||
//~^ ERROR borrowed value does not live long enough | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0597]: borrowed value does not live long enough | ||
--> $DIR/const-ptr-unique.rs:8:33 | ||
| | ||
LL | let x: &'static *mut u32 = &(unique.as_ptr()); | ||
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough | ||
LL | //~^ ERROR borrowed value does not live long enough | ||
LL | } | ||
| - temporary value only lives until here | ||
| | ||
= note: borrowed value must be valid for the static lifetime... | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0597`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @Centril is it too dangerous that just adding a
const
doesn't require anything like a#[const_stable(since = "1.2.3")]
attribute?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dangerous from what perspective? If it's used in a min-const-fn it will also need to be min-const-fn... or do you mean from the POV of accidental stabilization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, accidental stabilization. Like making something stable that is already
const fn
stabilizes the constness along with it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean any stabilized const fn should have something like a
#[const_stable]
attribute?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah... idk if I find it risky rising to the level of dangerous but there's some risk, no doubt. @varkor wanted it from a different perspective (#57562 (comment)) but I thought it too much of an annotation burden. However, from the POV of accidental stabilization,
#[const_stable(...)]
makes sense to me.Can you open up an issue and cc me and varkor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TimDiekmann Yeah, that's the idea.