Skip to content

Index syntax autoderef is different from direct method call #15757

Closed
@tomassedovic

Description

@tomassedovic

The col[i] syntax seems to do some autodereferencing of col, but it doesn't work with && and Rc. This is confusing because method calls dereference as much as necessary:

use std::rc::Rc;

fn main() {
    let v = vec![1u, 2, 3];
    println!("first item: {}", v[0]);  // OK

    let v_ref = &v;
    println!("first item: {}", v_ref[0]);  // OK

    let v_ref_ref = &&v;
    println!("first item: {}", v_ref_ref.index(&0));  // OK
    // Fails, should be equivalent to the above:
    println!("first item: {}", v_ref_ref[0]);
    // error: cannot index a value of type `&&collections::vec::Vec<uint>`
    // println!("first item: {}", v_ref_ref[0]);
    //                            ^~~~~~~~~~~~

    let v_rc = Rc::new(vec![1u, 2, 3]);
    println!("first item: {}", v_rc.index(&0));  // OK
    // Fails, should be equivalent to the above:
    println!("first item: {}", v_rc[0]);
    // error: cannot index a value of type `alloc::rc::Rc<collections::vec::Vec<uint>>`
    // println!("first item: {}", v_rc[0]);
    //                            ^~~~~~~
}

I'm not sure if this is related to issue #15734. The circumstances seem different, but the underlying cause might be the same.

$ rustc --version
rustc 0.12.0-pre-nightly (459ffc2adc74f5e8b64a76f5670edb419b9f65da 2014-07-17 01:16:19 +0000)

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions