{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/21464","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/21464/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/21464/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/21464/events","html_url":"https://github.com/rust-lang/rust/issues/21464","id":55016044,"number":21464,"title":"Compile error messages related to inferring closure types can be hard to debug","user":{"login":"nical","id":264854,"avatar_url":"https://avatars.githubusercontent.com/u/264854?v=3","gravatar_id":"","url":"https://api.github.com/users/nical","html_url":"https://github.com/nical","followers_url":"https://api.github.com/users/nical/followers","following_url":"https://api.github.com/users/nical/following{/other_user}","gists_url":"https://api.github.com/users/nical/gists{/gist_id}","starred_url":"https://api.github.com/users/nical/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nical/subscriptions","organizations_url":"https://api.github.com/users/nical/orgs","repos_url":"https://api.github.com/users/nical/repos","events_url":"https://api.github.com/users/nical/events{/privacy}","received_events_url":"https://api.github.com/users/nical/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rust-lang/rust/labels/A-diagnostics","name":"A-diagnostics","color":"d7e102"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-01-21T13:09:24Z","updated_at":"2015-05-19T18:33:26Z","closed_at":null,"body":"Sample code to illustrate the issue:  http://is.gd/qzkZ5k\r\n```\r\nfn foo(_callback: &Fn()) {\r\n    // ...\r\n}\r\n\r\nfn main() {\r\n    let captured_var = 42;\r\n    \r\n    foo(&||{\r\n        println!(\"{}\", captured_var);\r\n    });\r\n}\r\n```\r\n\r\nerror message:\r\n```\r\n<anon>:8:9: 10:6 error: unable to infer enough type information about `closure[<anon>:8:10: 10:6]`; type annotations required\r\n<anon>:8     foo(&||{\r\n<anon>:9         println!(\"{}\", captured_var);\r\n<anon>:10     });\r\n```\r\n\r\nWhat happens here is that captured_var's type isn't fully defined at the moment the closure is created and rustc doesn't know what type to put in the environment of the closure. It does make sense but the confusing part it can actually take a while to figure out that the issue is not with the visible part of the closure (it's signature), but with the invisible part (the environment). The error message points to the first line of the closure (the one with the \"&|args| {\" part which is the visible \"signature\" part) \r\nI ran into this issue with a much bigger closure, so the usage of the captured variable was much farther than the signature line pointed in the error message. Rustc does a lot of inference around the signature of closures so it encourages the developer to spend time looking there when the compiler isn't given enough info to infer the type.\r\n\r\nMy suggestion is that there should be a different error messages when the missing type information is related to the captured environment of the closure, and that this error message should have words like \"captured\" and \"environment\" in them to let the developer know that it's not about inferring the signature of the closure."},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/103625581","html_url":"https://github.com/rust-lang/rust/issues/21464#issuecomment-103625581","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/21464","id":103625581,"user":{"login":"remram44","id":426784,"avatar_url":"https://avatars.githubusercontent.com/u/426784?v=3","gravatar_id":"","url":"https://api.github.com/users/remram44","html_url":"https://github.com/remram44","followers_url":"https://api.github.com/users/remram44/followers","following_url":"https://api.github.com/users/remram44/following{/other_user}","gists_url":"https://api.github.com/users/remram44/gists{/gist_id}","starred_url":"https://api.github.com/users/remram44/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/remram44/subscriptions","organizations_url":"https://api.github.com/users/remram44/orgs","repos_url":"https://api.github.com/users/remram44/repos","events_url":"https://api.github.com/users/remram44/events{/privacy}","received_events_url":"https://api.github.com/users/remram44/received_events","type":"User","site_admin":false},"created_at":"2015-05-19T18:33:26Z","updated_at":"2015-05-19T18:33:26Z","body":"I don't see this error on your example anymore (code runs fine)."}}