Skip to content

Trait method resolution is overly eager for nested pointers & types #11820

Closed
@huonw

Description

@huonw

&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();
                                                         ^~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions