Skip to content

Better closure error message #42443

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

Merged
merged 6 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use hir::{self, intravisit, Local, Pat, Body};
use hir::intravisit::{Visitor, NestedVisitorMap};
use hir::map::NodeExpr;
use hir::def_id::DefId;
use infer::{self, InferCtxt};
use infer::{self, InferCtxt, InferTables, InferTablesRef};
use infer::type_variable::TypeVariableOrigin;
use rustc::lint::builtin::EXTRA_REQUIREMENT_IN_IMPL;
use std::fmt;
Expand Down Expand Up @@ -640,16 +640,38 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
ty::Predicate::ClosureKind(closure_def_id, kind) => {
let found_kind = self.closure_kind(closure_def_id).unwrap();
let closure_span = self.tcx.hir.span_if_local(closure_def_id).unwrap();
let node_id = self.tcx.hir.as_local_node_id(closure_def_id).unwrap();
let mut err = struct_span_err!(
self.tcx.sess, closure_span, E0525,
"expected a closure that implements the `{}` trait, \
but this closure only implements `{}`",
kind,
found_kind);
err.span_note(
obligation.cause.span,
&format!("the requirement to implement \
`{}` derives from here", kind));

let infer_tables = match self.tables {
InferTables::Interned(tables) =>
Some(InferTablesRef::Interned(tables)),
InferTables::InProgress(tables) =>
Some(InferTablesRef::InProgress(tables.borrow())),
InferTables::Missing => None,
};

if let Some(tables) = infer_tables {
if let Some(&(ty::ClosureKind::FnOnce, Some((span, name)))) =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is specific to FnOnce, but similar scenarios can arise in other situations. For example, we could highlight the x += 1 here and say something like "mutating this variable":

fn foo<F: Fn()>(f: F) { }
fn main() {
    let mut x = 0;
    foo(|| x += 1); // Error
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we match more cases here or make the error more generic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I think it'd be better to match more cases, and keep the message fairly tailored -- there are really only two cases. The closure might be FnOnce (in which case we can talk about something being moved) or it might be FnMut (in which case we can talk about something being written to).

tables.closure_kinds.get(&node_id)
{
err.span_note(
span,
&format!("closure is `FnOnce` because it moves the \
variable `{}` out of its environment", name));
}
} else {
err.span_note(
obligation.cause.span,
&format!("the requirement to implement `{}` \
derives from here", kind));
}

err.emit();
return;
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/issue-26046.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn get_closure() -> Box<Fn() -> Vec<u8>> {
let vec = vec![1u8, 2u8];

let closure = move || {
vec
};

Box::new(closure)
}

fn main() {}
17 changes: 17 additions & 0 deletions src/test/ui/issue-26046.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
--> $DIR/issue-26046.rs:14:19
|
14 | let closure = move || {
| ___________________^
15 | | vec
16 | | };
| |_____^
|
note: closure is `FnOnce` because it moves the variable `vec` out of its environment
--> $DIR/issue-26046.rs:15:9
|
15 | vec
| ^^^

error: aborting due to previous error(s)