rust/compiler
Stuart Cook ad14de2375
Rollup merge of #145194 - compiler-errors:coro-witness-re, r=lcnr
Ignore coroutine witness type region args in auto trait confirmation

## The problem

Consider code like:

```
async fn process<'a>() {
    Box::pin(process()).await;
}

fn require_send(_: impl Send) {}

fn main() {
    require_send(process());
}
```

When proving that the coroutine `{coroutine@process}::<'?0>: Send`, we end up instantiating a nested goal `{witness@process}::<'?0>: Send` by synthesizing a witness type from the coroutine's args:

Proving a coroutine witness type implements an auto trait requires looking up the coroutine's witness types. The witness types are a binder that look like `for<'r> { Pin<Box<{coroutine@process}::<'r>>> }`. We instantiate this binder with placeholders and prove `Send` on the witness types. This ends up eventually needing to prove something like `{coroutine@process}::<'!1>: Send`. Repeat this process, and we end up in an overflow during fulfillment, since fulfillment does not use freshening.

This can be visualized with a trait stack that ends up looking like:
* `{coroutine@process}::<'?0>: Send`
  * `{witness@process}::<'?0>: Send`
    * `Pin<Box<{coroutine@process}::<'!1>>>: Send`
      * `{coroutine@process}::<'!1>: Send`
        * ...
          * `{coroutine@process}::<'!2>: Send`
            * `{witness@process}::<'!2>: Send`
              * ...
                * overflow!

The problem here specifically comes from the first step: synthesizing a witness type from the coroutine's args.

## Why wasn't this an issue before?

Specifically, before 63f6845e57, this wasn't an issue because we were instead extracting the witness from the coroutine type itself. It turns out that given some `{coroutine@process}::<'?0>`, the witness type was actually something like `{witness@process}::<'erased>`!

So why do we end up with a witness type with `'erased` in its args? This is due to the fact that opaque type inference erases all regions from the witness. This is actually explicitly part of opaque type inference -- changing this to actually visit the witness types actually replicates this overflow even with 63f6845e57 reverted:

ca77504943/compiler/rustc_borrowck/src/type_check/opaque_types.rs (L303-L313)

To better understand this difference and how it avoids a cycle, if you look at the trait stack before 63f6845e57, we end up with something like:

* `{coroutine@process}::<'?0>: Send`
  * `{witness@process}::<'erased>: Send` **<-- THIS CHANGED**
    * `Pin<Box<{coroutine@process}::<'!1>>>: Send`
      * `{coroutine@process}::<'!1>: Send`
        * ...
          * `{coroutine@process}::<'erased>: Send` **<-- THIS CHANGED**
            * `{witness@process}::<'erased>: Send` **<-- THIS CHANGED**
              * coinductive cycle! 🎉

## So what's the fix?

This hack replicates the behavior in opaque type inference to erase regions from the witness type, but instead erasing the regions during auto trait confirmation. This is kinda a hack, but is sound. It does not need to be replicated in the new trait solver, of course.

---

I hope this explanation makes sense.

We could beta backport this instead of the revert https://github.com/rust-lang/rust/pull/145193, but then I'd like to un-revert that on master in this PR along with landing this this hack. Thoughts?

r? lcnr
2025-08-11 18:22:33 +10:00
..
rustc rename stable_mir to rustc_public, and rustc_smir to rustc_public_bridge 2025-07-14 09:25:54 +00:00
rustc_abi Reuse sign_extend helper 2025-07-29 14:17:48 +00:00
rustc_arena Introduce Arena::try_alloc_from_iter. 2025-04-19 01:13:18 +00:00
rustc_ast remove P 2025-08-09 15:47:01 +08:00
rustc_ast_ir Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_ast_lowering Rollup merge of #135331 - fmease:ban-assoc-ty-unbounds, r=lcnr 2025-08-11 18:22:31 +10:00
rustc_ast_passes remove P 2025-08-09 15:47:01 +08:00
rustc_ast_pretty remove P 2025-08-09 15:47:01 +08:00
rustc_attr_parsing Revert "Port #[allow_internal_unsafe] to the new attribute system" 2025-08-08 11:54:20 +02: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 remove from_forall 2025-08-11 09:18:46 +02:00
rustc_builtin_macros remove P 2025-08-09 15:47:01 +08:00
rustc_codegen_cranelift Rollup merge of #144192 - RalfJung:atomicrmw-ptr, r=nikic 2025-08-08 14:22:44 -05:00
rustc_codegen_gcc Rollup merge of #144192 - RalfJung:atomicrmw-ptr, r=nikic 2025-08-08 14:22:44 -05:00
rustc_codegen_llvm Stop using uadd.with.overflow 2025-08-08 21:59:28 -07:00
rustc_codegen_ssa Rollup merge of #141828 - Fulgen301:status-stack-buffer-overrun-diagnostic, r=wesleywiser 2025-08-09 13:58:43 +10:00
rustc_const_eval rename TraitRef::from_method to from_assoc 2025-08-09 14:22:01 +08:00
rustc_data_structures Allow pretty printing paths with -Zself-profile-events=args 2025-07-25 22:24:21 +02:00
rustc_driver Remove recursion_limit increases. 2025-04-02 16:25:27 +11:00
rustc_driver_impl Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_error_codes Update E0562 to account for the new impl trait positions 2025-08-02 11:29:04 +02:00
rustc_error_messages Remove unused feature gates 2025-06-28 23:36:46 +00:00
rustc_errors Rollup merge of #144558 - estebank:issue-68119, r=lcnr 2025-08-11 12:21:08 +10:00
rustc_expand Rollup merge of #145200 - joshtriplett:mbe-typo-fix, r=lqd 2025-08-10 15:43:55 -04:00
rustc_feature Fix an unstable feature comment that wasn't a doc comment 2025-08-09 15:17:02 -07:00
rustc_fluent_macro Remove all unused feature gates from the compiler 2025-06-08 14:50:42 +00:00
rustc_fs_util Retry if creating temp fails with access denied 2025-04-25 11:28:36 +00: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 Auto merge of #144873 - cjgillot:implications, r=lqd 2025-08-10 05:44:53 +00:00
rustc_hir_analysis Rollup merge of #135331 - fmease:ban-assoc-ty-unbounds, r=lcnr 2025-08-11 18:22:31 +10:00
rustc_hir_pretty remove rustc_attr_data_structures 2025-07-31 14:19:27 +02:00
rustc_hir_typeck Account for macros when trying to point at inference cause 2025-08-10 21:47:52 +00:00
rustc_incremental Auto merge of #139758 - Zoxc:thread-local-graph, r=oli-obk 2025-05-07 12:39:54 +00:00
rustc_index Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_index_macros Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_infer remove from_forall 2025-08-11 09:18:46 +02:00
rustc_interface Auto merge of #145146 - fee1-dead-contrib:push-zmqrkurlzrxy, r=nnethercote 2025-08-09 23:27:55 +00:00
rustc_lexer test(lexer): Add frontmatter unit test 2025-07-10 10:25:29 -05:00
rustc_lint Auto merge of #145146 - fee1-dead-contrib:push-zmqrkurlzrxy, r=nnethercote 2025-08-09 23:27:55 +00:00
rustc_lint_defs Start reporting future breakage for ILL_FORMED_ATTRIBUTE_INPUT in dependencies 2025-08-10 10:48:08 +02:00
rustc_llvm Fix typo with paren rustc_llvm/build.rs 2025-08-10 12:22:13 -07:00
rustc_log Add documentation for init_logger_with_additional_layer 2025-06-12 12:11:15 +02:00
rustc_macros remove rustc_attr_data_structures 2025-07-31 14:19:27 +02:00
rustc_metadata Detect struct construction with private field in field with default 2025-08-10 19:15:18 +00:00
rustc_middle review comments 2025-08-10 19:15:20 +00:00
rustc_mir_build Auto merge of #143376 - dianne:guard-scope, r=matthewjasper 2025-08-09 03:19:26 +00:00
rustc_mir_dataflow Escape diff strings in graphviz 2025-08-08 00:20:55 -05:00
rustc_mir_transform Rollup merge of #144883 - scottmcm:remove-unneeded-drop_in_place, r=nnethercote 2025-08-09 13:58:44 +10:00
rustc_monomorphize Add support for shortening Instance and use it 2025-08-06 22:21:49 +00:00
rustc_next_trait_solver some derive_more refactors 2025-08-09 22:24:44 +08:00
rustc_parse Auto merge of #145146 - fee1-dead-contrib:push-zmqrkurlzrxy, r=nnethercote 2025-08-09 23:27:55 +00:00
rustc_parse_format update to literal-escaper-0.0.5 2025-07-08 10:16:44 +00:00
rustc_passes Auto merge of #144873 - cjgillot:implications, r=lqd 2025-08-10 05:44:53 +00:00
rustc_pattern_analysis Rollup merge of #144545 - ChayimFriedman2:bool-witness-order, r=Nadrieril 2025-08-08 14:22:44 -05:00
rustc_privacy Auto merge of #144554 - cjgillot:no-hir-eff-vis, r=petrochenkov 2025-08-02 09:23:36 +00:00
rustc_proc_macro Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_public fix missing parenthesis in pretty discriminant 2025-08-09 01:35:50 +08:00
rustc_public_bridge Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
rustc_query_impl Remove unused allow attrs 2025-07-07 12:58:16 +00:00
rustc_query_system remove rustc_attr_data_structures 2025-07-31 14:19:27 +02:00
rustc_resolve review comments 2025-08-10 19:15:20 +00:00
rustc_sanitizers rename TraitRef::from_method to from_assoc 2025-08-09 14:22:01 +08:00
rustc_serialize use div_ceil instead of manual logic 2025-07-05 10:55:42 +02:00
rustc_session coverage: Remove all unstable support for MC/DC instrumentation 2025-08-06 22:38:52 +10:00
rustc_span Rollup merge of #145132 - camsteffen:refactor-map-unit-fn, r=fee1-dead 2025-08-09 13:58:55 +10:00
rustc_symbol_mangling Reimplement print_region in type_name.rs. 2025-08-07 12:46:33 +10:00
rustc_target Add target_env = "macabi" and target_env = "sim" 2025-08-08 13:29:46 +02:00
rustc_thread_pool Drop rust-version from rustc_thread_pool 2025-08-04 15:03:49 -07:00
rustc_trait_selection Rollup merge of #145194 - compiler-errors:coro-witness-re, r=lcnr 2025-08-11 18:22:33 +10:00
rustc_traits Auto merge of #144446 - nnethercote:opt-region-constraints, r=lcnr 2025-08-01 04:06:21 +00:00
rustc_transmute Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_ty_utils Rollup merge of #145147 - fee1-dead-contrib:push-mxxpmlpmzmsz, r=compiler-errors 2025-08-10 19:45:53 +10:00
rustc_type_ir Rollup merge of #145147 - fee1-dead-contrib:push-mxxpmlpmzmsz, r=compiler-errors 2025-08-10 19:45:53 +10:00
rustc_type_ir_macros Split TypeFolder and FallibleTypeFolder 2025-04-15 18:30:35 +00:00