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

&mut [T;N] as *mut T does not compile, but &[T;N] as *const T does #24151

Closed
jagourq opened this issue Apr 7, 2015 · 8 comments · Fixed by #81479
Closed

&mut [T;N] as *mut T does not compile, but &[T;N] as *const T does #24151

jagourq opened this issue Apr 7, 2015 · 8 comments · Fixed by #81479
Labels
A-typesystem Area: The type system C-bug Category: This is a bug. P-low Low priority

Comments

@jagourq
Copy link
Contributor

jagourq commented Apr 7, 2015

unsafe {
    let my_num: &[i32; 2] = &[10, 20];
    let my_num: *const i32 = my_num as *const i32; // This works.
    println!("{}", *my_num.offset(1));
}
unsafe {
    let my_num: &mut [i32; 2] = &mut [10, 20];
    // error: mismatched types:
    //  expected `*mut i32`,
    //         found `&mut [i32; 2]`
    let my_num: *mut i32 = my_num as *mut i32;
    println!("{}", *my_num.offset(1));
}
@alexcrichton
Copy link
Member

cc @nrc, is this a coercion that's expected to work?

@alexcrichton
Copy link
Member

triage: I-nominated

@nrc
Copy link
Member

nrc commented Apr 7, 2015

The first cast is from &[i32; 2] to *const i32, the second is from &mut [i32; 2] to *mut i32. I'm not actually sure if this is meant to work, I guess it should. The intention here is to give a way to treat arrays like C-arrays and get a pointer to the start of the array. It seems that if the array is mutable, then the pointer should be too.

I recommend this is P-low - it is backwards compatible, rare, and just a bug by omission, really. The fix should be pretty easy though.

@alexcrichton
Copy link
Member

Ah I wasn't sure if this was intended, if it's actually intended then I don't mind removing the nomination as well!

@nikomatsakis
Copy link
Contributor

I'm not sure how intended this is, but it doesn't seem too harmful. ;)

@pnkfelix
Copy link
Member

pnkfelix commented Apr 9, 2015

P-low, (perhaps "just a bug", perhaps a minor feature.)

@pnkfelix pnkfelix added P-low Low priority and removed I-nominated labels Apr 9, 2015
@nikomatsakis nikomatsakis changed the title Changing *const and & to *mut and &mut in some cases prevents code from compiling. &mut [T;N] as *mut T does not compile, but &[T;N] as *const T does Apr 9, 2015
@steveklabnik steveklabnik added the A-typesystem Area: The type system label Jun 27, 2016
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@steveklabnik
Copy link
Member

Triage: no change

@osa1
Copy link
Contributor

osa1 commented Jan 28, 2021

This still happens today (with e32f372), but the error message is slightly different:

error[E0606]: casting `&mut [i32; 2]` as `*mut i32` is invalid
  --> test.rs:13:32
   |
13 |         let my_num: *mut i32 = my_num as *mut i32;
   |                                ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

#80258 is another *mut-related issue.

JohnTitor added a commit to JohnTitor/rust that referenced this issue Feb 11, 2021
Allow casting mut array ref to mut ptr

Allow casting mut array ref to mut ptr

We now allow two new casts:

- mut array reference to mut ptr. Example:

      let mut x: [usize; 2] = [0, 0];
      let p = &mut x as *mut usize;

  We allow casting const array references to const pointers so not
  allowing mut references to mut pointers was inconsistent.

- mut array reference to const ptr. Example:

      let mut x: [usize; 2] = [0, 0];
      let p = &mut x as *const usize;

  This was similarly inconsistent as we allow casting mut references to
  const pointers.

Existing test 'vector-cast-weirdness' updated to test both cases.

Fixes rust-lang#24151
JohnTitor added a commit to JohnTitor/rust that referenced this issue Feb 11, 2021
Allow casting mut array ref to mut ptr

Allow casting mut array ref to mut ptr

We now allow two new casts:

- mut array reference to mut ptr. Example:

      let mut x: [usize; 2] = [0, 0];
      let p = &mut x as *mut usize;

  We allow casting const array references to const pointers so not
  allowing mut references to mut pointers was inconsistent.

- mut array reference to const ptr. Example:

      let mut x: [usize; 2] = [0, 0];
      let p = &mut x as *const usize;

  This was similarly inconsistent as we allow casting mut references to
  const pointers.

Existing test 'vector-cast-weirdness' updated to test both cases.

Fixes rust-lang#24151
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 12, 2021
Allow casting mut array ref to mut ptr

Allow casting mut array ref to mut ptr

We now allow two new casts:

- mut array reference to mut ptr. Example:

      let mut x: [usize; 2] = [0, 0];
      let p = &mut x as *mut usize;

  We allow casting const array references to const pointers so not
  allowing mut references to mut pointers was inconsistent.

- mut array reference to const ptr. Example:

      let mut x: [usize; 2] = [0, 0];
      let p = &mut x as *const usize;

  This was similarly inconsistent as we allow casting mut references to
  const pointers.

Existing test 'vector-cast-weirdness' updated to test both cases.

Fixes rust-lang#24151
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 12, 2021
Allow casting mut array ref to mut ptr

Allow casting mut array ref to mut ptr

We now allow two new casts:

- mut array reference to mut ptr. Example:

      let mut x: [usize; 2] = [0, 0];
      let p = &mut x as *mut usize;

  We allow casting const array references to const pointers so not
  allowing mut references to mut pointers was inconsistent.

- mut array reference to const ptr. Example:

      let mut x: [usize; 2] = [0, 0];
      let p = &mut x as *const usize;

  This was similarly inconsistent as we allow casting mut references to
  const pointers.

Existing test 'vector-cast-weirdness' updated to test both cases.

Fixes rust-lang#24151
@bors bors closed this as completed in d64b749 Feb 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system C-bug Category: This is a bug. P-low Low priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants