Skip to content

Conversation

clarfonthey
Copy link
Contributor

@clarfonthey clarfonthey commented Aug 11, 2025

This is the first part of #144289 being split into smaller pieces. It adds/moves constness of several traits under the const_convert feature:

  • From
  • Into
  • TryFrom
  • TryInto
  • FromStr
  • AsRef
  • AsMut
  • Borrow
  • BorrowMut
  • Deref
  • DerefMut

There are a few methods that are intrinsically tied to these traits which I've included in the feature. Particularly, those which are wrappers over AsRef:

  • ByteStr::new (unstable under bstr feature)
  • OsStr::new
  • Path::new

Those which directly use Into:

  • Result::into_ok
  • Result::into_err

And those which use Deref and DerefMut:

  • Pin::as_ref
  • Pin::as_mut
  • Pin::as_deref_mut

The parts which are missing from this PR are:

  • Anything that involves heap-allocated types
  • Making any method const than the ones listed above
  • Anything that could rely on the above, or could rely on system-specific code for OsStr or Path (note: this mostly makes these methods useless since str doesn't implement AsRef<OsStr> yet, but it's better to track the method for now and add impls later, IMHO)

r? @tgross35 (who mostly already reviewed this)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 11, 2025
#![feature(const_default)]
#![feature(const_eval_select)]
#![feature(const_heap)]
#![feature(const_trait_impl)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was listed under library features, and is actually a language feature. Since the diff doesn't make that clear.

Comment on lines +117 to +121
// FIXME(const-hack): this should use map_err instead
match u8::try_from(u32::from(c)) {
Ok(b) => Ok(b),
Err(_) => Err(TryFromCharError(())),
}
Copy link
Contributor Author

@clarfonthey clarfonthey Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mentioned not wanting to add any more const-hacks, but I figured this one (+ for u16 below) was okay because we're already going to have to fix a lot of const code once things are stable, and I think that simple char <-> integer conversions are useful enough to get on nightly sooner. Especially since const closures seem pretty far off at the moment.

@bors
Copy link
Collaborator

bors commented Aug 13, 2025

☔ The latest upstream changes (presumably #145334) made this pull request unmergeable. Please resolve the merge conflicts.

@clarfonthey clarfonthey force-pushed the const-convert-initial branch from 365fcde to 97c330f Compare August 13, 2025 16:40
@clarfonthey
Copy link
Contributor Author

Rebasing over #144847 since that one conflicts with this one, which is why I was going to do this one before I got to that one, but, it's in the queue now, so, 🤷🏻.

Only changes with the rebase are eliminating more additions of const_from which is renamed to const_convert.

@clarfonthey clarfonthey force-pushed the const-convert-initial branch from 97c330f to 4ebb4b7 Compare August 17, 2025 18:58
@rustbot
Copy link
Collaborator

rustbot commented Aug 17, 2025

This PR was rebased onto a different master commit! Check out the changes with our range-diff.

@bors
Copy link
Collaborator

bors commented Aug 20, 2025

☔ The latest upstream changes (presumably #145644) made this pull request unmergeable. Please resolve the merge conflicts.

@clarfonthey
Copy link
Contributor Author

Will rebase once that PR gets merged. I can mark it as blocked, but it still can be reviewed before to verify you're comfortable with the changes.

@clarfonthey clarfonthey force-pushed the const-convert-initial branch from 4ebb4b7 to 4bcf7bd Compare August 21, 2025 00:47
@rustbot

This comment has been minimized.

@clarfonthey
Copy link
Contributor Author

(Un-rebased over that PR since it's no longer in the queue.)

Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 28, 2025
…oshtriplett

`const`ify (the unstable) `str::as_str`

Tracking issue: rust-lang#130366

The method was not initially marked `const` presumably because it is only useful with `Deref`. But now that const traits seem to be a thing that can actually become real, why not make it `const`?

PR `const`ifying `Deref`: rust-lang#145279
rust-timer added a commit that referenced this pull request Aug 28, 2025
Rollup merge of #145930 - GrigorenkoPV:const_str_as_str, r=joshtriplett

`const`ify (the unstable) `str::as_str`

Tracking issue: #130366

The method was not initially marked `const` presumably because it is only useful with `Deref`. But now that const traits seem to be a thing that can actually become real, why not make it `const`?

PR `const`ifying `Deref`: #145279
@clarfonthey clarfonthey force-pushed the const-convert-initial branch from 4bcf7bd to 66dcd76 Compare August 29, 2025 12:52
@rustbot
Copy link
Collaborator

rustbot commented Aug 29, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

+ error[E0658]: use of unstable const library feature `const_convert`
+   --> $DIR/reservation-impl-ice.rs:4:31
+    |
+ LL | const fn impls_from<T: ~const From<!>>() {}
+    |                        ------ ^^^^^^^
+    |                        |
+    |                        trait is not stable as const yet
+    |
+    = note: see issue #143773 <https://github.com/rust-lang/rust/issues/143773> for more information
+    = help: add `#![feature(const_convert)]` to the crate attributes to enable
+    = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+ 
+ error[E0635]: unknown feature `const_from`
+   --> $DIR/reservation-impl-ice.rs:2:12
+    |
+ LL | #![feature(const_from, never_type, const_trait_impl)]
+    |            ^^^^^^^^^^
+ 
1 error[E0277]: the trait bound `(): From<!>` is not satisfied
2   --> $DIR/reservation-impl-ice.rs:9:18
3    |

20 LL | const fn impls_from<T: ~const From<!>>() {}
21    |                        ^^^^^^^^^^^^^^ required by this bound in `impls_from`
22 
- error: aborting due to 1 previous error
+ error: aborting due to 3 previous errors
24 
- For more information about this error, try `rustc --explain E0277`.
---
-   --> /checkout/tests/ui/traits/const-traits/reservation-impl-ice.rs:2:12
+ error[E0658]: use of unstable const library feature `const_convert`
+   --> $DIR/reservation-impl-ice.rs:4:31
+    |
+ LL | const fn impls_from<T: ~const From<!>>() {}
+    |                        ------ ^^^^^^^
+    |                        |
+    |                        trait is not stable as const yet
+    |
+    = note: see issue #143773 <https://github.com/rust-lang/rust/issues/143773> for more information
+    = help: add `#![feature(const_convert)]` to the crate attributes to enable
+    = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+ 
+ error[E0635]: unknown feature `const_from`
+   --> $DIR/reservation-impl-ice.rs:2:12
+    |
+ LL | #![feature(const_from, never_type, const_trait_impl)]
---
To only update this specific test, also pass `--test-args traits/const-traits/reservation-impl-ice.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/traits/const-traits/reservation-impl-ice.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/traits/const-traits/reservation-impl-ice" "-A" "unused" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "-Znext-solver"
stdout: none
--- stderr -------------------------------
error[E0658]: use of unstable const library feature `const_convert`
##[error]  --> /checkout/tests/ui/traits/const-traits/reservation-impl-ice.rs:4:31
   |
LL | const fn impls_from<T: ~const From<!>>() {}
   |                        ------ ^^^^^^^
   |                        |
   |                        trait is not stable as const yet
   |
   = note: see issue #143773 <https://github.com/rust-lang/rust/issues/143773> for more information
   = help: add `#![feature(const_convert)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0635]: unknown feature `const_from`
##[error]  --> /checkout/tests/ui/traits/const-traits/reservation-impl-ice.rs:2:12
   |
LL | #![feature(const_from, never_type, const_trait_impl)]
   |            ^^^^^^^^^^

error[E0277]: the trait bound `(): From<!>` is not satisfied
##[error]  --> /checkout/tests/ui/traits/const-traits/reservation-impl-ice.rs:9:18
   |
LL |     impls_from::<()>();
   |                  ^^ the trait `From<!>` is not implemented for `()`
   |
   = help: the following other types implement trait `From<T>`:
             `(T, T)` implements `From<[T; 2]>`
             `(T, T, T)` implements `From<[T; 3]>`
             `(T, T, T, T)` implements `From<[T; 4]>`
             `(T, T, T, T, T)` implements `From<[T; 5]>`
             `(T, T, T, T, T, T)` implements `From<[T; 6]>`
             `(T, T, T, T, T, T, T)` implements `From<[T; 7]>`
             `(T, T, T, T, T, T, T, T)` implements `From<[T; 8]>`
             `(T, T, T, T, T, T, T, T, T)` implements `From<[T; 9]>`
           and 4 others
note: required by a bound in `impls_from`
  --> /checkout/tests/ui/traits/const-traits/reservation-impl-ice.rs:4:24
   |
LL | const fn impls_from<T: ~const From<!>>() {}
   |                        ^^^^^^^^^^^^^^ required by this bound in `impls_from`

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0277, E0635, E0658.
For more information about an error, try `rustc --explain E0277`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants