-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Minor standard library constification #55278
Conversation
Many of these are literally impossible to use in a const context. Why are we doing this? |
|
This comment has been minimized.
This comment has been minimized.
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.
Most of these seem fine in isolation.
I don't think it is good practice to make functions const fn while they have an argument that is not constructable in a const context.
Doing so will incentivise people to use unions or transmute to construct values in const contexts.
Agreed. Right now we haven't established a shared vision about what should be const. There's an argument to be made that anything that can be const should be, but that argument has not been made yet. I think as a first pass, to make this list more manageable, I'd like to see every function removed that can't actually be called in a const context already.
const fn isn't intended to mean that the function is referentially transparent. In particular, there's not a technical reason we could not allow mutable references as arguments to const fn, which would violate any referential transparency. |
That's my vision at least; I guess it might need to be RFCed to become shared.
Will do. :)
Mutable references would not really violate referential transparency in my view and I have no problem allowing them in |
@withoutboats Done. I've segregated the list; the ones that are removed are in their separate list in the OP. |
Removed changes to internal APIs per review. |
I am for this change. But we do have a ban on creating new stable public const fns in the libstd, and we haven't officially lifted that ban. cc @nikomatsakis I am reasonably sure that we have fixed everything that can go wrong with new public const fns. Reasons we didn't stabilize any new ones before:
I propose we lift the ban on adding new const fns to the libstd |
I believe the ban was originally imposed by the language team for the reasons @oli-obk mentioned.
Yes, let's :) |
The PR title currently says it's a "Minor standard library constification" but then the PR text says:
It looks like this has been scaled back since the first round but we need to be very careful to not over-promise on implementations here. Landing this PR will require an FCP signoff from the @rust-lang/libs team. I personally still think that the list is too ambitious and would like to see it cut back further. We can always add For example I don't think we should stabilize the
|
As aggressive as possible given what stable
Fair enough; I personally think we should ask the question "is there any reason we would introduce non-const behavior in this function making it not a
In the sense that someone has explicitly asked for it? No. The definition is fairly simple, so you should be able to copy it readily.
|
@withoutboats wrote
And I agree. I appreciate that is your vision, @Centril, but until we have adapted this vision as our shared vision, please do not use it to justify changes that we cannot take back later.
I think the question should be "is this useful?" -- as in, can you as the advocate of these features come up with an example where using this function as a Things related to concurrency (
|
I've trimmed the list along the lines per @alexcrichton's list; I kept |
My take: The one that I am torn about is |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Ok I think the list looks good now, but I'd like to get libs team signoff as well: @rfcbot fcp merge |
Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
f9b22c2
to
ac1c6b0
Compare
@alexcrichton rebased + minor other fixes. :) |
@bors: r+ |
📌 Commit ac1c6b0 has been approved by |
Minor standard library constification This PR makes some bits of the standard library into `const fn`s. I've tried to be as aggressive as I possibly could in the constification. The list is rather small due to how restrictive `const fn` is at the moment. r? @oli-obk cc @rust-lang/libs Stable public APIs affected: + [x] `Cell::as_ptr` + [x] `UnsafeCell::get` + [x] `char::is_ascii` + [x] `iter::empty` + [x] `ManuallyDrop::{new, into_inner}` + [x] `RangeInclusive::{start, end}` + [x] `NonNull::as_ptr` + [x] `{[T], str}::as_ptr` + [x] `Duration::{as_secs, subsec_millis, subsec_micros, subsec_nanos}` + [x] `CStr::as_ptr` + [x] `Ipv4Addr::is_unspecified` + [x] `Ipv6Addr::new` + [x] `Ipv6Addr::octets` Unstable public APIs affected: + [x] `Duration::{as_millis, as_micros, as_nanos, as_float_secs}` + [x] `Wrapping::{count_ones, count_zeros, trailing_zeros, rotate_left, rotate_right, swap_bytes, reverse_bits, from_be, from_le, to_be, to_le, leading_zeros, is_positive, is_negative, leading_zeros}` + [x] `core::convert::identity` -------------------------- ## Removed from list in first pass: Stable public APIs affected: + [ ] `BTree{Map, Set}::{len, is_empty}` + [ ] `VecDeque::is_empty` + [ ] `String::{is_empty, len}` + [ ] `FromUtf8Error::utf8_error` + [ ] `Vec<T>::{is_empty, len}` + [ ] `Layout::size` + [ ] `DecodeUtf16Error::unpaired_surrogate` + [ ] `core::fmt::{fill, width, precision, sign_plus, sign_minus, alternate, sign_aware_zero_pad}` + [ ] `panic::Location::{file, line, column}` + [ ] `{ChunksExact, RChunksExact}::remainder` + [ ] `Utf8Error::valid_up_to` + [ ] `VacantEntry::key` + [ ] `NulError::nul_position` + [ ] `IntoStringError::utf8_error` + [ ] `IntoInnerError::error` + [ ] `io::Chain::get_ref` + [ ] `io::Take::{limit, get_ref}` + [ ] `SocketAddrV6::{flowinfo, scope_id}` + [ ] `PrefixComponent::{kind, as_os_str}` + [ ] `Path::{ancestors, display}` + [ ] `WaitTimeoutResult::timed_out` + [ ] `Receiver::{iter, try_iter}` + [ ] `thread::JoinHandle::thread` + [ ] `SystemTimeError::duration` Unstable public APIs affected: + [ ] `core::fmt::Arguments::new_v1` + [ ] `core::fmt::Arguments::new_v1_formatted` + [ ] `Pin::{get_ref, into_ref}` + [ ] `Utf8Lossy::chunks` + [ ] `LocalWaker::as_waker` + [ ] `panic::PanicInfo::{internal_constructor, message, location}` + [ ] `panic::Location::{internal_constructor }` ## Removed from list in 2nd pass: Stable public APIs affected: + [ ] `LinkedList::{new, iter, is_empty, len}` + [ ] `mem::forget` + [ ] `Cursor::{new, get_ref, position}` + [ ] `io::{empty, repeat, sink}` + [ ] `PoisonError::new` + [ ] `thread::Builder::new` + [ ] `process::Stdio::{piped, inherit, null}` Unstable public APIs affected: + [ ] `io::Initializer::{zeroing, should_initialize}`
@Centril Is the top comment up-to-date? Mostly asking to help us down the line when making release notes. |
☀️ Test successful - status-appveyor, status-travis |
@Mark-Simulacrum I can't say it is with confidence but it probably is; I'd search for |
The top comment appears to be up-to-date. If needed for release notes, "affected" includes two types of changes -- prepending Stable public APIs affected: Unstable public APIs affected: The rest of the top comment prepends |
…=sfackler Make `u8::is_ascii` a stable `const fn` `char::is_ascii` was already stabilized as `const fn` in rust-lang#55278, so there is no reason for `u8::is_ascii` to go through an unstable period. cc @rust-lang/libs
This PR makes some bits of the standard library into
const fn
s.I've tried to be as aggressive as I possibly could in the constification.
The list is rather small due to how restrictive
const fn
is at the moment.r? @oli-obk cc @rust-lang/libs
Stable public APIs affected:
Cell::as_ptr
UnsafeCell::get
char::is_ascii
iter::empty
ManuallyDrop::{new, into_inner}
RangeInclusive::{start, end}
NonNull::as_ptr
{[T], str}::as_ptr
Duration::{as_secs, subsec_millis, subsec_micros, subsec_nanos}
CStr::as_ptr
Ipv4Addr::is_unspecified
Ipv6Addr::new
Ipv6Addr::octets
Unstable public APIs affected:
Duration::{as_millis, as_micros, as_nanos, as_float_secs}
Wrapping::{count_ones, count_zeros, trailing_zeros, rotate_left, rotate_right, swap_bytes, reverse_bits, from_be, from_le, to_be, to_le, leading_zeros, is_positive, is_negative, leading_zeros}
core::convert::identity
Removed from list in first pass:
Stable public APIs affected:
BTree{Map, Set}::{len, is_empty}
VecDeque::is_empty
String::{is_empty, len}
FromUtf8Error::utf8_error
Vec<T>::{is_empty, len}
Layout::size
DecodeUtf16Error::unpaired_surrogate
core::fmt::{fill, width, precision, sign_plus, sign_minus, alternate, sign_aware_zero_pad}
panic::Location::{file, line, column}
{ChunksExact, RChunksExact}::remainder
Utf8Error::valid_up_to
VacantEntry::key
NulError::nul_position
IntoStringError::utf8_error
IntoInnerError::error
io::Chain::get_ref
io::Take::{limit, get_ref}
SocketAddrV6::{flowinfo, scope_id}
PrefixComponent::{kind, as_os_str}
Path::{ancestors, display}
WaitTimeoutResult::timed_out
Receiver::{iter, try_iter}
thread::JoinHandle::thread
SystemTimeError::duration
Unstable public APIs affected:
core::fmt::Arguments::new_v1
core::fmt::Arguments::new_v1_formatted
Pin::{get_ref, into_ref}
Utf8Lossy::chunks
LocalWaker::as_waker
panic::PanicInfo::{internal_constructor, message, location}
panic::Location::{internal_constructor }
Removed from list in 2nd pass:
Stable public APIs affected:
LinkedList::{new, iter, is_empty, len}
mem::forget
Cursor::{new, get_ref, position}
io::{empty, repeat, sink}
PoisonError::new
thread::Builder::new
process::Stdio::{piped, inherit, null}
Unstable public APIs affected:
io::Initializer::{zeroing, should_initialize}