rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust/pin-init: remove workaround for type inference cycle
@ 2025-07-27 17:02 Suchit Karunakaran
  2025-07-27 17:28 ` Benno Lossin
  0 siblings, 1 reply; 5+ messages in thread
From: Suchit Karunakaran @ 2025-07-27 17:02 UTC (permalink / raw)
  To: lossin, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	a.hindborg, aliceryhl, tmgross, dakr, me, chrisi.schrefl,
	rust-for-linux
  Cc: skhan, linux-kernel-mentees, linux-kernel, Suchit Karunakaran

The `cast_pin_init` and `cast_init` functions previously used an
intermediate `let` binding before returning the result expression to work
around a Rust compiler issue causing type inference cycles. With the
minimum Rust compiler version for the kernel now at 1.78.0, where this
issue is fixed, the workaround is no longer needed. This patch removes the
unnecessary `let` variables and returns the expressions directly.

Signed-off-by: Suchit Karunakaran <suchitkarunakaran@gmail.com>
---
 rust/pin-init/src/lib.rs | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs
index 62e013a5cc20..cc244eeb19cd 100644
--- a/rust/pin-init/src/lib.rs
+++ b/rust/pin-init/src/lib.rs
@@ -1278,10 +1278,7 @@ unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
 pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
     // SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
     // requirements.
-    let res = unsafe { pin_init_from_closure(|ptr: *mut U| init.__pinned_init(ptr.cast::<T>())) };
-    // FIXME: remove the let statement once the nightly-MSRV allows it (1.78 otherwise encounters a
-    // cycle when computing the type returned by this function)
-    res
+    unsafe { pin_init_from_closure(|ptr: *mut U| init.__pinned_init(ptr.cast::<T>())) }
 }
 
 /// Changes the to be initialized type.
@@ -1294,10 +1291,7 @@ unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
 pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
     // SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
     // requirements.
-    let res = unsafe { init_from_closure(|ptr: *mut U| init.__init(ptr.cast::<T>())) };
-    // FIXME: remove the let statement once the nightly-MSRV allows it (1.78 otherwise encounters a
-    // cycle when computing the type returned by this function)
-    res
+    unsafe { init_from_closure(|ptr: *mut U| init.__init(ptr.cast::<T>())) }
 }
 
 /// An initializer that leaves the memory uninitialized.
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust/pin-init: remove workaround for type inference cycle
  2025-07-27 17:02 [PATCH] rust/pin-init: remove workaround for type inference cycle Suchit Karunakaran
@ 2025-07-27 17:28 ` Benno Lossin
  2025-07-27 17:44   ` Suchit K
  0 siblings, 1 reply; 5+ messages in thread
From: Benno Lossin @ 2025-07-27 17:28 UTC (permalink / raw)
  To: Suchit Karunakaran, ojeda, alex.gaynor, boqun.feng, gary,
	bjorn3_gh, a.hindborg, aliceryhl, tmgross, dakr, me,
	chrisi.schrefl, rust-for-linux
  Cc: skhan, linux-kernel-mentees, linux-kernel

On Sun Jul 27, 2025 at 7:02 PM CEST, Suchit Karunakaran wrote:
> The `cast_pin_init` and `cast_init` functions previously used an
> intermediate `let` binding before returning the result expression to work
> around a Rust compiler issue causing type inference cycles. With the
> minimum Rust compiler version for the kernel now at 1.78.0, where this
> issue is fixed, the workaround is no longer needed. This patch removes the
> unnecessary `let` variables and returns the expressions directly.
>
> Signed-off-by: Suchit Karunakaran <suchitkarunakaran@gmail.com>

I still encounter the cycle when compiling with 1.78.0, which version
did you test this with?

One of The errors I see after applying the patch on top of
`pin-init-next`:

    error[E0391]: cycle detected when computing type of opaque `cast_pin_init::{opaque#0}`
        --> rust/pin-init/src/lib.rs:1278:73
         |
    1278 | pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
         |                                                                         ^^^^^^^^^^^^^^^^^^
         |
    note: ...which requires borrow-checking `cast_pin_init`...
        --> rust/pin-init/src/lib.rs:1278:1
         |
    1278 | pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    note: ...which requires promoting constants in MIR for `cast_pin_init`...
        --> rust/pin-init/src/lib.rs:1278:1
         |
    1278 | pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    note: ...which requires const checking `cast_pin_init`...
        --> rust/pin-init/src/lib.rs:1278:1
         |
    1278 | pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
         | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         = note: ...which requires computing whether `cast_pin_init::{opaque#0}` is freeze...
         = note: ...which requires evaluating trait selection obligation `cast_pin_init::{opaque#0}: core::marker::Freeze`...
         = note: ...which again requires computing type of opaque `cast_pin_init::{opaque#0}`, completing the cycle
    note: cycle used when computing type of `cast_pin_init::{opaque#0}`
        --> rust/pin-init/src/lib.rs:1278:73
         |
    1278 | pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
         |                                                                         ^^^^^^^^^^^^^^^^^^
         = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

> ---
>  rust/pin-init/src/lib.rs | 10 ++--------
>  1 file changed, 2 insertions(+), 8 deletions(-)
>
> diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs
> index 62e013a5cc20..cc244eeb19cd 100644
> --- a/rust/pin-init/src/lib.rs
> +++ b/rust/pin-init/src/lib.rs
> @@ -1278,10 +1278,7 @@ unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
>  pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
>      // SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
>      // requirements.
> -    let res = unsafe { pin_init_from_closure(|ptr: *mut U| init.__pinned_init(ptr.cast::<T>())) };
> -    // FIXME: remove the let statement once the nightly-MSRV allows it (1.78 otherwise encounters a

Do note the comment mentioning that it is needed for version 1.78 here,
so I think this patch still needs to wait until we bump the minimum.

---
Cheers,
Benno

> -    // cycle when computing the type returned by this function)
> -    res
> +    unsafe { pin_init_from_closure(|ptr: *mut U| init.__pinned_init(ptr.cast::<T>())) }
>  }

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust/pin-init: remove workaround for type inference cycle
  2025-07-27 17:28 ` Benno Lossin
@ 2025-07-27 17:44   ` Suchit K
  2025-07-27 17:58     ` Benno Lossin
  0 siblings, 1 reply; 5+ messages in thread
From: Suchit K @ 2025-07-27 17:44 UTC (permalink / raw)
  To: Benno Lossin
  Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, a.hindborg,
	aliceryhl, tmgross, dakr, me, chrisi.schrefl, rust-for-linux,
	skhan, linux-kernel-mentees, linux-kernel

On Sun, 27 Jul 2025 at 22:58, Benno Lossin <lossin@kernel.org> wrote:
>
> On Sun Jul 27, 2025 at 7:02 PM CEST, Suchit Karunakaran wrote:
> > The `cast_pin_init` and `cast_init` functions previously used an
> > intermediate `let` binding before returning the result expression to work
> > around a Rust compiler issue causing type inference cycles. With the
> > minimum Rust compiler version for the kernel now at 1.78.0, where this
> > issue is fixed, the workaround is no longer needed. This patch removes the
> > unnecessary `let` variables and returns the expressions directly.
> >
> > Signed-off-by: Suchit Karunakaran <suchitkarunakaran@gmail.com>
>
> I still encounter the cycle when compiling with 1.78.0, which version
> did you test this with?
>

Oops, I just realised that I compiled it locally and forgot that I had
installed version 1.78.0 on my VM and not on my local system. I
actually compiled it with version 1.88.0. Sorry for the inconvenience.

> One of The errors I see after applying the patch on top of
> `pin-init-next`:
>
>     error[E0391]: cycle detected when computing type of opaque `cast_pin_init::{opaque#0}`
>         --> rust/pin-init/src/lib.rs:1278:73
>          |
>     1278 | pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
>          |                                                                         ^^^^^^^^^^^^^^^^^^
>          |
>     note: ...which requires borrow-checking `cast_pin_init`...
>         --> rust/pin-init/src/lib.rs:1278:1
>          |
>     1278 | pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
>          | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>     note: ...which requires promoting constants in MIR for `cast_pin_init`...
>         --> rust/pin-init/src/lib.rs:1278:1
>          |
>     1278 | pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
>          | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>     note: ...which requires const checking `cast_pin_init`...
>         --> rust/pin-init/src/lib.rs:1278:1
>          |
>     1278 | pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
>          | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>          = note: ...which requires computing whether `cast_pin_init::{opaque#0}` is freeze...
>          = note: ...which requires evaluating trait selection obligation `cast_pin_init::{opaque#0}: core::marker::Freeze`...
>          = note: ...which again requires computing type of opaque `cast_pin_init::{opaque#0}`, completing the cycle
>     note: cycle used when computing type of `cast_pin_init::{opaque#0}`
>         --> rust/pin-init/src/lib.rs:1278:73
>          |
>     1278 | pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
>          |                                                                         ^^^^^^^^^^^^^^^^^^
>          = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
>
> > ---
> >  rust/pin-init/src/lib.rs | 10 ++--------
> >  1 file changed, 2 insertions(+), 8 deletions(-)
> >
> > diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs
> > index 62e013a5cc20..cc244eeb19cd 100644
> > --- a/rust/pin-init/src/lib.rs
> > +++ b/rust/pin-init/src/lib.rs
> > @@ -1278,10 +1278,7 @@ unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
> >  pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
> >      // SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
> >      // requirements.
> > -    let res = unsafe { pin_init_from_closure(|ptr: *mut U| init.__pinned_init(ptr.cast::<T>())) };
> > -    // FIXME: remove the let statement once the nightly-MSRV allows it (1.78 otherwise encounters a
>
> Do note the comment mentioning that it is needed for version 1.78 here,
> so I think this patch still needs to wait until we bump the minimum.
>

Yup got it. I'll send the patch after the minimum version is
increased. Thanks for reviewing.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust/pin-init: remove workaround for type inference cycle
  2025-07-27 17:44   ` Suchit K
@ 2025-07-27 17:58     ` Benno Lossin
  2025-07-28  4:00       ` Suchit K
  0 siblings, 1 reply; 5+ messages in thread
From: Benno Lossin @ 2025-07-27 17:58 UTC (permalink / raw)
  To: Suchit K
  Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, a.hindborg,
	aliceryhl, tmgross, dakr, me, chrisi.schrefl, rust-for-linux,
	skhan, linux-kernel-mentees, linux-kernel

On Sun Jul 27, 2025 at 7:44 PM CEST, Suchit K wrote:
> On Sun, 27 Jul 2025 at 22:58, Benno Lossin <lossin@kernel.org> wrote:
>> On Sun Jul 27, 2025 at 7:02 PM CEST, Suchit Karunakaran wrote:
>> > The `cast_pin_init` and `cast_init` functions previously used an
>> > intermediate `let` binding before returning the result expression to work
>> > around a Rust compiler issue causing type inference cycles. With the
>> > minimum Rust compiler version for the kernel now at 1.78.0, where this
>> > issue is fixed, the workaround is no longer needed. This patch removes the
>> > unnecessary `let` variables and returns the expressions directly.
>> >
>> > Signed-off-by: Suchit Karunakaran <suchitkarunakaran@gmail.com>
>>
>> I still encounter the cycle when compiling with 1.78.0, which version
>> did you test this with?
>>
>
> Oops, I just realised that I compiled it locally and forgot that I had
> installed version 1.78.0 on my VM and not on my local system. I
> actually compiled it with version 1.88.0. Sorry for the inconvenience.

No worries.

>> Do note the comment mentioning that it is needed for version 1.78 here,
>> so I think this patch still needs to wait until we bump the minimum.
>>
>
> Yup got it. I'll send the patch after the minimum version is
> increased. Thanks for reviewing.

If you do, then please send the patch upstream at [1], that makes things
easier for me, thanks!

[1]: https://github.com/rust-for-linux/pin-init

---
Cheers,
Benno

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust/pin-init: remove workaround for type inference cycle
  2025-07-27 17:58     ` Benno Lossin
@ 2025-07-28  4:00       ` Suchit K
  0 siblings, 0 replies; 5+ messages in thread
From: Suchit K @ 2025-07-28  4:00 UTC (permalink / raw)
  To: Benno Lossin
  Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, a.hindborg,
	aliceryhl, tmgross, dakr, me, chrisi.schrefl, rust-for-linux,
	skhan, linux-kernel-mentees, linux-kernel

On Sun, 27 Jul 2025 at 23:28, Benno Lossin <lossin@kernel.org> wrote:
>
> On Sun Jul 27, 2025 at 7:44 PM CEST, Suchit K wrote:
> > On Sun, 27 Jul 2025 at 22:58, Benno Lossin <lossin@kernel.org> wrote:
> >> On Sun Jul 27, 2025 at 7:02 PM CEST, Suchit Karunakaran wrote:
> >> > The `cast_pin_init` and `cast_init` functions previously used an
> >> > intermediate `let` binding before returning the result expression to work
> >> > around a Rust compiler issue causing type inference cycles. With the
> >> > minimum Rust compiler version for the kernel now at 1.78.0, where this
> >> > issue is fixed, the workaround is no longer needed. This patch removes the
> >> > unnecessary `let` variables and returns the expressions directly.
> >> >
> >> > Signed-off-by: Suchit Karunakaran <suchitkarunakaran@gmail.com>
> >>
> >> I still encounter the cycle when compiling with 1.78.0, which version
> >> did you test this with?
> >>
> >
> > Oops, I just realised that I compiled it locally and forgot that I had
> > installed version 1.78.0 on my VM and not on my local system. I
> > actually compiled it with version 1.88.0. Sorry for the inconvenience.
>
> No worries.
>
> >> Do note the comment mentioning that it is needed for version 1.78 here,
> >> so I think this patch still needs to wait until we bump the minimum.
> >>
> >
> > Yup got it. I'll send the patch after the minimum version is
> > increased. Thanks for reviewing.
>
> If you do, then please send the patch upstream at [1], that makes things
> easier for me, thanks!
>
> [1]: https://github.com/rust-for-linux/pin-init
>

Sure, I’ll send the patch both here and upstream at [1]. Thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-07-28  4:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-27 17:02 [PATCH] rust/pin-init: remove workaround for type inference cycle Suchit Karunakaran
2025-07-27 17:28 ` Benno Lossin
2025-07-27 17:44   ` Suchit K
2025-07-27 17:58     ` Benno Lossin
2025-07-28  4:00       ` Suchit K

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).