Skip to content

Confusing 'implicit copy' warnings when copying into heap closures without a capture clause #2942

Closed
@bblum

Description

@bblum

One of my teammates during ICFP (#2928) who didn't already know Rust was boggled by a particularly insistent warning.

a.rs:5:17: 5:18 warning: implicitly copying a non-implicitly-copyable value
a.rs:5         bar(copy v, i)
                        ^

Rustc was really complaining that v was being copied into a heap closure. The code looked like this:

fn bar(+v: ~[int], i: uint) -> int { v[i] }

fn foo(v: ~[int]) -> fn@(uint) -> int {
    ret |i| {
        bar(copy v, i)
    }   
}

fn main() { #error["%d", foo(~[5])(0)]; }

The solution was to write copy v in the capture clause.

Perhaps the warning message could be more informative? Could it hint that the copy comes from building the heap closure, and suggest writing a capture clause?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions