Closed
Description
&T
has an impl of Clone
for all T
(i.e. (&T).clone()
should always work), however having &TypeThatConditionallyImplementsClone<T>
is not always clonable, because (it appears that) the compiler will lock itself into using the Clone
impl for the inner type, but find that T
doesn't satisfy the requirements and so error.
#[allow(dead_code)];
struct NoClone;
#[cfg(error)]
fn err() {
let x = Some(NoClone);
let y = &x;
let _: &Option<NoClone> = y.clone();
}
fn ok() {
let x = NoClone;
let y = &x;
let _: &NoClone = y.clone();
}
fn main() { }
Without --cfg error
it compiles fine, but with it:
pointer-option-method.rs:9:31: 9:41 error: failed to find an implementation of trait std::clone::Clone for NoClone
pointer-option-method.rs:9 let _: &Option<NoClone> = y.clone();
^~~~~~~~~~