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

Suggest const block, instead of const item, for array repeat of non-Copy element #126894

Closed
dtolnay opened this issue Jun 24, 2024 · 5 comments
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Jun 24, 2024

Code

struct Thing;

fn main() {
    let _ = [Thing; 2];
}

Current output

error[E0277]: the trait bound `Thing: Copy` is not satisfied
 --> src/main.rs:4:14
  |
4 |     let _ = [Thing; 2];
  |              ^^^^^ the trait `Copy` is not implemented for `Thing`
  |
  = note: the `Copy` trait is required because this value will be copied for each element of the array
  = help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information
help: consider annotating `Thing` with `#[derive(Copy)]`
  |
1 + #[derive(Copy)]
2 | struct Thing;
  |
help: consider creating a new `const` item and initializing it with the result of the constructor to be used in the repeat position
  |
4 ~     const ARRAY_REPEAT_VALUE: Thing = Thing;
5 ~     let _ = [ARRAY_REPEAT_VALUE; 2];
  |

Rationale and extra context

Rustc's suggestion to make this code compile with a non-Copy type is:

fn main() {
    const ARRAY_REPEAT_VALUE: Thing = Thing;
    let _ = [ARRAY_REPEAT_VALUE; 2];
}

Instead it should be:

fn main() {
    let _ = [const { Thing }; 2];
}

Relatedly, now that const blocks are a stable part of the language since Rust 1.79 (https://blog.rust-lang.org/2024/06/13/Rust-1.79.0.html#inline-const-expressions), I don't think the diagnostic should be linking to the initial const block RFC.

Rust Version

rustc 1.81.0-nightly (bcf94dec5 2024-06-23)
binary: rustc
commit-hash: bcf94dec5ba6838e435902120c0384c360126a26
commit-date: 2024-06-23
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
@dtolnay dtolnay added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) labels Jun 24, 2024
@GrigorenkoPV
Copy link
Contributor

@rustbot claim

@GrigorenkoPV
Copy link
Contributor

// FIXME: we may suggest array::repeat instead

Is that a thing? I am not sure array::repeat even exists.

@fmease
Copy link
Member

fmease commented Jun 24, 2024

Is that a thing? I am not sure array::repeat even exists.

https://doc.rust-lang.org/nightly/std/array/fn.repeat.html

@GrigorenkoPV
Copy link
Contributor

Is that a thing? I am not sure array::repeat even exists.

https://doc.rust-lang.org/nightly/std/array/fn.repeat.html

Ah, thank you! Completely missed it.

Should I make the array::from_fn suggestion machine-applicable too and recommend array::from_fn::<N>(|_| ...) ?

Should I add the array::repeat suggestion on nightly?

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 24, 2024
…r=davidtwco

Suggest inline const blocks for array initialization

rust-lang#126894
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 24, 2024
…r=davidtwco

Suggest inline const blocks for array initialization

rust-lang#126894
compiler-errors added a commit to compiler-errors/rust that referenced this issue Jun 24, 2024
…r=davidtwco

Suggest inline const blocks for array initialization

rust-lang#126894
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 24, 2024
Rollup merge of rust-lang#126899 - GrigorenkoPV:suggest-const-block, r=davidtwco

Suggest inline const blocks for array initialization

rust-lang#126894
@chenyukang
Copy link
Member

Close it since #126899 is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants