Skip to content

Inference weirdness with a closure #20558

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

Closed
nrc opened this issue Jan 5, 2015 · 1 comment · Fixed by #21353
Closed

Inference weirdness with a closure #20558

nrc opened this issue Jan 5, 2015 · 1 comment · Fixed by #21353
Labels
A-type-system Area: Type system

Comments

@nrc
Copy link
Member

nrc commented Jan 5, 2015

Sorry this is a vague bug report and a crap example, it comes from trans_struct in expr.rs, the following fails:

let mut need_base: Vec<_> = repeat(true).take(field_tys.len()).collect();

        let numbered_fields = fields.iter().map(|field| {
            let opt_pos =
                field_tys.iter().position(|field_ty|
                                          field_ty.name == field.ident.node.name);
            let result = match opt_pos {
                Some(i) => {
                    need_base[i] = false;
                    (i, &*field.expr)
                }
                None => {
                    tcx.sess.span_bug(field.span,
                                      "Couldn't find field in struct type")
                }
            };
            result
        }).collect::<Vec<_>>();

Changing Vec<_> to Vec<bool> makes it compile.

The error message is unable to infer enough type information about "closure[/home/ncameron/rust3/src/librustc_trans/trans/expr.rs:1390:74: 1405:10]; type annotations required".

The reason I think this is a bug and not just annoying is that it is so obvious that the parameter is bool.

cc @nikomatsakis

@kmcallister kmcallister added the A-type-system Area: Type system label Jan 5, 2015
@lifthrasiir
Copy link
Contributor

Maybe related:

fn foo<F: FnMut(&[u8])>(mut f: F) {
    f(b"hello ");
    f(b"world!");
}

fn main() {
    let mut v = Vec::new();
    // error: unable to infer enough type information about `closure[...]`; type annotations required
    foo(|buf| v.extend(buf.iter().cloned()));
    println!("{:?}", v);
}

At least we need a good error message here. It says "type annotations required", but doesn't say which ones (and confusingly enough, variables above that closure have to be annotated).

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

Successfully merging a pull request may close this issue.

3 participants