-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
On type errors, show closure signature #46350
Conversation
Test? =) |
This is a bit as hoc and doesn't use type resolve to infer the args and return type. Is there a way to do it here, or should I keep it as simple as it is in this PR? The same can be done in ppaux (for the sake of consistent output). If we change it in ppaux I would like to keep the The difference would be |
bc45f33
to
89fccea
Compare
89fccea
to
1ac4442
Compare
Well, leaving aside the mechanism, I'd like to know what you have in mind in terms of how you want things to look. I am not sure what I think about printing closure types as |
I believe that for the following code: fn closure_to_loc() {
let mut x = |c| c + 1;
x = |c| c + 1;
} the output should be close to
instead of
(This PR does not implement the entirety of this as of yet.) |
☔ The latest upstream changes (presumably #46349) made this pull request unmergeable. Please resolve the merge conflicts. |
Instead of refering to closures with their span, format their signature.
1ac4442
to
541cfb4
Compare
I think that printing closures as I also think changing the way closures are printed in some error messages, but not all of them, is just going to confuse everyone when they see the error they are less familiar with. |
I think the main way you get closure types in type errors is when you have a type error containing a complicated type with a closure, and we need to avoid the closure "dominating" the error message by its ugliness. |
@arielb1 what do you think of referring to closures as
That being said, I think it'd be nice to have a (pseudo)signature available in the type error output. |
Having multiple spans in a |
* Accept type errors where the closure was expected and/or found * Change closure presentation to `move |args| -> _` * Closure type error help and note don't point at span anymore * Update tests
4a411a3
to
9bfdbd9
Compare
As I said, the main way you get closures in error messages is in more complicated types. I need to find a few good situations so we can figure out how to cover them with error messages. |
@@ -588,6 +588,28 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { | |||
s.push_normal(format!("{}", tnm.ty)); | |||
} | |||
|
|||
fn push_closure<'tcx>(capture: &hir::CaptureClause, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any particular reason to scrape this information from the HIR? It can also be extracted from the closure type readily enough. To get the closure signature, you would do something like this (I have a more convenient helper for this on the nll-master branch, but it's not landed yet):
ty::TyClosure(did1, substs1) => {
let closure_sig_ty = substs.closure_sig_ty(def_id, self.tcx);
let closure_sig_ty = self.shallow_resolve(&closure_sig_ty);
let sig: PolyFnSig<'_> = closure_sig_ty.fn_sig(self.tcx);
}
I think this makes sense. It feels like there are a number of situations in which we want to a way to "extract out" more complex subparts of a type. Well, maybe not a number, I'm thinking primarily of the desire to be able to say something like "the type of this variable is
Is this a good idea to pursue? I'm not sure. Maybe. |
@nikomatsakis I suggested something similar in #21025 (comment), I know that there is a ticket or comment where the same was proposed in more detail but couldn't find it. I think it is a worthwhile idea, specially for cases where we have
|
@estebank do you want to try pursuing that instead then, see how it feels? I like the idea of finding a way to "pull out" more detailed information, seems like it could be a big improvement overall; on the other hand, types appear in a lot of contexts. It feels like ideally we'd due some sort of revamp of the |
☔ The latest upstream changes (presumably #46641) made this pull request unmergeable. Please resolve the merge conflicts. |
Triage ping @estebank — @nikomatsakis has left some comments for this PR! BTW there is also merge conflict. |
I'll close this PR in lieu of implementing the suggestion above sometime in the near future :) |
Instead of referring to closures with their span, format their signature.
r? @nikomatsakis
Fix #19939.