-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Don't suggest adding return type for closures with default return type #129260
Conversation
bf22d44
to
893f2be
Compare
@@ -832,7 +832,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Unit { span }); | |||
return true; | |||
} | |||
&hir::FnRetTy::DefaultReturn(span) if expected.is_unit() => { | |||
// Don't suggest adding return types for closures with default return. |
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.
There's some redundancy between this check and the can_suggest
boolean above. Can you find out if we can just reuse can_suggest
in this position, rather than making the check from within the function?
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.
I did some investigations and I found that
hir::ClosureKind::Closure => Some((def_id, fn_decl, true)), |
If I set can_suggest
to false
for closures here, then for tests/ui/closures/add_semicolon_non_block_closure.rs
rust/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Lines 829 to 835 in bf662eb
match &fn_decl.output { | |
&hir::FnRetTy::DefaultReturn(span) if expected.is_unit() && !can_suggest => { | |
// `fn main()` must return `()`, do not suggest changing return type | |
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Unit { span }); | |
return true; | |
} | |
&hir::FnRetTy::DefaultReturn(span) if expected.is_unit() => { |
code here will go to the upper branch. I think we still need some way to distinguish main
and closures or we will get something like this:
--> testfile.rs:8:12
|
8 | foo(|| bar())
| -^^^^^ expected `()`, found `i32`
| |
| expected `()` because of default return type
|
I don't expect the last line to be printed.
I've thought of some possible solutions:
- The above result is acceptable?
- Keep my original solution, which indeed adds to complexity
- Add
is_main
to the return values ofget_fn_decl
, which seems (I think) a bit overkill and may impact performance if it's getting called a lot - Refactor some of the code to see if we can find a better solution
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.
Would appreciate testing out the suggestion I had.
893f2be
to
9b7739c
Compare
9b7739c
to
736ab66
Compare
I removed This also fixed not suggesting adding return type in non-trait @rustbot ready |
@bors r+ |
…mpiler-errors Don't suggest adding return type for closures with default return type Follow up of rust-lang#129223 r? `@compiler-errors`
…kingjubilee Rollup of 14 pull requests Successful merges: - rust-lang#129260 (Don't suggest adding return type for closures with default return type) - rust-lang#129520 (Suggest the correct pattern syntax on usage of unit variant pattern for a struct variant) - rust-lang#129696 (update stdarch) - rust-lang#129759 (Stabilize `const_refs_to_static`) - rust-lang#129835 (enable const-float-classify test, and test_next_up/down on 32bit x86) - rust-lang#129866 (Clarify documentation labelling and definitions for std::collections) - rust-lang#130052 (Don't leave debug locations for constants sitting on the builder indefinitely) - rust-lang#130077 (Fix linking error when compiling for 32-bit watchOS) - rust-lang#130123 (Report the `note` when specified in `diagnostic::on_unimplemented`) - rust-lang#130156 (Add test for S_OBJNAME & update test for LF_BUILDINFO cl and cmd) - rust-lang#130206 (Map `WSAEDQUOT` to `ErrorKind::FilesystemQuotaExceeded`) - rust-lang#130207 (Map `ERROR_CANT_RESOLVE_FILENAME` to `ErrorKind::FilesystemLoop`) - rust-lang#130219 (Fix false positive with `missing_docs` and `#[test]`) - rust-lang#130221 (Make SearchPath::new public) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 9 pull requests Successful merges: - rust-lang#129260 (Don't suggest adding return type for closures with default return type) - rust-lang#129520 (Suggest the correct pattern syntax on usage of unit variant pattern for a struct variant) - rust-lang#129866 (Clarify documentation labelling and definitions for std::collections) - rust-lang#130123 (Report the `note` when specified in `diagnostic::on_unimplemented`) - rust-lang#130161 (refactor merge base logic and fix `x fmt`) - rust-lang#130206 (Map `WSAEDQUOT` to `ErrorKind::FilesystemQuotaExceeded`) - rust-lang#130207 (Map `ERROR_CANT_RESOLVE_FILENAME` to `ErrorKind::FilesystemLoop`) - rust-lang#130219 (Fix false positive with `missing_docs` and `#[test]`) - rust-lang#130221 (Make SearchPath::new public) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#129260 - wafarm:dont-suggest-closures, r=compiler-errors Don't suggest adding return type for closures with default return type Follow up of rust-lang#129223 r? ``@compiler-errors``
Follow up of #129223
r? @compiler-errors