rust/compiler
Dylan DPC 3530a7895a
Rollup merge of #84701 - nikomatsakis:stabilize-member-constraints-61997, r=jackh726
stabilize member constraints

Stabilizes the use of "member constraints" in solving `impl Trait` bindings. This is a step towards stabilizing a "MVP" of "named impl Trait".

# Member constraint stabilization report

| Info | |
| --- | --- |
| Tracking issue | [rust-lang/rust#61997](https://github.com/rust-lang/rust/issues/61997) |
| Implementation history | [rust-lang/rust#61775] |
| rustc-dev-guide coverage | [link](https://rustc-dev-guide.rust-lang.org/borrow_check/region_inference/member_constraints.html) |
| Complications | [rust-lang/rust#61773] |

[rust-lang/rust#61775]: https://github.com/rust-lang/rust/pull/61775
[rust-lang/rust#61773]: https://github.com/rust-lang/rust/issues/61773

## Background

Member constraints are an extension to our region solver that was introduced to make async fn region solving tractable. There are used in situations like the following:

```rust
fn foo<'a, 'b>(...) -> impl Trait<'a, 'b> { .. }
```

The problem here is that every region R in the hidden type must be equal to *either* `'a` *or* `'b` (or `'static`). This cannot be expressed simply via 'outlives constriants' like `R: 'a`. Therefore, we introduce a 'member constraint' `R member of ['a, 'b]`.

These constraints were introduced in [rust-lang/rust#61775]. At the time, we kept them feature gated and used them only for `impl Trait` return types that are derived from `async fn`. The intention, however, was always to support them in other contexts once we had time to gain more experience with them.

**In the time since their introduction, we have encountered no surprises or bugs due to these member constraints.** They are tested extensively as part of every async function that involves multiple unrelated lifetimes in its arguments.

## Tests

The behavior of member constraints is covered by the following tests:

* [`src/test/ui/async-await/multiple-lifetimes`](20e032e650/src/test/ui/async-await/multiple-lifetimes) -- tests using the async await, which are mostly already stabilized
* [`src/test/ui/impl-trait/multiple-lifetimes.rs`](20e032e650/src/test/ui/impl-trait/multiple-lifetimes.rs)
* [`src/test/ui/impl-trait/multiple-lifetimes/ordinary-bounds-unsuited.rs`](20e032e650/src/test/ui/impl-trait/multiple-lifetimes/ordinary-bounds-unsuited.rs)
* [`src/test/ui/async-await/multiple-lifetimes/ret-impl-trait-fg.rs`](20e032e650/src/test/ui/async-await/multiple-lifetimes/ret-impl-trait-fg.rs)
* [`src/test/ui/async-await/multiple-lifetimes/ret-impl-trait-one.rs`](20e032e650/src/test/ui/async-await/multiple-lifetimes/ret-impl-trait-one.rs)

These tests cover a number of scenarios:

* `-> implTrait<'a, 'b>` with unrelated lifetimes `'a` and `'b`, as described above
* `async fn` that returns an `impl Trait` like the previous case, which desugars to a kind of "nested" impl trait like `impl Future<Output = impl Trait<'a, 'b>>`

## Potential concerns

There is a potential interaction with `impl Trait` on local variables, described in [rust-lang/rust#61773]. The challenge is that if you have a program like:

```rust=
trait Foo<'_> { }
impl Foo<'_> for &u32 { }

fn bar() {
  let x: impl Foo<'_> = &44; // let's call the region variable for `'_` `'1`
}
```

then we would wind up with `'0 member of ['1, 'static]`, where `'0` is the region variable in the hidden type (`&'0 u32`) and `'1` is the region variable in the bounds `Foo<'1>`. This is tricky because both `'0` and `'1` are being inferred -- so making them equal may have other repercussions.

That said, `impl Trait` in bindings are not stable, and the implementation is pretty far from stabilization. Moreover, the difficulty highlighted here is not due to the presence of member constraints -- it's inherent to the design of the language. In other words, stabilizing member constraints does not actually cause us to accept anything that would make this problem any harder.

So I don't see this as a blocker to stabilization of member constraints; it is potentially a blocker to stablization of `impl trait` in let bindings.
2021-05-27 03:02:07 +02:00
..
rustc
rustc_apfloat remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_arena
rustc_ast remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_ast_lowering remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_ast_passes Stabilize extended_key_value_attributes 2021-05-18 01:01:36 -04:00
rustc_ast_pretty remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_attr remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_builtin_macros remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_codegen_cranelift (try to) fix cranelift 2021-05-23 12:44:05 +02:00
rustc_codegen_llvm remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_codegen_ssa remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_data_structures Only compute Obligation cache_key once in register_obligation_at 2021-05-04 11:57:53 -07:00
rustc_driver Auto merge of #83842 - LeSeulArtichaut:thir-vec, r=nikomatsakis 2021-05-19 18:41:23 +00:00
rustc_error_codes swap function order for better read flow 2021-05-13 13:22:24 +02:00
rustc_errors Stabilize extended_key_value_attributes 2021-05-18 01:01:36 -04:00
rustc_expand remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_feature stabilize member constraints 2021-05-26 06:01:53 -04:00
rustc_fs_util
rustc_graphviz
rustc_hir remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_hir_pretty remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_incremental Use () for codegen queries. 2021-05-12 13:58:46 +02:00
rustc_index Change bitwise operator to more easily keep data in vector registers 2021-05-04 11:55:18 -04:00
rustc_infer remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_interface Make thir_check_unsafety itself responsible for checking gate 2021-05-24 15:09:33 +02:00
rustc_lexer
rustc_lint remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_lint_defs Add additional migrations to handle auto-traits and clone traits 2021-05-06 14:17:59 -04:00
rustc_llvm Auto merge of #85416 - durin42:llvm-catchup-may-2021, r=nagisa 2021-05-21 11:21:06 +00:00
rustc_macros
rustc_metadata remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_middle Rollup merge of #85633 - lqd:stackless_span_stacks, r=oli-obk 2021-05-26 13:32:08 +02:00
rustc_mir Rollup merge of #85633 - lqd:stackless_span_stacks, r=oli-obk 2021-05-26 13:32:08 +02:00
rustc_mir_build Fix unused_unsafe in THIR unsafeck 2021-05-25 20:11:29 +02:00
rustc_parse remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_parse_format remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_passes remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_plugin_impl Use () for plugin_registrar_fn. 2021-05-12 13:58:43 +02:00
rustc_privacy remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_query_impl Use () in dependency_formats. 2021-05-12 13:58:41 +02:00
rustc_query_system Show nicer error when an 'unstable fingerprints' error occurs 2021-05-10 17:43:51 -04:00
rustc_resolve Rollup merge of #85478 - FabianWolff:issue-85348, r=petrochenkov 2021-05-26 13:32:05 +02:00
rustc_save_analysis remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_serialize
rustc_session Rollup merge of #85361 - bjorn3:rustdoc_target_json_path_canonicalize, r=jyn514 2021-05-25 13:05:09 +02:00
rustc_span Rollup merge of #85439 - mgacek8:add_diagnostic_item_to_CStr_type, r=davidtwco 2021-05-20 00:19:07 +02:00
rustc_symbol_mangling remove cfg(bootstrap) 2021-05-24 11:07:48 -04:00
rustc_target Remove linker_is_gnu: true cases as that is now the default. 2021-05-20 23:36:04 -07:00
rustc_trait_selection stabilize member constraints 2021-05-26 06:01:53 -04:00
rustc_traits
rustc_ty_utils add new attribute rustc_insignificant_dtor and a query to check if a type has a significant drop 2021-05-14 22:57:33 -04:00
rustc_type_ir
rustc_typeck Rollup merge of #84221 - ABouttefeux:generic-arg-elision, r=estebank 2021-05-27 03:02:03 +02:00