-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add ignore value suggestion in closure body #135562
base: master
Are you sure you want to change the base?
Changes from all commits
3d50334
8cddffb
d6d9c2e
774ab46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1883,25 +1883,24 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { | |
fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env, expected, found, ty_err); | ||
|
||
let due_to_block = matches!(fcx.tcx.hir_node(block_or_return_id), hir::Node::Block(..)); | ||
|
||
let parent_id = fcx.tcx.parent_hir_id(block_or_return_id); | ||
let parent = fcx.tcx.hir_node(parent_id); | ||
let parent = fcx.tcx.parent_hir_node(block_or_return_id); | ||
if let Some(expr) = expression | ||
&& let hir::Node::Expr(hir::Expr { | ||
kind: hir::ExprKind::Closure(&hir::Closure { body, .. }), | ||
.. | ||
}) = parent | ||
Comment on lines
1888
to
1891
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...and change the pattern here to surround it with It is a shame how much macros in general complicate this kind of logic :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's complicated. |
||
&& !matches!(fcx.tcx.hir_body(body).value.kind, hir::ExprKind::Block(..)) | ||
{ | ||
fcx.suggest_missing_semicolon(&mut err, expr, expected, true); | ||
let needs_block = | ||
!matches!(fcx.tcx.hir_body(body).value.kind, hir::ExprKind::Block(..)); | ||
fcx.suggest_missing_semicolon(&mut err, expr, expected, needs_block, true); | ||
} | ||
// Verify that this is a tail expression of a function, otherwise the | ||
// label pointing out the cause for the type coercion will be wrong | ||
// as prior return coercions would not be relevant (#57664). | ||
if let Some(expr) = expression | ||
&& due_to_block | ||
{ | ||
fcx.suggest_missing_semicolon(&mut err, expr, expected, false); | ||
fcx.suggest_missing_semicolon(&mut err, expr, expected, false, false); | ||
let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail( | ||
&mut err, | ||
expr, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fn main() { | ||
b"abc".iter().for_each(|x| x); //~ ERROR: mismatched types | ||
|
||
b"abc".iter().for_each(|x| dbg!(x)); //~ ERROR: mismatched types | ||
|
||
b"abc".iter().for_each(|x| { | ||
println!("{}", x); | ||
x //~ ERROR: mismatched types | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/closure-ty-mismatch-issue-128561.rs:2:32 | ||
| | ||
LL | b"abc".iter().for_each(|x| x); | ||
| ^ expected `()`, found `&u8` | ||
| | ||
help: consider ignoring the value | ||
| | ||
LL | b"abc".iter().for_each(|x| _ = x); | ||
| +++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/closure-ty-mismatch-issue-128561.rs:4:32 | ||
| | ||
LL | b"abc".iter().for_each(|x| dbg!(x)); | ||
| ^^^^^^^ expected `()`, found `&u8` | ||
| | ||
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
Comment on lines
+12
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I notice that the suggestion didn't trigger for the macro case, which is what the original report wanted. I think that in order to sidestep the issues of the macro not being visible in the HIR, to get the span by looking at the closure's tail expression (instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't notice in the macro case the parent type check here is not closure: I changed the code to avoid suggesting for macro case explicitly. Seems we need to get the parent hir recursively up to 4 levels in macro case to get the closure body, I tried with some debug code like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two options that might or might not work: from the |
||
|
||
error[E0308]: mismatched types | ||
--> $DIR/closure-ty-mismatch-issue-128561.rs:8:9 | ||
| | ||
LL | x | ||
| ^ expected `()`, found `&u8` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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.
If you change this for
get_closure_node(block_or_return_id)
...