-
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
Confusing mismatched type error #43608
Comments
Triage: no change. |
Mentoring notes: You need to take the |
I'd like to have a shot at this. @rustbot claim |
@sulami for an example of a similar case I worked on recently, you can look at https://github.com/rust-lang/rust/pull/106519/files#diff-f1e0bfc4e359c5e1d92ebdc4aafe6dfda183fbdbc9933a3b5e9a7fb274b9d84bR1675-R1717 |
I've pushed a WIP commit and would appreciate some early feedback if this is going in the right direction. |
@sulami added some comments to the commit linked above your comment. Let me know if I was too terse, but the high level idea is you already have the HIR node for the call, which is great. From it you can look at the fields of the node to get the Res::Def if it is available, and with that you can get the original function/method type (I think that |
So I think I've got most things lined up now. I have two questions though:
|
It looks great! Ideally I think that the output would look closer to
You can accomplish that by doing something like let mut multi_span: MultiSpan = parent_expr.span.into();
multi_span.push_span_label(args[arg_idx].span, format!("this argument influences the return type of `{}`", path.segments[0].ident));
err.span_help(multi_span, ...); You can also look at the |
Emit a hint for bad call return types due to generic arguments When the return type of a function call depends on the type of an argument, e.g. ``` fn foo<T>(x: T) -> T { x } ``` and the expected type is set due to either an explicitly typed binding, or because the call to the function is in a tail position without semicolon, the current error implies that the argument in the call has the wrong type. This new hint highlights that the expected type doesn't match the returned type, which matches the argument type, and that that's why we're flagging the argument type. Fixes rust-lang#43608.
Emit a hint for bad call return types due to generic arguments When the return type of a function call depends on the type of an argument, e.g. ``` fn foo<T>(x: T) -> T { x } ``` and the expected type is set due to either an explicitly typed binding, or because the call to the function is in a tail position without semicolon, the current error implies that the argument in the call has the wrong type. This new hint highlights that the expected type doesn't match the returned type, which matches the argument type, and that that's why we're flagging the argument type. Fixes rust-lang#43608.
Emit a hint for bad call return types due to generic arguments When the return type of a function call depends on the type of an argument, e.g. ``` fn foo<T>(x: T) -> T { x } ``` and the expected type is set due to either an explicitly typed binding, or because the call to the function is in a tail position without semicolon, the current error implies that the argument in the call has the wrong type. This new hint highlights that the expected type doesn't match the returned type, which matches the argument type, and that that's why we're flagging the argument type. Fixes rust-lang#43608.
Looks like the error message for the original example is still unchanged even with #106752. Is that expected behavior? |
If you forget to put a semicolon after the last expression in the body of a function that returns no values,
rustc
will infer that the type of the expression should be()
. If the programmer didn't intend to return a value, but merely forgot to type a semicolon, the resulting error can be confusing. Consider, for example, the following toy program:This produces the following error message:
In this simple example, it may be obvious, but in the somewhat more complicated code I was working with that led me to this issue, I spent a decent amount of time trying to figure out why I was supposedly passing a pointer to
()
tomem::replace
, thus causing the type of the second argument toreplace
to fail to unify. It took me a while to figure out that this was actually the culprit.The text was updated successfully, but these errors were encountered: