1
Fork 0
rust/compiler
bors 2a71115261 Auto merge of #105888 - skyzh:skyzh/suggest-lifetime-closure, r=compiler-errors
suggest lifetime for closure parameter type when mismatch

This is a draft PR, will add test cases later and be ready for review.

This PR fixes https://github.com/rust-lang/rust/issues/105675 by adding a diagnostics suggestion. Also a partial fix to https://github.com/rust-lang/rust/issues/105528.

The following code will have a compile error now:

```
fn const_if_unit(input: bool) -> impl for<'a> FnOnce(&'a ()) -> usize {
    let x = |_| 1;
    x
}
```

Before this PR:

```
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ one type is more general than the other
  |
  = note: expected trait `for<'a> FnOnce<(&'a (),)>`
             found trait `FnOnce<(&(),)>`
note: this closure does not fulfill the lifetime requirements
 --> src/lib.rs:2:13
  |
2 |     let x = |_| 1;
  |             ^^^

error: implementation of `FnOnce` is not general enough
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 ()) -> usize` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rust-test` due to 2 previous errors
```

After this PR:

```
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ one type is more general than the other
  |
  = note: expected trait `for<'a> FnOnce<(&'a (),)>`
             found trait `FnOnce<(&(),)>`
note: this closure does not fulfill the lifetime requirements
 --> src/lib.rs:2:13
  |
2 |     let x = |_| 1;
  |             ^^^
help: consider changing the type of the closure parameters
  |
2 |     let x = |_: &_| 1;
  |             ~~~~~~~

error: implementation of `FnOnce` is not general enough
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 ()) -> usize` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rust-test` due to 2 previous errors
```

After applying the suggestion, it compiles. The suggestion might not always be correct as the generation procedure of that suggestion is quite simple...
2023-04-16 03:06:46 +00:00
..
rustc fix link 2023-03-11 10:53:47 -06:00
rustc_abi Fix some clippy::complexity 2023-04-09 23:22:14 +02:00
rustc_apfloat
rustc_arena Allow clippy::from_mut_ref 2023-04-09 22:29:56 +02:00
rustc_ast Rollup merge of #110203 - compiler-errors:rtn-dots, r=eholk 2023-04-12 20:56:22 +02:00
rustc_ast_lowering Rollup merge of #110203 - compiler-errors:rtn-dots, r=eholk 2023-04-12 20:56:22 +02:00
rustc_ast_passes Rollup merge of #110237 - oli-obk:impl_trait_in_assoc_tys, r=jackh726 2023-04-12 20:56:24 +02:00
rustc_ast_pretty Remove .. from return type notation 2023-04-10 22:19:46 +00:00
rustc_attr Bless tidy 2023-03-27 18:58:07 +00:00
rustc_baked_icu_data update ICU4X to 1.1.0 2023-02-04 22:28:59 +01:00
rustc_borrowck fix clippy::{clone_on_copy, useless_conversion} 2023-04-15 19:02:26 +02:00
rustc_builtin_macros Auto merge of #110252 - matthiaskrgr:rollup-ovaixra, r=matthiaskrgr 2023-04-12 22:19:29 +00:00
rustc_codegen_cranelift Remove from cranelift too. 2023-04-14 16:26:11 +00:00
rustc_codegen_gcc Auto merge of #109989 - ids1024:m68k-asm, r=Amanieu 2023-04-13 11:41:57 +00:00
rustc_codegen_llvm Add inline assembly support for m68k 2023-04-12 17:58:15 -07:00
rustc_codegen_ssa Remove attempt to optimize codegen for discriminants. 2023-04-14 16:26:11 +00:00
rustc_const_eval Implement Copy for LocationDetail 2023-04-13 18:04:30 +00:00
rustc_data_structures Use SSO_ARRAY_SIZE instead of 8 in SsoHashMap impl 2023-04-11 11:02:01 +00:00
rustc_driver Remove unneeded extern crate 2023-02-02 07:47:39 +01:00
rustc_driver_impl Rollup merge of #110072 - joshtriplett:stabilize-is-terminal, r=Mark-Simulacrum 2023-04-13 11:21:00 +02:00
rustc_error_codes Make "unneccesary visibility qualifier" error much more clear 2023-04-03 21:52:27 -05:00
rustc_error_messages Stabilize a portion of 'once_cell' 2023-03-29 18:04:44 -04:00
rustc_errors Rollup merge of #110072 - joshtriplett:stabilize-is-terminal, r=Mark-Simulacrum 2023-04-13 11:21:00 +02:00
rustc_expand Rollup merge of #110222 - lovelymono:rustc-expand-mbe-diagnostic, r=davidtwco 2023-04-12 20:56:24 +02:00
rustc_feature Rollup merge of #110237 - oli-obk:impl_trait_in_assoc_tys, r=jackh726 2023-04-12 20:56:24 +02:00
rustc_fs_util Add try_canonicalize to rustc_fs_util and use it over fs::canonicalize 2023-03-16 21:50:23 +01:00
rustc_graphviz
rustc_hir Fix some clippy::complexity 2023-04-09 23:22:14 +02:00
rustc_hir_analysis fix clippy::{clone_on_copy, useless_conversion} 2023-04-15 19:02:26 +02:00
rustc_hir_pretty Remove identity casts 2023-04-09 23:22:14 +02:00
rustc_hir_typeck fix clippy::{filter_map_identiy, map_identity, manual_flatten} 2023-04-15 18:56:25 +02:00
rustc_incremental Check for escape sequences in Fluent resources 2023-03-29 18:34:29 +01:00
rustc_index Fix some clippy::complexity 2023-04-09 23:22:14 +02:00
rustc_infer fix import Applicability 2023-04-14 11:42:37 -04:00
rustc_interface Migrate sess.opts.tests uses to sess.is_test_crate() 2023-04-09 21:37:31 +02:00
rustc_lexer Revert "Don't recover lifetimes/labels containing emojis as character literals" 2023-04-10 06:52:41 +00:00
rustc_lint Rollup merge of #110153 - DaniPopes:compiler-typos, r=Nilstrieb 2023-04-12 20:56:21 +02:00
rustc_lint_defs Fix typos in compiler 2023-04-10 22:02:52 +02:00
rustc_llvm Rollup merge of #96971 - zhaixiaojuan:master, r=wesleywiser 2023-04-11 20:28:45 -07:00
rustc_log Stabilize IsTerminal 2023-04-10 17:24:23 +09:00
rustc_macros Auto merge of #110092 - clubby789:builtin-macros-translatable, r=compiler-errors 2023-04-11 14:40:54 +00:00
rustc_metadata resolve: Pre-compute non-reexport module children 2023-04-12 15:22:03 +03:00
rustc_middle Rollup merge of #110299 - kylematsuda:earlybinder-impl-subject, r=compiler-errors 2023-04-14 07:58:42 +02:00
rustc_mir_build Auto merge of #109466 - davidlattimore:inline-arg-via-var-debug-info, r=wesleywiser 2023-04-13 01:51:27 +00:00
rustc_mir_dataflow Fix typos in compiler 2023-04-10 22:02:52 +02:00
rustc_mir_transform Remove outdated comment. 2023-04-15 07:46:46 +00:00
rustc_monomorphize fix clippy::{filter_map_identiy, map_identity, manual_flatten} 2023-04-15 18:56:25 +02:00
rustc_parse remove redundant clones 2023-04-15 18:04:51 +02:00
rustc_parse_format Fix typos in compiler 2023-04-10 22:02:52 +02:00
rustc_passes impl reviewer feedback 2023-04-13 23:39:14 +12:00
rustc_plugin_impl expand: Pass ast::Crate by reference to AST transforming passes 2023-03-23 14:20:55 +04:00
rustc_privacy resolve: Pre-compute non-reexport module children 2023-04-12 15:22:03 +03:00
rustc_query_impl fix clippy::{clone_on_copy, useless_conversion} 2023-04-15 19:02:26 +02:00
rustc_query_system Auto merge of #109935 - michaelwoerister:fix-feed-in-eval-always, r=cjgillot 2023-04-12 11:16:35 +00:00
rustc_resolve Auto merge of #110160 - petrochenkov:notagain2, r=cjgillot 2023-04-14 01:14:36 +00:00
rustc_serialize Remove f32 & f64 from MemDecoder/MemEncoder 2023-04-06 00:54:07 -07:00
rustc_session Implement Copy for LocationDetail 2023-04-13 18:04:30 +00:00
rustc_smir Add a stable MIR way to get the main function 2023-04-14 07:28:56 +00:00
rustc_span Auto merge of #109989 - ids1024:m68k-asm, r=Amanieu 2023-04-13 11:41:57 +00:00
rustc_symbol_mangling Remove index from BrAnon 2023-04-06 23:01:40 -04:00
rustc_target Auto merge of #109989 - ids1024:m68k-asm, r=Amanieu 2023-04-13 11:41:57 +00:00
rustc_trait_selection Rollup merge of #110364 - matthiaskrgr:anti_clone, r=Nilstrieb 2023-04-16 06:55:23 +09:00
rustc_traits explicit adt_dtorck_constraint for ManuallyDrop 2023-04-14 15:44:05 +02:00
rustc_transmute Improve safe transmute error reporting 2023-04-13 21:57:08 +00:00
rustc_ty_utils Update ty_utils_never_to_any_not_supported diagnostic message 2023-04-10 22:03:08 +02:00
rustc_type_ir Fix typos in compiler 2023-04-10 22:02:52 +02:00