Skip to content

Conversation

@jplatte
Copy link
Contributor

@jplatte jplatte commented Mar 31, 2022

I recently noticed that arrays don't actually implement Deref and DerefMut (the slice coercion works through compiler magic, presumably: CoerceUnsized is also not implemented AFAICT).

With this PR, I want to expose the idea of adding these impls to more people. I already noticed that this will be blocked on const trait implementations (#67792) unless there is a way to bypass the error when #[rustc_const_unstable(…)] is omitted.

@rust-highfive
Copy link
Contributor

r? @joshtriplett

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 31, 2022
@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
    Checking rand v0.7.3
    Checking std v0.0.0 (/checkout/library/std)
    Checking alloc v0.0.0 (/checkout/library/alloc)
    Checking core v0.0.0 (/checkout/library/core)
error: `std::array::<impl Deref for [T; N]>::deref` is not yet stable as a const fn
  |
  |
6 |     unsafe { (DATA.as_ptr() as *const u8).add(1) as *const u16 }
  |
  = help: add `#![feature(const_array_deref)]` to the crate attributes to enable


error: `std::array::<impl DerefMut for [T; N]>::deref_mut` is not yet stable as a const fn
   |
   |
70 |             let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
   |
   = help: add `#![feature(const_array_deref)]` to the crate attributes to enable


error: `std::array::<impl DerefMut for [T; N]>::deref_mut` is not yet stable as a const fn
   |
   |
94 |             let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
   |
   = help: add `#![feature(const_array_deref)]` to the crate attributes to enable


error: `std::array::<impl DerefMut for [T; N]>::deref_mut` is not yet stable as a const fn
   |
46 |             write_bytes(arr.as_mut_ptr(), 0, 2);
   |                         ^^^^^^^^^^^^^^^^
   |
   |
   = help: add `#![feature(const_array_deref)]` to the crate attributes to enable

error: `std::array::<impl DerefMut for [T; N]>::deref_mut` is not yet stable as a const fn
   |
58 |             write_bytes(arr.as_mut_ptr(), 1, 2);
   |                         ^^^^^^^^^^^^^^^^
   |
   |
   = help: add `#![feature(const_array_deref)]` to the crate attributes to enable

error: `std::array::<impl DerefMut for [T; N]>::deref_mut` is not yet stable as a const fn
    |
    |
257 |         let ptr = xs.as_mut_ptr();
    |
    = help: add `#![feature(const_array_deref)]` to the crate attributes to enable


error: `std::array::<impl Deref for [T; N]>::deref` is not yet stable as a const fn
    |
    |
259 |             ptr.write_bytes(5u8, xs.len());
    |
    = help: add `#![feature(const_array_deref)]` to the crate attributes to enable

error: could not compile `core` due to 7 previous errors

@lcnr
Copy link
Contributor

lcnr commented Mar 31, 2022

see #92652 (comment) for a previous attempt and for why that might not be desired.

@jplatte
Copy link
Contributor Author

jplatte commented Mar 31, 2022

Hm, well I don't have a strong argument for why this should exist, only a vague notion of "can be useful for generic code". Should I just close?

@lcnr
Copy link
Contributor

lcnr commented Mar 31, 2022

I personally think this isn't something we want, so I would say yeah 😅

Array to slice is an unsize coercion, not a dereference, so using Deref for that isn't the right choice imo. Deref for Vec is already not ideal '^^

@jplatte jplatte closed this Mar 31, 2022
@jplatte jplatte deleted the array-deref branch March 31, 2022 14:17
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants