Skip to content

Type errors with deriving on structures/enums with internal references #15689

Closed
@netvl

Description

@netvl

This code:

#[deriving(PartialEq)]
enum Test<'a> {
    Slice(&'a int)
}

fn main() {}

fails to compile with the following error:

main2.rs:3:11: 3:18 error: mismatched types: expected `&int` but found `&&'a int` (expected int but found &-ptr)
main2.rs:3     Slice(&'a int)
                     ^~~~~~~
note: in expansion of #[deriving]
main2.rs:1:1: 1:23 note: expansion site
main2.rs:3:11: 3:18 error: mismatched types: expected `&int` but found `&&'a int` (expected int but found &-ptr)
main2.rs:3     Slice(&'a int)
                     ^~~~~~~
note: in expansion of #[deriving]
main2.rs:1:1: 1:23 note: expansion site
error: aborting due to 2 previous errors

Basically I understand the reason for this error, According to rustc --pretty expanded output:

    #[inline]
    fn eq(&self, __arg_0: &Test<'a>) -> ::bool {
        match (&*self, &*__arg_0) {
            (&Slice(ref __self_0), &Slice(ref __arg_1_0)) =>
            true && (*__self_0).eq(&(*__arg_1_0))
        }
    }

here (*__self_0).eq(...) resolves to correct method call due to auto[de]ref on method target, but its argument is still of &&'a int type, which leads to the type error.

I can see why this can be the intended behavior, but I'd argue that this is counterintuitive and very inconvenient: for example, I have a large enum with a lot of references inside its variants, and I'd like to make it comparable with itself, but autoderiving won't work due to this behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions