rust/compiler
bors cb3752d20e Auto merge of #124136 - estebank:clone-o-rama-2, r=nnethercote
Provide more context and suggestions in borrowck errors involving closures

Start pointing to where bindings where declared when they are captured in closures:

```
error[E0597]: `x` does not live long enough
  --> $DIR/suggest-return-closure.rs:23:9
   |
LL |     let x = String::new();
   |         - binding `x` declared here
...
LL |     |c| {
   |     --- value captured here
LL |         x.push(c);
   |         ^ borrowed value does not live long enough
...
LL | }
   | -- borrow later used here
   | |
   | `x` dropped here while still borrowed
```

Suggest cloning in more cases involving closures:

```
error[E0507]: cannot move out of `foo` in pattern guard
  --> $DIR/issue-27282-move-ref-mut-into-guard.rs:11:19
   |
LL |             if { (|| { let mut bar = foo; bar.take() })(); false } => {},
   |                   ^^                 --- move occurs because `foo` has type `&mut Option<&i32>`, which does not implement the `Copy` trait
   |                   |
   |                   `foo` is moved here
   |
   = note: variables bound in patterns cannot be moved from until after the end of the pattern guard
help: consider cloning the value if the performance cost is acceptable
   |
LL |             if { (|| { let mut bar = foo.clone(); bar.take() })(); false } => {},
   |                                         ++++++++
```

Mention when type parameter could be Clone

```
error[E0382]: use of moved value: `t`
  --> $DIR/use_of_moved_value_copy_suggestions.rs:7:9
   |
LL | fn duplicate_t<T>(t: T) -> (T, T) {
   |                   - move occurs because `t` has type `T`, which does not implement the `Copy` trait
...
LL |     (t, t)
   |      -  ^ value used here after move
   |      |
   |      value moved here
   |
help: if `T` implemented `Clone`, you could clone the value
  --> $DIR/use_of_moved_value_copy_suggestions.rs:4:16
   |
LL | fn duplicate_t<T>(t: T) -> (T, T) {
   |                ^ consider constraining this type parameter with `Clone`
...
LL |     (t, t)
   |      - you could clone this value
help: consider restricting type parameter `T`
   |
LL | fn duplicate_t<T: Copy>(t: T) -> (T, T) {
   |                 ++++++
```

The `help` is new. On ADTs, we also extend the output with span labels:

```
error[E0507]: cannot move out of static item `FOO`
  --> $DIR/issue-17718-static-move.rs:6:14
   |
LL |     let _a = FOO;
   |              ^^^ move occurs because `FOO` has type `Foo`, which does not implement the `Copy` trait
   |
note: if `Foo` implemented `Clone`, you could clone the value
  --> $DIR/issue-17718-static-move.rs:1:1
   |
LL | struct Foo;
   | ^^^^^^^^^^ consider implementing `Clone` for this type
...
LL |     let _a = FOO;
   |              --- you could clone this value
help: consider borrowing here
   |
LL |     let _a = &FOO;
   |              +
```

Suggest cloning captured binding in move closure

```
error[E0507]: cannot move out of `bar`, a captured variable in an `FnMut` closure
  --> $DIR/borrowck-move-by-capture.rs:9:29
   |
LL |     let bar: Box<_> = Box::new(3);
   |         --- captured outer variable
LL |     let _g = to_fn_mut(|| {
   |                        -- captured by this `FnMut` closure
LL |         let _h = to_fn_once(move || -> isize { *bar });
   |                             ^^^^^^^^^^^^^^^^   ----
   |                             |                  |
   |                             |                  variable moved due to use in closure
   |                             |                  move occurs because `bar` has type `Box<isize>`, which does not implement the `Copy` trait
   |                             `bar` is moved here
   |
help: clone the value before moving it into the closure 1
   |
LL ~         let value = bar.clone();
LL ~         let _h = to_fn_once(move || -> isize { value });
   |
```
2024-04-25 01:00:41 +00:00
..
rustc
rustc_abi Use the Align type when parsing alignment attributes 2024-04-01 03:05:55 +01:00
rustc_arena rename ptr::invalid -> ptr::without_provenance 2024-02-21 20:15:52 +01:00
rustc_ast delegation: Support renaming 2024-04-23 22:38:16 +03:00
rustc_ast_ir introduce Mutability::ptr_str 2024-04-10 18:51:09 +02:00
rustc_ast_lowering Auto merge of #122500 - petrochenkov:deleg, r=fmease 2024-04-24 11:57:35 +00:00
rustc_ast_passes Stabilise inline_const 2024-04-24 13:12:25 +01:00
rustc_ast_pretty Give a name to each distinct manipulation of pretty-printer FixupContext 2024-04-19 23:49:44 -07:00
rustc_attr Stabilize generic NonZero. 2024-04-22 18:48:47 +02:00
rustc_baked_icu_data
rustc_borrowck Suggest cloning captured binding in move closure 2024-04-24 22:21:16 +00:00
rustc_builtin_macros Pass translation closure to add_to_diag_with() as reference 2024-04-21 07:45:03 +00:00
rustc_codegen_cranelift Error on using yield without also using #[coroutine] on the closure 2024-04-24 08:05:29 +00:00
rustc_codegen_gcc Error on using yield without also using #[coroutine] on the closure 2024-04-24 08:05:29 +00:00
rustc_codegen_llvm Auto merge of #122053 - erikdesjardins:alloca, r=nikic 2024-04-24 03:00:44 +00:00
rustc_codegen_ssa Auto merge of #122053 - erikdesjardins:alloca, r=nikic 2024-04-24 03:00:44 +00:00
rustc_const_eval properly fill a promoted's required_consts 2024-04-23 23:02:54 +02:00
rustc_data_structures Stabilize generic NonZero. 2024-04-22 18:48:47 +02:00
rustc_driver
rustc_driver_impl Fix pretty hir for anon consts in diagnostics 2024-04-15 18:48:12 -04:00
rustc_error_codes Error on using yield without also using #[coroutine] on the closure 2024-04-24 08:05:29 +00:00
rustc_error_messages Rename SubdiagnosticMessage as SubdiagMessage. 2024-03-05 12:14:49 +11:00
rustc_errors Rollup merge of #124218 - Xiretza:subsubdiagnostics, r=davidtwco 2024-04-23 17:25:17 +02:00
rustc_expand Auto merge of #124008 - nnethercote:simpler-static_assert_size, r=Nilstrieb 2024-04-18 09:47:45 +00:00
rustc_feature Stabilise inline_const 2024-04-24 13:12:25 +01:00
rustc_fluent_macro Rename SubdiagnosticMessage as SubdiagMessage. 2024-03-05 12:14:49 +11:00
rustc_fs_util Stabilize std::path::absolute 2024-04-24 14:35:02 +00:00
rustc_graphviz
rustc_hir Rollup merge of #120929 - long-long-float:wrap-dyn-in-suggestion, r=fmease 2024-04-23 17:25:14 +02:00
rustc_hir_analysis Auto merge of #122500 - petrochenkov:deleg, r=fmease 2024-04-24 11:57:35 +00:00
rustc_hir_pretty Rename BindingAnnotation to BindingMode 2024-04-17 09:34:39 -04:00
rustc_hir_typeck Rollup merge of #123794 - oli-obk:define_opaque_types2, r=lcnr 2024-04-24 14:00:56 +02:00
rustc_incremental Fix ICE when there is a non-Unicode entry in the incremental crate directory 2024-04-21 11:24:55 +01:00
rustc_index Simplify static_assert_sizes. 2024-04-18 15:36:25 +10:00
rustc_index_macros rustc_index: Add a ZERO constant to index types 2024-04-03 19:06:22 +03:00
rustc_infer Rollup merge of #124218 - Xiretza:subsubdiagnostics, r=davidtwco 2024-04-23 17:25:17 +02:00
rustc_interface Auto merge of #123126 - oli-obk:feed_crate_num, r=davidtwco 2024-04-23 20:46:48 +00:00
rustc_lexer Properly handle emojis as literal prefix in macros 2024-04-10 23:19:27 +00:00
rustc_lint Rollup merge of #124218 - Xiretza:subsubdiagnostics, r=davidtwco 2024-04-23 17:25:17 +02:00
rustc_lint_defs Rename feature gate 2024-04-15 23:27:21 -04:00
rustc_llvm Rollup merge of #124132 - RalfJung:OpBundlesIndirect, r=Mark-Simulacrum 2024-04-20 21:45:37 +01:00
rustc_log Construct SourceMap at the same time as SessionGlobals. 2024-04-16 13:02:53 +10:00
rustc_macros Allow nesting subdiagnostics 2024-04-21 07:45:03 +00:00
rustc_metadata Error on using yield without also using #[coroutine] on the closure 2024-04-24 08:05:29 +00:00
rustc_middle Auto merge of #104087 - nbdd0121:const, r=scottmcm 2024-04-24 17:23:03 +00:00
rustc_mir_build Rollup merge of #124218 - Xiretza:subsubdiagnostics, r=davidtwco 2024-04-23 17:25:17 +02:00
rustc_mir_dataflow Add a non-shallow fake borrow 2024-04-20 16:01:35 +02:00
rustc_mir_transform Stabilise inline_const 2024-04-24 13:12:25 +01:00
rustc_monomorphize Use non-exhaustive matches for TyKind 2024-04-17 20:49:53 +03:00
rustc_next_trait_solver Auto merge of #122077 - oli-obk:eager_opaque_checks4, r=lcnr 2024-04-08 23:01:50 +00:00
rustc_parse Auto merge of #104087 - nbdd0121:const, r=scottmcm 2024-04-24 17:23:03 +00:00
rustc_parse_format Simplify static_assert_sizes. 2024-04-18 15:36:25 +10:00
rustc_passes Rollup merge of #124218 - Xiretza:subsubdiagnostics, r=davidtwco 2024-04-23 17:25:17 +02:00
rustc_pattern_analysis Pass translation closure to add_to_diag_with() as reference 2024-04-21 07:45:03 +00:00
rustc_privacy Actually create ranged int types in the type system. 2024-04-08 12:02:19 +00:00
rustc_query_impl Stabilize generic NonZero. 2024-04-22 18:48:47 +02:00
rustc_query_system Rollup merge of #124252 - michaelwoerister:better-forbidden-read-ice, r=oli-obk 2024-04-22 20:26:00 +02:00
rustc_resolve delegation: Support async, const, extern "ABI" and C-variadic functions 2024-04-23 23:05:39 +03:00
rustc_sanitizers Update encode.rs 2024-04-14 14:56:41 +02:00
rustc_serialize Stabilise inline_const 2024-04-24 13:12:25 +01:00
rustc_session Rollup merge of #124333 - Urgau:better-bad-print, r=fmease 2024-04-25 00:19:55 +02:00
rustc_smir Rollup merge of #122598 - Nadrieril:full-derefpats, r=matthewjasper 2024-04-23 17:25:15 +02:00
rustc_span Rollup merge of #124322 - whosehang:master, r=Nilstrieb 2024-04-25 00:19:54 +02:00
rustc_symbol_mangling Add simple async drop glue generation 2024-04-16 20:45:07 +03:00
rustc_target Auto merge of #117457 - daxpedda:wasm-nontrapping-fptoint, r=wesleywiser 2024-04-21 06:32:10 +00:00
rustc_trait_selection Suggest cloning captured binding in move closure 2024-04-24 22:21:16 +00:00
rustc_traits Merge check_mod_impl_wf and check_mod_type_wf 2024-03-07 06:27:09 +00:00
rustc_transmute ScalarInt: add methods to assert being a (u)int of given size 2024-04-19 13:51:52 +02:00
rustc_ty_utils Auto merge of #121801 - zetanumbers:async_drop_glue, r=oli-obk 2024-04-23 02:10:23 +00:00
rustc_type_ir Rollup merge of #124322 - whosehang:master, r=Nilstrieb 2024-04-25 00:19:54 +02:00
stable_mir Rollup merge of #122598 - Nadrieril:full-derefpats, r=matthewjasper 2024-04-23 17:25:15 +02:00