-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Clean up callable type mismatch errors #41488
Conversation
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
e) | ||
let expected_trait_ty = expected_trait_ref.self_ty(); | ||
if expected_trait_ty.is_closure() { | ||
if let &TypeError::TupleSize(ref expected_found) = e { |
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.
Shouldn't you match on expected_trait_ref
& actual_trait_ref
, to avoid Fn(X<()>)
vs. Fn(X<(Foo, Bar)>)
etc.
Also, this error message is probably good for all Fn()
trait users, even if they are not closures.
This code:
Currently gives me lot of error output:
A better error output could be:
|
Nit on wording. In definitions/declarations it is “arguments”, not “parameters”. “parameters” is for expressions that get passed into the function call. |
d05bbec
to
ad291b7
Compare
@leonardo-m - did the error message get worse or stay the same? If it's the same, we could take this PR and then tackle your example as a separate issue. |
src/test/compile-fail/E0281.rs
Outdated
foo(|y| { }); //~ ERROR E0281 | ||
//~^ ERROR E0281 | ||
foo(|y: String| { }); | ||
//~^ ERROR E0281 |
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.
Can we move this to a ui
test?
@@ -19,9 +19,13 @@ fn apply<T, F>(t: T, f: F) where F: FnOnce(T) { | |||
fn main() { | |||
apply(&3, takes_imm); | |||
apply(&3, takes_mut); | |||
//~^ ERROR (types differ in mutability) |
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.
same
@@ -18,4 +18,11 @@ fn main() { | |||
//~^ ERROR no method named `count` | |||
//~| ERROR E0281 | |||
//~| ERROR E0281 | |||
//~| NOTE expected &str, found str |
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.
same
let z = call_it(3, f); | ||
//~^ ERROR type mismatch | ||
//~| ERROR type mismatch | ||
//~| NOTE expected isize, found usize |
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.
same
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.
Done for all.
| ^^^ expected bound lifetime parameter, found concrete lifetime | ||
| | ||
= note: concrete lifetime that was found is lifetime '_#0r | ||
= note: required because of the requirements on the impl of `Foo` for `[closure@$DIR/closure-mismatch.rs:18:9: 18:15]` |
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.
These two notes don't seem helplful. Are they what is there currently, rather than being added by this PR?
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.
That's already part of the current output. I would prefer to remove them in a separate (smaller) PR.
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.
Sure, that's fine
Yeah, that looks pretty good. 👍 from me. |
for error in errors { | ||
self.report_fulfillment_error(error); | ||
// Avoid multiple errors saying the same thing for closures |
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 this targeted at something specific? e.g. it would be nice if we could filter projection errors if there is a corresponding selection error.
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.
You can see the effect of this in the stderr output, right now for closure type mismatch errors we output two (may be three in some cases?, haven't seen that yet) after the compiler tries to coerce to Fn
, FnMut
and FnOnce
. This change was hacky and want to fix it properly somewhere closer to the source but this is as close as I got to the culprit last night. We can either remove this hack now and merge the PR with the duplicated errors, or merge this as is and I send a follow up PR fully addressing this.
@jonathandturner: >did the error message get worse or stay the same? I don't fully understand your question. Given this code:
This is how its error messages have evolved:
So the error messages are essentially the same. And I think they could use an improvement (the first improvement is to generate only one error message). |
@leonardo-m the output for that file with the changes on this PR is: error[E0308]: mismatched types
--> $DIR/closure-arg-count.rs:3:24
|
3 | [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
| ^^^^^^^^^^^^^^^ expected &{integer}, found tuple
|
= note: expected type `&{integer}`
found type `(_, _)`
error[E0593]: closure takes 1 argument but 2 arguments are required
--> $DIR/closure-arg-count.rs:3:15
|
3 | [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
| ^^^^^^^ -------------------------- takes 1 argument
| |
| expected closure that takes 2 arguments
error: aborting due to 2 previous errors |
It's it too much to tell me five times what's the problem? Telling me once or twice should suffice. |
@leonardo-m I think there might have been a misunderstanding. I believe you were pointing out the output for this error over time, since 1.0 to current nightly, where there were 3 different, but equally unspecific, outputs, as well as suggesting a possible new output. I was pointing out what the new output with this code would be like. Your proposal to point out at only the tuple arg of the closure is not trivial at the moment because I only have the full closure's span to point at, but could be done with some more work. Apologies if you felt I was talking down to you in any way.
This PR does that (as pointed out in a prior comment), other than removing the mismatched types error, which I'll try to remove in a clean way in a follow up PR. |
@leonardo-m - yeah sorry, I wasn't clear either. I think you're pointing out a way it can be improved even further. Can you file an issue with your example so we don't forget and can improve your example also? I was just trying to make sure we weren't making your example worse with this PR, but it looks like we just have more work to do after this PR. |
Thank you for the work.
I think it's better to wait for this PR to be merged and inside one Nightly, then I'll take a look at the error message it gives and we can open a new enhancement request. |
Ping @arielb1, @jonathandturner already 👍'd, I think the only thing left is to make a decision on wether we can leave the last commit's deduplication logic as is until I submit a cleaner follow up PR for that part or we land this PR without that code. |
The last commit's deduplication is a hack. I am working on a more principled version but this week is an holiday. |
@arielb1 removed the hacky commit. |
@bors r+ |
📌 Commit b10e293 has been approved by |
Clean up callable type mismatch errors ```rust error[E0593]: closure takes 1 argument but 2 arguments are required here --> ../../src/test/ui/mismatched_types/closure-arg-count.rs:13:15 | 13 | [1, 2, 3].sort_by(|(tuple, tuple2)| panic!()); | ^^^^^^^ -------------------------- takes 1 argument | | | expected closure that takes 2 arguments ``` instead of ```rust error[E0281]: type mismatch: the type `[closure@../../src/test/ui/mismatched_types/closure-arg-count.rs:13:23: 13:49]` implements the trait `for<'r> std::ops::FnMut<(&'r {integer},)>`, but the trait `for<'r, 'r> std::ops::FnMut<(&'r {integer}, &'r {integer})>` is required (expected a tuple with 2 elements, found one with 1 elements) --> ../../src/test/ui/mismatched_types/closure-arg-count.rs:13:15 | 13 | [1, 2, 3].sort_by(|(tuple, tuple2)| panic!()); | ^^^^^^^ ``` Fix #21857, re #24680.
☀️ Test successful - status-appveyor, status-travis |
instead of
Fix #21857, re #24680.