]> git.lizzy.rs Git - rust.git/commit - src/tools/miri
Rollup merge of #81503 - henryboisdequin:fix-const-fn-arr-err-msg, r=estebank
authorJonas Schievink <jonasschievink@gmail.com>
Mon, 15 Feb 2021 15:06:47 +0000 (16:06 +0100)
committerGitHub <noreply@github.com>
Mon, 15 Feb 2021 15:06:47 +0000 (16:06 +0100)
commitf02f7b05b24543f0928f34c75aad2465e20094ca
tree4780cf8493f1d6a3c5f22c2af793a7f54ed8e860
parent9503ea19edbf01b9435e80e17d60ce1b88390116
parent64fe2c183d722ef3d2e9dee312de4b98aa6af9a0
Rollup merge of #81503 - henryboisdequin:fix-const-fn-arr-err-msg, r=estebank

Suggest to create a new `const` item if the `fn` in the array is a `const fn`

Fixes #73734. If the `fn` in the array repeat expression is a `const fn`, suggest creating a new `const` item. On nightly, suggest creating an inline `const` block. This PR also removes the `suggest_const_in_array_repeat_expressions` as it is no longer necessary.

Example:

```rust
fn main() {
    // Should not compile but hint to create a new const item (stable) or an inline const block (nightly)
    let strings: [String; 5] = [String::new(); 5];
    println!("{:?}", strings);
}

```

Gives this error:

```
error[E0277]: the trait bound `std::string::String: std::marker::Copy` is not satisfied
 --> $DIR/const-fn-in-vec.rs:3:32
  |
2 |     let strings: [String; 5] = [String::new(); 5];
  |                             ^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `String`
  |
  = note: the `Copy` trait is required because the repeated element will be copied
```

With this change, this is the error message:

```
error[E0277]: the trait bound `String: Copy` is not satisfied
  --> $DIR/const-fn-in-vec.rs:3:32
   |
LL |     let strings: [String; 5] = [String::new(); 5];
   |                                ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
   |
   = help: moving the function call to a new `const` item will resolve the error
```
compiler/rustc_mir/src/borrow_check/type_check/mod.rs
compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs