Skip to content

inferred repeat expression length unnecessarily needs Copy #110443

Closed
@lcnr

Description

@lcnr
#![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.

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.F-generic_arg_inferUsing `_` as a const argument: #![feature(generic_arg_infer)]`

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions