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 1 commit
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
11 changes: 6 additions & 5 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
kind,
found_kind);

err.span_label(
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)),
Expand All @@ -656,6 +660,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
InferTables::Missing => None,
};

// Additional context information explaining why the closure only implements
// a particular trait.
if let Some(tables) = infer_tables {
match tables.closure_kinds.get(&node_id) {
Some(&(ty::ClosureKind::FnOnce, Some((span, name)))) => {
Expand All @@ -670,11 +676,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
},
_ => {}
Copy link
Contributor

Choose a reason for hiding this comment

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

One minor point -- in this arm, we emit neither the new note nor the old one. Seems like the old one might still be helpful?

Actually, maybe we should always emit the old note -- but instead of a note, just make it a span_label? Seems like it's useful information either way. In other words, the old note tells you "why it has to be Fn", and the new note tells you why it cannot be...

If you agree, maybe move the body of the else below out and above this if, and convert span_note to span_label (so that it prints within one code snippet)

Copy link
Contributor Author

@tommyip tommyip Jun 6, 2017

Choose a reason for hiding this comment

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

Good point, I didn't think about that!

}
} else {
err.span_note(
obligation.cause.span,
&format!("the requirement to implement `{}` \
derives from here", kind));
}

err.emit();
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/closure_context/issue-26046-fn-mut.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ error[E0525]: expected a closure that implements the `Fn` trait, but this closur
15 | | num += 1;
16 | | };
| |_____^
17 |
Copy link
Contributor

Choose a reason for hiding this comment

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

cc @estebank -- @tommyip mentioned that there is a trailing space after this 17 | -- this doesn't bother me per se, but is that just how the diagnostics output works in general?

Copy link
Contributor

Choose a reason for hiding this comment

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

@nikomatsakis that space is to pad to compensate for the multiline markers. We can add a check for empty lines and avoid it.

18 | Box::new(closure)
| ----------------- the requirement to implement `Fn` derives from here
|
note: closure is `FnMut` because it mutates the variable `num` here
Copy link
Contributor

Choose a reason for hiding this comment

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

slick

--> $DIR/issue-26046-fn-mut.rs:15:9
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/closure_context/issue-26046-fn-once.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ error[E0525]: expected a closure that implements the `Fn` trait, but this closur
15 | | vec
16 | | };
| |_____^
17 |
18 | Box::new(closure)
| ----------------- the requirement to implement `Fn` derives from here
|
note: closure is `FnOnce` because it moves the variable `vec` out of its environment
--> $DIR/issue-26046-fn-once.rs:15:9
Expand Down