Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-dereferencing bug? #24551

Closed
wooque opened this issue Apr 18, 2015 · 3 comments
Closed

Auto-dereferencing bug? #24551

wooque opened this issue Apr 18, 2015 · 3 comments
Labels
A-type-system Area: Type system

Comments

@wooque
Copy link

wooque commented Apr 18, 2015

Auto-dereferencing is working if &Box<Struct> is in place of &Struct function argument, or in:

let x: &Struct = &_boxed_struct; // &Box<Struct>

but not if Struct reference is destructured:

let &Struct{a: a1, b: b1, c: c1} = &_boxed_struct; // &Box<Struct>

Example:

struct A {
    x: i32,
    y: i32,
}

fn print_a(a: &A) {
    println!("x: {}, y: {}", a.x, a.y);
}

fn main() {
    let boxed_a = &Box::new(A{x: 1, y: 2});
    print_a(boxed_a); // this works

    let ref_a: &A = boxed_a; // this works
    print_a(ref_a);

    let &A{x: x1, y: y1} = boxed_a;
    /*
        this outputs:

        test.rs:17:10: 17:25 error: mismatched types:
        expected `Box<A>`,
            found `A`
        (expected box,
            found struct `A`) [E0308]
        test.rs:17     let &A{x: x1, y: y1} = boxed_a;
                            ^~~~~~~~~~~~~~~
        error: aborting due to previous error
    */
    println!("x: {}, y: {}", x1, y1);
}

Is it a bug or intended behaviour?

@steveklabnik steveklabnik added the A-type-system Area: Type system label Apr 18, 2015
@steveklabnik
Copy link
Member

/cc @nikomatsakis

@chuck-park
Copy link

This still reproduces in rustc 1.14.0-nightly (86affcdf6 2016-09-28).

@Mark-Simulacrum
Copy link
Member

I feel like this is intentional behavior. Pattern matching on a Box requires the use of box patterns; that is, let &box A { x: x1, y: y1 } = boxed_a; is the intended way to make this work. Alternatively, you could do let &A { x: x1, y: y1 } = &**boxed_a;, which while ugly, does work on stable.

As such I'm going to close this issue; we simply don't deref in patterns (intentionally, as far as I know) though this could change with the recent match reform RFC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

4 participants