Skip to content
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

Tracking Issue for const_convert #88674

Open
2 of 5 tasks
usbalbin opened this issue Sep 5, 2021 · 3 comments
Open
2 of 5 tasks

Tracking Issue for const_convert #88674

usbalbin opened this issue Sep 5, 2021 · 3 comments
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. F-const_trait_impl `#![feature(const_trait_impl)]` T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@usbalbin
Copy link
Contributor

usbalbin commented Sep 5, 2021

Feature gate: #![feature(const_convert)]

This is a tracking issue for constifying some conversions (see #87852 for numeric conversions). Among other things this enables using the ?-operator on Option and Result in const contexts for trivial conversions. Note that this does not add any new implementations, it only constifies the existing ones.

Simple example:

const fn foo() -> Result<(), FooError> {
    let bar = try_thing()?; // A function that returns Result<_, FooError>
    do_stuff_with(bar)?;    // A function that returns Result<_, FooError>
    Ok(())
}

Public API

Constifies the following impls

impl<T, U> const Into<U> for T where U: ~const From<T>;
impl<T> const From<T> for T;

impl<T> const ops::Try for Option<T>;
impl<T> const ops::FromResidual for Option<T>;

impl<T, E> const ops::Try for Result<T, E>;
impl<T, E, F> const ops::FromResidual<Result<convert::Infallible, E>> for Result<T, F> where F: ~const From<E>;


impl<T: ?Sized, U: ?Sized> const AsRef<U> for &T where T: ~const AsRef<U>;
impl<T: ?Sized, U: ?Sized> const AsRef<U> for &mut T where T: ~const AsRef<U>;
impl<T: ?Sized, U: ?Sized> const AsMut<U> for &mut T where T: ~const AsMut<U>;

impl<T, U> const TryInto<U> for T where U: ~const TryFrom<T>;
impl<T, U> const TryFrom<U> for T where U: ~const Into<T>;

Steps / History

Unresolved Questions

  • None yet.
@usbalbin usbalbin added C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Sep 5, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 30, 2021
Constify ?-operator for Result and Option

Try to make `?`-operator usable in `const fn` with `Result` and `Option`, see rust-lang#74935 . Note that the try-operator itself was constified in rust-lang#87237.

TODO
* [x] Add tests for const T -> T conversions
* [x] cleanup commits
* [x] Remove `#![allow(incomplete_features)]`
* [?] Await decision in rust-lang#86808 - I'm not sure
* [x] Await support for parsing `~const` in bootstrapping compiler
* [x] Tracking issue(s)? - rust-lang#88674
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Oct 19, 2021
Make more `From` impls `const` (libcore)

Adding `const` to `From` implementations in the core. `rustc_const_unstable` attribute is not added to unstable implementations.

Tracking issue: rust-lang#88674

<details>
<summary>Done</summary><div>

- `T` from `T`
- `T` from `!`
- `Option<T>` from `T`
- `Option<&T>` from `&Option<T>`
- `Option<&mut T>` from `&mut Option<T>`
- `Cell<T>` from `T`
- `RefCell<T>` from `T`
- `UnsafeCell<T>` from `T`
- `OnceCell<T>` from `T`
- `Poll<T>` from `T`
- `u32` from `char`
- `u64` from `char`
- `u128` from `char`
- `char` from `u8`
- `AtomicBool` from `bool`
- `AtomicPtr<T>` from `*mut T`
- `AtomicI(bits)` from `i(bits)`
- `AtomicU(bits)` from `u(bits)`
- `i(bits)` from `NonZeroI(bits)`
- `u(bits)` from `NonZeroU(bits)`
- `NonNull<T>` from `Unique<T>`
- `NonNull<T>` from `&T`
- `NonNull<T>` from `&mut T`
- `Unique<T>` from `&mut T`
- `Infallible` from `!`
- `TryIntError` from `!`
- `TryIntError` from `Infallible`
- `TryFromSliceError` from `Infallible`
- `FromResidual for Option<T>`
</div></details>

<details>
<summary>Remaining</summary><dev>

- `NonZero` from `NonZero`
These can't be made const at this time because these use Into::into.
https://github.com/rust-lang/rust/blob/master/library/core/src/convert/num.rs#L393

- `std`, `alloc`
There may still be many implementations that can be made `const`.
</div></details>
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 19, 2021
Make more `From` impls `const` (libcore)

Adding `const` to `From` implementations in the core. `rustc_const_unstable` attribute is not added to unstable implementations.

Tracking issue: rust-lang#88674

<details>
<summary>Done</summary><div>

- `T` from `T`
- `T` from `!`
- `Option<T>` from `T`
- `Option<&T>` from `&Option<T>`
- `Option<&mut T>` from `&mut Option<T>`
- `Cell<T>` from `T`
- `RefCell<T>` from `T`
- `UnsafeCell<T>` from `T`
- `OnceCell<T>` from `T`
- `Poll<T>` from `T`
- `u32` from `char`
- `u64` from `char`
- `u128` from `char`
- `char` from `u8`
- `AtomicBool` from `bool`
- `AtomicPtr<T>` from `*mut T`
- `AtomicI(bits)` from `i(bits)`
- `AtomicU(bits)` from `u(bits)`
- `i(bits)` from `NonZeroI(bits)`
- `u(bits)` from `NonZeroU(bits)`
- `NonNull<T>` from `Unique<T>`
- `NonNull<T>` from `&T`
- `NonNull<T>` from `&mut T`
- `Unique<T>` from `&mut T`
- `Infallible` from `!`
- `TryIntError` from `!`
- `TryIntError` from `Infallible`
- `TryFromSliceError` from `Infallible`
- `FromResidual for Option<T>`
</div></details>

<details>
<summary>Remaining</summary><dev>

- `NonZero` from `NonZero`
These can't be made const at this time because these use Into::into.
https://github.com/rust-lang/rust/blob/master/library/core/src/convert/num.rs#L393

- `std`, `alloc`
There may still be many implementations that can be made `const`.
</div></details>
@clarfonthey
Copy link
Contributor

clarfonthey commented Dec 29, 2021

Note: impl<T, U> const Into<U> for T where U: ~const From<T>; is not actually included here; I add it with #92382.

Just kidding; I forgot to rebase when I thought I had. ><

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 14, 2022
Extend const_convert to rest of blanket core::convert impls

This adds constness to all the blanket impls in `core::convert` under the existing `const_convert` feature, tracked by rust-lang#88674.

Existing impls under that feature:

```rust
impl<T> const From<T> for T;
impl<T, U> const Into<U> for T where U: ~const From<T>;

impl<T> const ops::Try for Option<T>;
impl<T> const ops::FromResidual for Option<T>;

impl<T, E> const ops::Try for Result<T, E>;
impl<T, E, F> const ops::FromResidual<Result<convert::Infallible, E>> for Result<T, F> where F: ~const From<E>;
```

Additional impls:

```rust
impl<T: ?Sized, U: ?Sized> const AsRef<U> for &T where T: ~const AsRef<U>;
impl<T: ?Sized, U: ?Sized> const AsRef<U> for &mut T where T: ~const AsRef<U>;
impl<T: ?Sized, U: ?Sized> const AsMut<U> for &mut T where T: ~const AsMut<U>;

impl<T, U> const TryInto<U> for T where U: ~const TryFrom<T>;
impl<T, U> const TryFrom<U> for T where U: ~const Into<T>;
```
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 15, 2022
Extend const_convert to rest of blanket core::convert impls

This adds constness to all the blanket impls in `core::convert` under the existing `const_convert` feature, tracked by rust-lang#88674.

Existing impls under that feature:

```rust
impl<T> const From<T> for T;
impl<T, U> const Into<U> for T where U: ~const From<T>;

impl<T> const ops::Try for Option<T>;
impl<T> const ops::FromResidual for Option<T>;

impl<T, E> const ops::Try for Result<T, E>;
impl<T, E, F> const ops::FromResidual<Result<convert::Infallible, E>> for Result<T, F> where F: ~const From<E>;
```

Additional impls:

```rust
impl<T: ?Sized, U: ?Sized> const AsRef<U> for &T where T: ~const AsRef<U>;
impl<T: ?Sized, U: ?Sized> const AsRef<U> for &mut T where T: ~const AsRef<U>;
impl<T: ?Sized, U: ?Sized> const AsMut<U> for &mut T where T: ~const AsMut<U>;

impl<T, U> const TryInto<U> for T where U: ~const TryFrom<T>;
impl<T, U> const TryFrom<U> for T where U: ~const Into<T>;
```
nipunn1313 added a commit to nipunn1313/rust that referenced this issue Jul 25, 2022
These implementations are trivially const - since they only create the enum structure
without running any non-const code.

const_convert is tracked by rust-lang#88674
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 22, 2022
…low, r=scottmcm

Extend const_convert with const {FormResidual, Try} for ControlFlow.

Very small change so I just used the existing `const_convert` feature flag.  rust-lang#88674
Newly const API:
```
impl<B, C> const ops::Try for ControlFlow<B, C>;
impl<B, C> const ops::FromResidual for ControlFlow<B, C>;
```

`@usbalbin` I hope it is ok that I added to your feature.
@rust-lang rust-lang deleted a comment from atifnimran Dec 25, 2022
@fee1-dead fee1-dead added the F-const_trait_impl `#![feature(const_trait_impl)]` label Mar 24, 2023
@G33KatWork
Copy link

With commit 76dbe29 the feature const_convert has been completely removed from the core library. I understand that it's unstable, but it's also not exactly documented why it got removed. I would've at least expected a discussion about this in this tracking issue.

Does anybody know more about this?

@usbalbin
Copy link
Contributor Author

usbalbin commented Apr 22, 2023

I believe that might be #110395

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. F-const_trait_impl `#![feature(const_trait_impl)]` T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants