rust/compiler
bors 922958cffe Auto merge of #145244 - lcnr:handle-opaque-types-before-region-inference, r=BoxyUwU
support non-defining uses of opaques in borrowck

Reimplements the first part of rust-lang/rust#139587, but limited to only the new solver. To do so I also entirely rewrite the way we handle opaque types in borrowck even on stable. This should not impact behavior however.

We now support revealing uses during MIR borrowck with the new solver:
```rust
fn foo<'a>(x: &'a u32) -> impl Sized + use<'a> {
    let local = 1;
    foo::<'_>(&local);
    x
}
```

### How do opaque types work right now

Whenever we use an opaque type during type checking, we remember this use in the `opaque_type_storage` of the `InferCtxt`.

Right now, we collect all *member constraints* at the end of MIR type check by looking at all uses from the `opaque_type_storage`. We then apply these constraints while computing the region values for each SCC. This does not add actual region constraints but directly updates the final region values.

This means we need to manually handle any constraints from member constraints for diagnostics. We do this by separately tracking `applied_member_constraints` in the `RegionInferenceContext`.

After we've finished computing the region values, it is now immutable and we check whether all member constraints hold. If not, we error.

We now map the hidden types of our defining uses to the defining scope. This assumes that all member constraints apply. To handle non-member regions, we simply map any region in the hidden type we fail to map to a choice region to `'erased` b1b26b834d/compiler/rustc_borrowck/src/region_infer/opaque_types.rs (L126-L132)

### How do we handle opaque types with this PR

MIR type check still works the same by populating the `opaque_type_storage` whenever we use an opaque type.

We now have a new step `fn handle_opaque_type_uses` which happens between MIR type check and `compute_regions`.

This step looks at all opaque type uses in the storage and first checks whether they are defining: are the arguments of the `opaque_type_key` unique region parameters. *With the new solver we silently ignore any *non-defining* uses here. The old solver emits an errors.*

`fn compute_concrete_opaque_types`: We then collect all member constraints for the defining uses and apply them just like we did before. However, we do it on a temporary region graph which is only used while computing the concrete opaque types. We then use this region graph to compute the concrete type which we then store in the `root_cx`.

`fn apply_computed_concrete_opaque_types`: Now that we know the final concrete type of each opaque type and have mapped them to the definition of the opaque. We iterate over all opaque type uses and equate their hidden type with the instantiated final concrete type. This is the step which actually mutates the region graph.

The actual region checking can now entirely ignores opaque types (outside of the `ConstraintCategory` from checking the opaque type uses).

### Diagnostics issue (chill)

Because we now simply use type equality to "apply member constraints" we get ordinary `OutlivesConstraint`s, even if the regions were already related to another.

This is generally not an issue, expect that it can *hide* the actual region constraints which resulted in the final value of the opaque. The constraints we get from checking against the final opaque type definition relies on constraints we used to compute that definition.

I mostly handle this by updating `find_constraint_path_between_regions` to first ignore member constraints in its search and only if that does not find a path, retry while considering member constraints.

### Diagnostics issue (not chill)

A separate issue is that `find_constraint_paths_between_regions` currently looks up member constraints by their **scc**, not by region value:
2c1ac85679/compiler/rustc_borrowck/src/region_infer/mod.rs (L1768-L1775)

This means that in the `borrowck-4` test, the resulting constraint path is currently
```
('?2: '?5) due to Single(bb0[5]) (None) (Boring) (ConstraintSccIndex(1): ConstraintSccIndex(1)),
('?5: '?3) due to Single(bb0[6]) (None) (Boring) (ConstraintSccIndex(1): ConstraintSccIndex(1)),
('?3: '?0) due to All(src/main.rs:15:5: 15:6 (#0)) (None) (OpaqueType) (ConstraintSccIndex(1): ConstraintSccIndex(1))
```
Here `'?3` is equal to `'?4`, but the reason why it's in the opaque is that it's related to `'?4`. With my PR this will be correctly tracked so we end up with
```
('?2: '?5) due to Single(bb0[5]) (None) (Boring) (ConstraintSccIndex(1): ConstraintSccIndex(1)),
('?5: '?3) due to Single(bb0[6]) (None) (Boring) (ConstraintSccIndex(1): ConstraintSccIndex(1)),
('?3: '?4) due to Single(bb0[6]) (None) (Assignment) (ConstraintSccIndex(1): ConstraintSccIndex(1)),
('?4: '?0) due to All(src/main.rs:15:5: 15:6 (#0)) (None) (OpaqueType) (ConstraintSccIndex(1): ConstraintSccIndex(1)),
```
This additional `Assignment` step then worsens the error message as we stop talking about the fact that the closures is returned from the function. Fixing this is hard. I've looked into this and it's making me sad :< Properly handling this requires some deeper changes to MIR borrowck diagnostics and that seems like too much for this PR. Given that this only impacts a single test, it seems acceptable to me.

r? `@ghost`
2025-08-21 03:49:17 +00: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
rustc_ast Auto merge of #145348 - nnethercote:parse_token_tree-speedup-for-uom, r=petrochenkov 2025-08-20 09:01:41 +00:00
rustc_ast_ir Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_ast_lowering Pass the target type down to parse_attribute_list 2025-08-14 18:11:56 +02:00
rustc_ast_passes Rollup merge of #144907 - ShoyuVanilla:no-const-async, r=fmease 2025-08-15 18:13:28 -04:00
rustc_ast_pretty Extract ast TraitImplHeader 2025-08-11 17:05:36 -05:00
rustc_attr_parsing Auto merge of #145601 - jieyouxu:rollup-t5mbqhc, r=jieyouxu 2025-08-19 23:52:06 +00:00
rustc_baked_icu_data
rustc_borrowck diagnostics :3 2025-08-20 11:10:38 +02:00
rustc_builtin_macros Auto merge of #145601 - jieyouxu:rollup-t5mbqhc, r=jieyouxu 2025-08-19 23:52:06 +00:00
rustc_codegen_cranelift Port the #[linkage] attribute to the new attribute system 2025-08-13 21:01:37 +02:00
rustc_codegen_gcc Complete functionality and general cleanup 2025-08-14 16:30:15 +00:00
rustc_codegen_llvm Auto merge of #145259 - nikic:read-only-capture, r=wesleywiser 2025-08-20 23:41:41 +00:00
rustc_codegen_ssa Auto merge of #145601 - jieyouxu:rollup-t5mbqhc, r=jieyouxu 2025-08-19 23:52:06 +00:00
rustc_const_eval Rollup merge of #145623 - compiler-errors:pretty-async-name, r=wesleywiser 2025-08-20 00:46:00 -04:00
rustc_data_structures Allow pretty printing paths with -Zself-profile-events=args 2025-07-25 22:24:21 +02:00
rustc_driver
rustc_driver_impl Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_error_codes Rollup merge of #144944 - He1pa:E0793, r=compiler-errors 2025-08-15 16:16:32 +10:00
rustc_error_messages Remove unused feature gates 2025-06-28 23:36:46 +00:00
rustc_errors Rollup merge of #139345 - smoelius:into-iter-stability, r=lcnr 2025-08-19 19:42:00 +08:00
rustc_expand Rollup merge of #139345 - smoelius:into-iter-stability, r=lcnr 2025-08-19 19:42:00 +08:00
rustc_feature Auto merge of #144086 - clubby789:alloc-zeroed, r=nikic 2025-08-20 17:16:34 +00: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
rustc_hashes
rustc_hir Rollup merge of #145206 - scrabsha:push-uxovoqzrxnlx, r=jdonszelmann 2025-08-18 15:31:10 +10:00
rustc_hir_analysis make prefetch intrinsics safe 2025-08-20 00:35:42 +02:00
rustc_hir_pretty Propagate TraitImplHeader to hir 2025-08-11 17:05:42 -05:00
rustc_hir_typeck Rollup merge of #144804 - WaffleLapkin:reach-for-the-casts, r=compiler-errors 2025-08-19 14:18:20 +10: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 nll-relate: improve hr opaque types support 2025-08-18 09:09:42 +02:00
rustc_interface Rollup merge of #140740 - ojeda:indirect-branch-cs-prefix, r=davidtwco 2025-08-19 19:42:01 +08:00
rustc_lexer test(lexer): Add frontmatter unit test 2025-07-10 10:25:29 -05:00
rustc_lint Rollup merge of #140794 - karolzwolak:allow-unused-doc-65464, r=davidtwco 2025-08-20 00:45:53 -04:00
rustc_lint_defs Remove the no_sanitize attribute in favor of sanitize 2025-08-18 08:45:28 +00:00
rustc_llvm Tell LLVM about read-only captures 2025-08-20 19:08:16 +02:00
rustc_log Add documentation for init_logger_with_additional_layer 2025-06-12 12:11:15 +02:00
rustc_macros remove should_render in PrintAttribute derive 2025-08-16 21:41:39 +08:00
rustc_metadata Auto merge of #145600 - jieyouxu:rollup-jw0bpnt, r=jieyouxu 2025-08-19 19:26:10 +00:00
rustc_middle Rollup merge of #145623 - compiler-errors:pretty-async-name, r=wesleywiser 2025-08-20 00:46:00 -04:00
rustc_mir_build Rollup merge of #145206 - scrabsha:push-uxovoqzrxnlx, r=jdonszelmann 2025-08-18 15:31:10 +10:00
rustc_mir_dataflow Escape diff strings in graphviz 2025-08-08 00:20:55 -05:00
rustc_mir_transform Auto merge of #145284 - nnethercote:type_name-print-regions, r=lcnr 2025-08-17 10:24:20 +00:00
rustc_monomorphize Rollup merge of #144865 - WaffleLapkin:track-tail, r=lqd 2025-08-15 16:16:31 +10:00
rustc_next_trait_solver Auto merge of #145600 - jieyouxu:rollup-jw0bpnt, r=jieyouxu 2025-08-19 19:26:10 +00:00
rustc_parse Auto merge of #145348 - nnethercote:parse_token_tree-speedup-for-uom, r=petrochenkov 2025-08-20 09:01:41 +00:00
rustc_parse_format update to literal-escaper-0.0.5 2025-07-08 10:16:44 +00:00
rustc_passes Rollup merge of #145500 - JonathanBrouwer:must_use_target, r=jdonszelmann 2025-08-19 19:45:36 +08:00
rustc_pattern_analysis avoid unnecessary type sanity checks 2025-08-14 09:44:22 +02:00
rustc_privacy Propagate TraitImplHeader to hir 2025-08-11 17:05:42 -05: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 Auto merge of #145600 - jieyouxu:rollup-jw0bpnt, r=jieyouxu 2025-08-19 19:26:10 +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 mention lint group in default level lint note 2025-08-19 21:27:10 +02:00
rustc_span Auto merge of #144086 - clubby789:alloc-zeroed, r=nikic 2025-08-20 17:16:34 +00:00
rustc_symbol_mangling Rollup merge of #145429 - bjorn3:codegen_fn_attrs_improvements, r=jdonszelmann 2025-08-19 19:45:31 +08:00
rustc_target Auto merge of #145259 - nikic:read-only-capture, r=wesleywiser 2025-08-20 23:41:41 +00:00
rustc_thread_pool Drop rust-version from rustc_thread_pool 2025-08-04 15:03:49 -07:00
rustc_trait_selection handle opaque types before region inference 2025-08-20 11:04:38 +02: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 Tell LLVM about read-only captures 2025-08-20 19:08:16 +02:00
rustc_type_ir Rollup merge of #145338 - lcnr:coroutine-witness-yikes, r=compiler-errors 2025-08-19 19:45:30 +08:00
rustc_type_ir_macros