Skip to content

[ER] Help for boxed array => slice/array #83410

Open
@leonardo-m

Description

@leonardo-m

This is wrong code:

#![allow(unused_variables)]
fn foo1(a: &[u32]) {}
fn foo2(a: &mut [u32]) {}
fn bar1(a: &[u32; 10]) {}
fn bar2(a: &mut [u32; 10]) {}
fn main() {
    let mut a = Box::new([0_u32; 10]);
    foo1(a);          // C01 Err
    foo1(&a);         // C02 Err
    foo1(&mut a);     // C03 Err
    foo1(&a[..]);     // C04 OK
    foo1(&mut a[..]); // C05 OK?

    foo2(a);          // C06 Err
    foo2(&a);         // C07 Err
    foo2(&mut a);     // C08 Err
    foo2(&a[..]);     // C09 Err
    foo2(&mut a[..]); // C10 OK

    bar1(a);          // C11 Err
    bar1(&a);         // C12 OK
    bar1(&mut a);     // C13 OK?
    bar1(&a[..]);     // C14 Err
    bar1(&mut a[..]); // C15 Err

    bar2(a);          // C16 Err
    bar2(&a);         // C17 Err
    bar2(&mut a);     // C18 OK
    bar2(&a[..]);     // C19 Err
    bar2(&mut a[..]); // C20 Err
}

rustc 1.53.0 nightly gives the error messages:

error[E0308]: mismatched types
 --> ...\test.rs:8:10
  |
8 |     foo1(a);          // C01
  |          ^ expected `&[u32]`, found struct `Box`
  |
  = note: expected reference `&[u32]`
                found struct `Box<[u32; 10]>`

error[E0308]: mismatched types
 --> ...\test.rs:9:10
  |
9 |     foo1(&a);         // C02
  |          ^^ expected slice `[u32]`, found struct `Box`
  |
  = note: expected reference `&[u32]`
             found reference `&Box<[u32; 10]>`

error[E0308]: mismatched types
  --> ...\test.rs:10:10
   |
10 |     foo1(&mut a);     // C03
   |          ^^^^^^ expected slice `[u32]`, found struct `Box`
   |
   = note:      expected reference `&[u32]`
           found mutable reference `&mut Box<[u32; 10]>`

error[E0308]: mismatched types
  --> ...\test.rs:14:10
   |
14 |     foo2(a);          // C06
   |          ^ expected `&mut [u32]`, found struct `Box`
   |
   = note: expected mutable reference `&mut [u32]`
                         found struct `Box<[u32; 10]>`

error[E0308]: mismatched types
  --> ...\test.rs:15:10
   |
15 |     foo2(&a);         // C07
   |          ^^ types differ in mutability
   |
   = note: expected mutable reference `&mut [u32]`
                      found reference `&Box<[u32; 10]>`

error[E0308]: mismatched types
  --> ...\test.rs:16:10
   |
16 |     foo2(&mut a);     // C08
   |          ^^^^^^ expected slice `[u32]`, found struct `Box`
   |
   = note: expected mutable reference `&mut [u32]`
              found mutable reference `&mut Box<[u32; 10]>`

error[E0308]: mismatched types
  --> ...\test.rs:17:10
   |
17 |     foo2(&a[..]);     // C09
   |          ^^^^^^ types differ in mutability
   |
   = note: expected mutable reference `&mut [u32]`
                      found reference `&[u32]`

error[E0308]: mismatched types
  --> ...\test.rs:20:10
   |
20 |     bar1(a);          // C11
   |          ^
   |          |
   |          expected `&[u32; 10]`, found struct `Box`
   |          help: consider borrowing here: `&a`
   |
   = note: expected reference `&[u32; 10]`
                 found struct `Box<[u32; 10]>`

error[E0308]: mismatched types
  --> ...\test.rs:23:10
   |
23 |     bar1(&a[..]);     // C14
   |          ^^^^^^ expected array `[u32; 10]`, found slice `[u32]`
   |
   = note: expected reference `&[u32; 10]`
              found reference `&[u32]`

error[E0308]: mismatched types
  --> ...\test.rs:24:10
   |
24 |     bar1(&mut a[..]); // C15
   |          ^^^^^^^^^^ expected array `[u32; 10]`, found slice `[u32]`
   |
   = note:      expected reference `&[u32; 10]`
           found mutable reference `&mut [u32]`

error[E0308]: mismatched types
  --> ...\test.rs:26:10
   |
26 |     bar2(a);          // C16
   |          ^
   |          |
   |          expected `&mut [u32; 10]`, found struct `Box`
   |          help: consider mutably borrowing here: `&mut a`
   |
   = note: expected mutable reference `&mut [u32; 10]`
                         found struct `Box<[u32; 10]>`

error[E0308]: mismatched types
  --> ...\test.rs:27:10
   |
27 |     bar2(&a);         // C17
   |          ^^ types differ in mutability
   |
   = note: expected mutable reference `&mut [u32; 10]`
                      found reference `&Box<[u32; 10]>`

error[E0308]: mismatched types
  --> ...\test.rs:29:10
   |
29 |     bar2(&a[..]);     // C19
   |          ^^^^^^ types differ in mutability
   |
   = note: expected mutable reference `&mut [u32; 10]`
                      found reference `&[u32]`

error[E0308]: mismatched types
  --> ...\test.rs:30:10
   |
30 |     bar2(&mut a[..]); // C20
   |          ^^^^^^^^^^ expected array `[u32; 10]`, found slice `[u32]`
   |
   = note: expected mutable reference `&mut [u32; 10]`
              found mutable reference `&mut [u32]`

error: aborting due to 14 previous errors

In all the wrong cases rustc could give as help the suggestion to use the syntax I've used in the correct cases (C4, C10, C12 and C18). (C11 and C16 already give the help).
Additionally rustc could warn in the cases of useless mut usage (C05 and C13) (See issue #83409 ).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions