Skip to content

Member function call breaks type inference #25166

Closed
@stepancheg

Description

@stepancheg

Code:

#[derive(Default)]
struct Foo;

impl Foo {
    fn set_quux(&mut self, v: u32) {
    }
}

fn main() {
    let mut v: Vec<Foo> = Vec::new();

    // this works perfectly: type of msg1 is inferred
    let mut msg1 = Default::default();
    v.push(msg1);

    // type of msg2 is explicitly specified
    let mut msg2: Foo = Default::default();
    msg2.set_quux(17);
    v.push(msg2);

    // type of msg3 could be inferred from parameter type of v.push(...)
    // although compiler reports:
    // error: the type of this value must be known in this context
    let mut msg3 = Default::default();
    msg3.set_quux(19); // <-- on this line
    v.push(msg3);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions