Re: [PATCH] rust: alloc: allow coercion from `Box<T>` to `Box<dyn U>` if T implements U

From: Alexandre Courbot
Date: Tue Apr 08 2025 - 09:56:08 EST


On Tue Apr 8, 2025 at 10:40 PM JST, Alice Ryhl wrote:
> On Tue, Apr 8, 2025 at 3:19 PM Alexandre Courbot <acourbot@xxxxxxxxxx> wrote:
>>
>> On Tue Apr 8, 2025 at 7:35 PM JST, Miguel Ojeda wrote:
>> > On Tue, Apr 8, 2025 at 12:22 PM Benno Lossin <benno.lossin@xxxxxxxxx> wrote:
>> >>
>> >> You forgot to add the `A: Allocator` generic here and in the impl below.
>> >
>> > Yeah, for this sort of changes (ideally, all patches, really), please
>> > test with both the minimum supported version and the latest.
>>
>> Apologies for that, I had no idea how to build using a specific
>> toolchain and did the wrong thing, which is sending without proper
>> testing.
>>
>> I had some trouble finding how to 1) find out the minimum supported Rust
>> version, and 2) how to build using a specific toolchain.
>>
>> For 1) I eventually found a hardcoded version in
>> scripts/min-tool-version.sh ; and 2) is somehow documentated in the
>> Quick Start guide that mentions the `rustup override set stable`
>> command.
>>
>> I can send a patch against the Coding Guidelines adding a section to
>> encourage testing against the minimum version and explain how to force a
>> specific Rust version if you can confirm this would be helpful (and that
>> min-tool-version.sh is the correct way to check the minimum supported
>> Rust version).
>>
>> >
>> > For instance, there is another issue, `#[pointee]` is only applicable
>> > when you have the feature:
>> >
>> > error: cannot find attribute `pointee` in this scope
>> > --> rust/kernel/alloc/kbox.rs:66:18
>> > |
>> > 66 | pub struct Box<#[pointee] T: ?Sized, A:
>> > Allocator>(NonNull<T>, PhantomData<A>);
>>
>> Mmm that one is trickier to address - I don't know of a way to make this
>> `#[pointee]` attribute depedent on `CONFIG_RUSTC_HAS_COERCE_POINTEE`...
>> Only solution I see if having two separate declarations for `Box`, and
>> then we have the problem of bindings the rustdoc to the declaration that
>> is picked at build time... Any idea for how to best address this?
>
> This might work:
> struct Box<#[cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, pointee)] T>

I thought I'd tried that already and it failed, but giving it another go
now, it indeed *does* work. I must have been thrown off by a different
build error at that time. This solves everything then - thanks a lot!