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

inferred repeat expression length unnecessarily needs Copy #110443

Closed
lcnr opened this issue Apr 17, 2023 · 0 comments · Fixed by #137045
Closed

inferred repeat expression length unnecessarily needs Copy #110443

lcnr opened this issue Apr 17, 2023 · 0 comments · Fixed by #137045
Assignees
Labels
C-bug Category: This is a bug. F-generic_arg_infer Using `_` as a const argument: #![feature(generic_arg_infer)]`

Comments

@lcnr
Copy link
Contributor

lcnr commented Apr 17, 2023

#![feature(generic_arg_infer)]
struct NotCopy;

fn with_len<T, const N: usize>(_: [T; N]) {}

fn main() {
    let x = [NotCopy; _];
    with_len::<NotCopy, 0>(x)
}

results in the following error:

error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): the trait bound `NotCopy: Copy` is not satisfied
 --> src/main.rs:7:14
  |
7 |     let x = [NotCopy; _];
  |              ^^^^^^^ the trait `Copy` is not implemented for `NotCopy`
  |
  = note: the `Copy` trait is required because this value will be copied for each element of the array
help: consider annotating `NotCopy` with `#[derive(Copy)]`
  |
2 + #[derive(Copy)]
3 | struct NotCopy;
  |

we should be able to solve that by using marker traits (potentially waiting until they're stable)

#[marker]
trait RepeatExprMayCopyValue {}
impl<T: Copy, const N: usize> RepeatExprMayCopyValue for [T; N] {}
impl<T> RepeatExprMayCopyValue for [T; 0] {}
impl<T> RepeatExprMayCopyValue for [T; 1] {}

and then change hir typeck to require RepeatExprMayCopyValue for the array instead of optionally requiring Copy for the value.

@lcnr lcnr added C-bug Category: This is a bug. F-generic_arg_infer Using `_` as a const argument: #![feature(generic_arg_infer)]` labels Apr 17, 2023
@BoxyUwU BoxyUwU self-assigned this Feb 14, 2025
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 14, 2025
…<try>

Defer repeat expr `Copy` checks to end of type checking

Fixes rust-lang#110443

r? `@ghost`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 28, 2025
…r=compiler-errors

Defer repeat expr `Copy` checks to end of type checking

Fixes rust-lang#110443

Defers repeat expr checks that the element type is `Copy` when the length is > 1 (or generic) to end of typeck so that under `generic_arg_infer` repeat exprs are able to have an inferred count, e.g. `let a: [_; 1] = [String::new(); _];`.

Currently the deferring is gated under `generic_arg_infer` though I intend to separately types FCP deferring the checks even outside of `generic_arg_infer` if we wind up not going with an alternative.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 28, 2025
…r=compiler-errors

Defer repeat expr `Copy` checks to end of type checking

Fixes rust-lang#110443

Defers repeat expr checks that the element type is `Copy` when the length is > 1 (or generic) to end of typeck so that under `generic_arg_infer` repeat exprs are able to have an inferred count, e.g. `let a: [_; 1] = [String::new(); _];`.

Currently the deferring is gated under `generic_arg_infer` though I intend to separately types FCP deferring the checks even outside of `generic_arg_infer` if we wind up not going with an alternative.
@bors bors closed this as completed in 91eb721 Mar 1, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 1, 2025
Rollup merge of rust-lang#137045 - BoxyUwU:defer_repeat_expr_checks, r=compiler-errors

Defer repeat expr `Copy` checks to end of type checking

Fixes rust-lang#110443

Defers repeat expr checks that the element type is `Copy` when the length is > 1 (or generic) to end of typeck so that under `generic_arg_infer` repeat exprs are able to have an inferred count, e.g. `let a: [_; 1] = [String::new(); _];`.

Currently the deferring is gated under `generic_arg_infer` though I intend to separately types FCP deferring the checks even outside of `generic_arg_infer` if we wind up not going with an alternative.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-generic_arg_infer Using `_` as a const argument: #![feature(generic_arg_infer)]`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants