inferred repeat expression length unnecessarily needs Copy · Issue #110443 · rust-lang/rust (original) (raw)

#![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]: 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 RepeatExprMayCopyValue for [T; 0] {} impl RepeatExprMayCopyValue for [T; 1] {}

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