rust/compiler
Jacob Pratt e9f6e01b3a
Rollup merge of #138517 - compiler-errors:better-child-capture, r=oli-obk
Improve upvar analysis for deref of child capture

Two fixes to the heuristic I implemented in #123660. As I noted in the code:

> Luckily, if this function is not correct, then the program is not unsound, since we still borrowck and validate the choices made from this function -- the only side-effect is that the user may receive unnecessary borrowck errors.

This indeed fixes unnecessary borrowck errors.

r? oli-obk

---

The heuristic is only valid if we deref a `&T`, not a `&mut T` or `Box<T>`, so make sure to check the type. This fixes:

```rust
struct Foo { precise: i32 }

fn mut_ref_inside_mut(f: &mut Foo) {
    let x: impl AsyncFn() = async move || {
        let y = &f.precise;
    };
}
```

Since the capture from `f` to `&f.precise` needs to be treated as a lending borrow from the parent coroutine-closure to the child coroutine.

---

The heuristic is also valid if *any* deref projection in the child capture's projections is a `&T`, but we were only looking at the last one. This ensures that this function is considered not to be lending:

```rust
struct Foo { precise: i32 }

fn ref_inside_mut(f: &mut &Foo) {
    let x: impl Fn() -> _ = async move || {
        let y = &f.precise;
    };
}
```

(Specifically, checking that `impl Fn() -> _` is satisfied is exercising that the coroutine is not considered to be lending.)
2025-03-17 05:47:51 -04:00
..
rustc Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_abi Use {Decodable,Encodable}_NoContext in type_ir 2025-03-15 06:34:36 +00:00
rustc_arena Remove #![warn(unreachable_pub)] from all compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_ast Rollup merge of #136355 - GuillaumeGomez:proc-macro_add_value_retrieval_methods, r=Amanieu 2025-03-17 05:47:48 -04:00
rustc_ast_ir Use {Decodable,Encodable}_NoContext in type_ir 2025-03-15 06:34:36 +00:00
rustc_ast_lowering Rollup merge of #138482 - nnethercote:fix-hir-printing, r=compiler-errors 2025-03-15 00:18:25 +01:00
rustc_ast_passes Rollup merge of #138331 - nnethercote:use-RUSTC_LINT_FLAGS-more, r=onur-ozkan,jieyouxu 2025-03-12 10:19:30 -07:00
rustc_ast_pretty Rollup merge of #138331 - nnethercote:use-RUSTC_LINT_FLAGS-more, r=onur-ozkan,jieyouxu 2025-03-12 10:19:30 -07:00
rustc_attr_data_structures Rollup merge of #138331 - nnethercote:use-RUSTC_LINT_FLAGS-more, r=onur-ozkan,jieyouxu 2025-03-12 10:19:30 -07:00
rustc_attr_parsing Auto merge of #138414 - matthiaskrgr:rollup-9ablqdb, r=matthiaskrgr 2025-03-12 17:27:43 +00:00
rustc_baked_icu_data Add unreachable_pub to RUSTC_LINT_FLAGS for compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_borrowck Auto merge of #138532 - matthiaskrgr:rollup-mgcynqu, r=matthiaskrgr 2025-03-15 11:40:17 +00:00
rustc_builtin_macros Auto merge of #138414 - matthiaskrgr:rollup-9ablqdb, r=matthiaskrgr 2025-03-12 17:27:43 +00:00
rustc_codegen_cranelift Rollup merge of #138082 - thaliaarchi:slice-cfg-not-test, r=thomcc 2025-03-16 09:40:05 +08:00
rustc_codegen_gcc intrinsics: remove unnecessary leading underscore from argument names 2025-03-12 08:04:09 +01:00
rustc_codegen_llvm Auto merge of #137011 - LuuuXXX:promote-ohos-with-host-tools, r=Amanieu 2025-03-16 18:42:18 +00:00
rustc_codegen_ssa Rollup merge of #138549 - scottmcm:option-ssa, r=saethlin 2025-03-16 13:19:53 +08:00
rustc_const_eval Stop relying on rustc_type_ir in non-type-system crates 2025-03-15 06:42:48 +00:00
rustc_data_structures Auto merge of #137011 - LuuuXXX:promote-ohos-with-host-tools, r=Amanieu 2025-03-16 18:42:18 +00:00
rustc_driver Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_driver_impl Rename PrintKind::{AllTargetSpecs,TargetSpec} to {AllTargetSpecsJson,TargetSpecJson} 2025-03-16 21:56:02 +08:00
rustc_error_codes Rollup merge of #138331 - nnethercote:use-RUSTC_LINT_FLAGS-more, r=onur-ozkan,jieyouxu 2025-03-12 10:19:30 -07:00
rustc_error_messages Rollup merge of #138404 - bjorn3:sysroot_handling_cleanup, r=petrochenkov,jieyouxu 2025-03-13 11:28:35 +01:00
rustc_errors Do not suggest using -Zmacro-backtrace for builtin macros 2025-03-14 19:50:03 +00:00
rustc_expand Do not suggest using -Zmacro-backtrace for builtin macros 2025-03-14 19:50:03 +00:00
rustc_feature Rollup merge of #138331 - nnethercote:use-RUSTC_LINT_FLAGS-more, r=onur-ozkan,jieyouxu 2025-03-12 10:19:30 -07:00
rustc_fluent_macro Remove #![warn(unreachable_pub)] from all compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_fs_util Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_graphviz Remove #![warn(unreachable_pub)] from all compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_hashes Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_hir Rollup merge of #138109 - Kohei316:feat/rust-doc-precise-capturing-arg, r=aDotInTheVoid,compiler-errors 2025-03-13 11:28:26 +01:00
rustc_hir_analysis Add RTN support to rustdoc 2025-03-15 18:13:27 +00:00
rustc_hir_pretty Fix HIR param pretty printing some more. 2025-03-14 09:45:41 +11:00
rustc_hir_typeck Rollup merge of #138517 - compiler-errors:better-child-capture, r=oli-obk 2025-03-17 05:47:51 -04:00
rustc_incremental Auto merge of #138416 - Manishearth:rollup-fejor9p, r=Manishearth 2025-03-13 01:37:26 +00:00
rustc_index Use {Decodable,Encodable}_NoContext in type_ir 2025-03-15 06:34:36 +00:00
rustc_index_macros Add unreachable_pub to RUSTC_LINT_FLAGS for compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_infer Fold visit into ty 2025-03-15 06:34:36 +00:00
rustc_interface Rollup merge of #138404 - bjorn3:sysroot_handling_cleanup, r=petrochenkov,jieyouxu 2025-03-13 11:28:35 +01:00
rustc_lexer Rollup merge of #136355 - GuillaumeGomez:proc-macro_add_value_retrieval_methods, r=Amanieu 2025-03-17 05:47:48 -04:00
rustc_lint Stop relying on rustc_type_ir in non-type-system crates 2025-03-15 06:42:48 +00:00
rustc_lint_defs Remove #![warn(unreachable_pub)] from all compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_llvm Auto merge of #137011 - LuuuXXX:promote-ohos-with-host-tools, r=Amanieu 2025-03-16 18:42:18 +00:00
rustc_log Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_macros Move codec module back into middle 2025-03-15 06:42:48 +00:00
rustc_metadata Stop relying on rustc_type_ir in non-type-system crates 2025-03-15 06:42:48 +00:00
rustc_middle Rollup merge of #138549 - scottmcm:option-ssa, r=saethlin 2025-03-16 13:19:53 +08:00
rustc_mir_build Delegation: allow foreign fns reuse 2025-03-13 14:13:07 +03:00
rustc_mir_dataflow Auto merge of #138414 - matthiaskrgr:rollup-9ablqdb, r=matthiaskrgr 2025-03-12 17:27:43 +00:00
rustc_mir_transform Don't drop Rvalue::WrapUnsafeBinder during GVN 2025-03-15 18:10:55 +00:00
rustc_monomorphize Auto merge of #138414 - matthiaskrgr:rollup-9ablqdb, r=matthiaskrgr 2025-03-12 17:27:43 +00:00
rustc_next_trait_solver Use {Decodable,Encodable}_NoContext in type_ir 2025-03-15 06:34:36 +00:00
rustc_parse Rollup merge of #136355 - GuillaumeGomez:proc-macro_add_value_retrieval_methods, r=Amanieu 2025-03-17 05:47:48 -04:00
rustc_parse_format Rollup merge of #136355 - GuillaumeGomez:proc-macro_add_value_retrieval_methods, r=Amanieu 2025-03-17 05:47:48 -04:00
rustc_passes Auto merge of #138416 - Manishearth:rollup-fejor9p, r=Manishearth 2025-03-13 01:37:26 +00:00
rustc_pattern_analysis Remove #![warn(unreachable_pub)] from all compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_privacy Auto merge of #138416 - Manishearth:rollup-fejor9p, r=Manishearth 2025-03-13 01:37:26 +00:00
rustc_query_impl Auto merge of #137011 - LuuuXXX:promote-ohos-with-host-tools, r=Amanieu 2025-03-16 18:42:18 +00:00
rustc_query_system Rollup merge of #138331 - nnethercote:use-RUSTC_LINT_FLAGS-more, r=onur-ozkan,jieyouxu 2025-03-12 17:59:08 +01:00
rustc_resolve resolve: Avoid some unstable iteration 2025-03-14 18:34:08 +03:00
rustc_sanitizers Squash fold into ty 2025-03-15 06:34:36 +00:00
rustc_serialize Use {Decodable,Encodable}_NoContext in type_ir 2025-03-15 06:34:36 +00:00
rustc_session Rename PrintKind::{AllTargetSpecs,TargetSpec} to {AllTargetSpecsJson,TargetSpecJson} 2025-03-16 21:56:02 +08:00
rustc_smir Auto merge of #138416 - Manishearth:rollup-fejor9p, r=Manishearth 2025-03-13 01:37:26 +00:00
rustc_span Do not suggest using -Zmacro-backtrace for builtin macros 2025-03-14 19:50:03 +00:00
rustc_symbol_mangling Auto merge of #138414 - matthiaskrgr:rollup-9ablqdb, r=matthiaskrgr 2025-03-12 17:27:43 +00:00
rustc_target Rollup merge of #137621 - Berrysoft:cygwin-std, r=joboet 2025-03-17 05:47:49 -04:00
rustc_trait_selection Fold visit into ty 2025-03-15 06:34:36 +00:00
rustc_traits Rollup merge of #138394 - lcnr:yeet-variant, r=compiler-errors 2025-03-12 10:19:32 -07:00
rustc_transmute Auto merge of #138414 - matthiaskrgr:rollup-9ablqdb, r=matthiaskrgr 2025-03-12 17:27:43 +00:00
rustc_ty_utils Stop relying on rustc_type_ir in non-type-system crates 2025-03-15 06:42:48 +00:00
rustc_type_ir Move codec module back into middle 2025-03-15 06:42:48 +00:00
rustc_type_ir_macros Fold visit into ty 2025-03-15 06:34:36 +00:00
stable_mir Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00