Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve opaque type higher-ranked region error message under NLL #92306

Merged
merged 1 commit into from
Feb 9, 2022

Conversation

Aaron1011
Copy link
Member

Currently, any higher-ranked region errors involving opaque types
fall back to a generic "higher-ranked subtype error" message when
run under NLL. This PR adds better error message handling for this
case, giving us the same kinds of error messages that we currently
get without NLL:

error: implementation of `MyTrait` is not general enough
  --> $DIR/opaque-hrtb.rs:12:13
   |
LL | fn foo() -> impl for<'a> MyTrait<&'a str> {
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `MyTrait` is not general enough
   |
   = note: `impl MyTrait<&'2 str>` must implement `MyTrait<&'1 str>`, for any lifetime `'1`...
   = note: ...but it actually implements `MyTrait<&'2 str>`, for some specific lifetime `'2`

error: aborting due to previous error

To accomplish this, several different refactoring needed to be made:

  • We now have a dedicated InstantiateOpaqueType struct which
    implements TypeOp. This is used to invoke instantiate_opaque_types
    during MIR type checking.
  • TypeOp is refactored to pass around a MirBorrowckCtxt, which is
    needed to report opaque type region errors.
  • We no longer assume that all TypeOps correspond to canonicalized
    queries. This allows us to properly handle opaque type instantiation
    (which does not occur in a query) as a TypeOp.
    A new ErrorInfo associated type is used to determine what
    additional information is used during higher-ranked region error
    handling.
  • The body of try_extract_error_from_fulfill_cx
    has been moved out to a new function try_extract_error_from_region_constraints.
    This allows us to re-use the same error reporting code between
    canonicalized queries (which can extract region constraints directly
    from a fresh InferCtxt) and opaque type handling (which needs to take
    region constraints from the pre-existing InferCtxt that we use
    throughout MIR borrow checking).

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 27, 2021
@rust-highfive
Copy link
Collaborator

r? @davidtwco

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 27, 2021
@Aaron1011
Copy link
Member Author

cc @matthewjasper @oli-obk @lqd

--> $DIR/opaque-hrtb.rs:12:13
|
LL | fn foo() -> impl for<'a> MyTrait<&'a str> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `MyTrait` is not general enough
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This span isn't great, but it's the same one that we get without #![feature(nll)]. I'll probably work on improving it in a follow-up PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most such spans will get better with my lazy TAIT PR

@Aaron1011
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 27, 2021
@bors
Copy link
Contributor

bors commented Dec 27, 2021

⌛ Trying commit 1bd8e29ed18ad76b00d71d3d51b1d5a94888e6d4 with merge 9cb40efda24c4b68d6c59a8e3d6d294ac2f86e67...

@bors
Copy link
Contributor

bors commented Dec 27, 2021

☀️ Try build successful - checks-actions
Build commit: 9cb40efda24c4b68d6c59a8e3d6d294ac2f86e67 (9cb40efda24c4b68d6c59a8e3d6d294ac2f86e67)

@rust-timer
Copy link
Collaborator

Queued 9cb40efda24c4b68d6c59a8e3d6d294ac2f86e67 with parent f8abed9, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9cb40efda24c4b68d6c59a8e3d6d294ac2f86e67): comparison url.

Summary: This change led to moderate relevant regressions 😿 in compiler performance.

  • Moderate regression in instruction counts (up to 1.2% on incr-unchanged builds of deeply-nested-async)

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR led to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf +perf-regression

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Dec 27, 2021
Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really cool and may make some things easier for me actually

compiler/rustc_borrowck/src/region_infer/mod.rs Outdated Show resolved Hide resolved
@oli-obk
Copy link
Contributor

oli-obk commented Dec 27, 2021

r? @oli-obk

@bors
Copy link
Contributor

bors commented Jan 14, 2022

☔ The latest upstream changes (presumably #92844) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Feb 8, 2022

☔ The latest upstream changes (presumably #92007) made this pull request unmergeable. Please resolve the merge conflicts.

@Aaron1011
Copy link
Member Author

@oli-obk I've rebased this now that #92007 has been merged. The PR is pretty much the same - I just changed the new type-op to work with the new opaque_ty_obligation method.

@oli-obk
Copy link
Contributor

oli-obk commented Feb 8, 2022

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 8, 2022
@bors
Copy link
Contributor

bors commented Feb 8, 2022

⌛ Trying commit 880d8e2a06157563f1fe47fb5125a96e7037fced with merge 0fd10de714ecd9c91b80fb63c4fffd13e1f68f56...

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Feb 8, 2022
Currently, any higher-ranked region errors involving opaque types
fall back to a generic "higher-ranked subtype error" message when
run under NLL. This PR adds better error message handling for this
case, giving us the same kinds of error messages that we currently
get without NLL:

```
error: implementation of `MyTrait` is not general enough
  --> $DIR/opaque-hrtb.rs:12:13
   |
LL | fn foo() -> impl for<'a> MyTrait<&'a str> {
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `MyTrait` is not general enough
   |
   = note: `impl MyTrait<&'2 str>` must implement `MyTrait<&'1 str>`, for any lifetime `'1`...
   = note: ...but it actually implements `MyTrait<&'2 str>`, for some specific lifetime `'2`

error: aborting due to previous error
```

To accomplish this, several different refactoring needed to be made:

* We now have a dedicated `InstantiateOpaqueType` struct which
implements `TypeOp`. This is used to invoke `instantiate_opaque_types`
during MIR type checking.
* `TypeOp` is refactored to pass around a `MirBorrowckCtxt`, which is
needed to report opaque type region errors.
* We no longer assume that all `TypeOp`s correspond to canonicalized
queries. This allows us to properly handle opaque type instantiation
(which does not occur in a query) as a `TypeOp`.
A new `ErrorInfo` associated type is used to determine what
additional information is used during higher-ranked region error
handling.
* The body of `try_extract_error_from_fulfill_cx`
has been moved out to a new function `try_extract_error_from_region_constraints`.
This allows us to re-use the same error reporting code between
canonicalized queries (which can extract region constraints directly
from a fresh `InferCtxt`) and opaque type handling (which needs to take
region constraints from the pre-existing `InferCtxt` that we use
throughout MIR borrow checking).
@Aaron1011
Copy link
Member Author

@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Feb 8, 2022

📌 Commit 48a48fd has been approved by oli-obk

@bors
Copy link
Contributor

bors commented Feb 8, 2022

⌛ Testing commit 48a48fd with merge 27ee5ca00e0d78f12757b37a3a8592495491c1f2...

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
test [ui] ui/nll/relate_tys/fn-subtype.rs ... ok
test [ui] ui/nll/relate_tys/hr-fn-aba-as-aaa.rs ... ok
test [ui] ui/nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs ... ok
test [ui] ui/nll/relate_tys/issue-48071.rs ... ok
test [ui] ui/nll/relate_tys/opaque-hrtb.rs ... ok
test [ui] ui/nll/return-ref-mut-issue-46557.rs ... ok
test [ui] ui/nll/ty-outlives/impl-trait-captures.rs ... ok
test [ui] ui/nll/return_from_loop.rs ... ok
test [ui] ui/nll/relate_tys/trait-hrtb.rs ... ok
---

---- [ui] ui/cmse-nonsecure/cmse-nonsecure-call/params-on-stack.rs stdout ----
diff of stderr:

1 error: <unknown>:0:0: in function test i32 (i32, i32, i32, i32, i32): call to non-secure function would require passing arguments on stack
+ thread '<unnamed>' panicked at 'assertion failed: `(left == right)`
+   left: `LLVMing`,
+   left: `LLVMing`,
+  right: `Codegenning`', /checkout/compiler/rustc_codegen_ssa/src/back/write.rs:1470:21
+    0:     0x7feb3677b85c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h10995e31cbc01166
+    1:     0x7feb367e9cce - core::fmt::write::hd7d6faa80ce2553b
+    1:     0x7feb367e9cce - core::fmt::write::hd7d6faa80ce2553b
+    2:     0x7feb3676a771 - std::io::Write::write_fmt::h3f5abf7b9f1eca07
+    3:     0x7feb3677b68b - std::sys_common::backtrace::print::h4d653406e7d402ad
+    4:     0x7feb3677fea4 - std::panicking::default_hook::{{closure}}::h73e6d85c2226b99a
+    5:     0x7feb3677fa86 - std::panicking::default_hook::h578dc98dd31a3735
+    6:     0x7feb378f087a - rustc_driver[52bb940b4ffe0591]::DEFAULT_HOOK::{closure#0}::{closure#0}
+    7:     0x7feb367805c3 - std::panicking::rust_panic_with_hook::hf831cb87f6160c47
+    8:     0x7feb367803d7 - std::panicking::begin_panic_handler::{{closure}}::h3962d95fa5d93909
+    9:     0x7feb3677bd74 - std::sys_common::backtrace::__rust_end_short_backtrace::hbbae4df880a75e04
+   10:     0x7feb36780099 - rust_begin_unwind
+   11:     0x7feb36736a53 - core::panicking::panic_fmt::h2cba5c8061a950b9
+   12:     0x7feb367e6658 - core::panicking::assert_failed_inner::h19ad8376367d71e3
+   13:     0x7feb373fb93b - core[ce52307a21260d31]::panicking::assert_failed::<rustc_codegen_ssa[f59136af200a9e6b]::back::write::MainThreadWorkerState, rustc_codegen_ssa[f59136af200a9e6b]::back::write::MainThreadWorkerState>
+   14:     0x7feb37c7df5e - rustc_codegen_ssa[f59136af200a9e6b]::back::write::start_executing_work::<rustc_codegen_llvm[96c54cfd82f181b2]::LlvmCodegenBackend>::{closure#4}
+   15:     0x7feb37c9a646 - std[962aeb78bac5d12c]::sys_common::backtrace::__rust_begin_short_backtrace::<<rustc_codegen_llvm[96c54cfd82f181b2]::LlvmCodegenBackend as rustc_codegen_ssa[f59136af200a9e6b]::traits::backend::ExtraBackendMethods>::spawn_thread<rustc_codegen_ssa[f59136af200a9e6b]::back::write::start_executing_work<rustc_codegen_llvm[96c54cfd82f181b2]::LlvmCodegenBackend>::{closure#4}, core[ce52307a21260d31]::result::Result<rustc_codegen_ssa[f59136af200a9e6b]::back::write::CompiledModules, ()>>::{closure#0}, core[ce52307a21260d31]::result::Result<rustc_codegen_ssa[f59136af200a9e6b]::back::write::CompiledModules, ()>>
+   16:     0x7feb37c77a39 - <<std[962aeb78bac5d12c]::thread::Builder>::spawn_unchecked_<<rustc_codegen_llvm[96c54cfd82f181b2]::LlvmCodegenBackend as rustc_codegen_ssa[f59136af200a9e6b]::traits::backend::ExtraBackendMethods>::spawn_thread<rustc_codegen_ssa[f59136af200a9e6b]::back::write::start_executing_work<rustc_codegen_llvm[96c54cfd82f181b2]::LlvmCodegenBackend>::{closure#4}, core[ce52307a21260d31]::result::Result<rustc_codegen_ssa[f59136af200a9e6b]::back::write::CompiledModules, ()>>::{closure#0}, core[ce52307a21260d31]::result::Result<rustc_codegen_ssa[f59136af200a9e6b]::back::write::CompiledModules, ()>>::{closure#1} as core[ce52307a21260d31]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
+   17:     0x7feb3678f3a3 - std::sys::unix::thread::Thread::new::thread_start::h631d624327c9afc0
+   18:     0x7feb364b8609 - start_thread
+   19:     0x7feb365fa293 - clone
+   20:                0x0 - <unknown>
+ error: internal compiler error: unexpected panic
+ 
+ note: the compiler unexpectedly panicked. this is a bug.
+ 
+ 
+ note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
+ 
+ note: rustc 1.60.0-nightly (27ee5ca00 2022-02-08) running on x86_64-unknown-linux-gnu
+ 
+ note: compiler flags: -Z threads=1 -C codegen-units=1 -Z ui-testing -Z deduplicate-diagnostics=no -C prefer-dynamic -C rpath -C debuginfo=0 --crate-type lib
+ query stack during panic:
+ end of query stack
3 error: aborting due to previous error
4 
---
To only update this specific test, also pass `--test-args cmse-nonsecure/cmse-nonsecure-call/params-on-stack.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/cmse-nonsecure/cmse-nonsecure-call/params-on-stack.rs" "-Zthreads=1" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/cmse-nonsecure/cmse-nonsecure-call/params-on-stack" "-A" "unused" "-Crpath" "-O" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--target" "thumbv8m.main-none-eabi" "--crate-type" "lib" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/cmse-nonsecure/cmse-nonsecure-call/params-on-stack/auxiliary"
------------------------------------------
Some tests failed in compiletest suite=ui mode=ui host=x86_64-unknown-linux-gnu target=x86_64-unknown-linux-gnu

------------------------------------------
------------------------------------------
stderr:
------------------------------------------
error: <unknown>:0:0: in function test i32 (i32, i32, i32, i32, i32): call to non-secure function would require passing arguments on stack
thread '<unnamed>' panicked at 'assertion failed: `(left == right)`
  left: `LLVMing`,
  left: `LLVMing`,
 right: `Codegenning`', /checkout/compiler/rustc_codegen_ssa/src/back/write.rs:1470:21
   0:     0x7feb3677b85c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h10995e31cbc01166
   1:     0x7feb367e9cce - core::fmt::write::hd7d6faa80ce2553b
   1:     0x7feb367e9cce - core::fmt::write::hd7d6faa80ce2553b
   2:     0x7feb3676a771 - std::io::Write::write_fmt::h3f5abf7b9f1eca07
   3:     0x7feb3677b68b - std::sys_common::backtrace::print::h4d653406e7d402ad
   4:     0x7feb3677fea4 - std::panicking::default_hook::{{closure}}::h73e6d85c2226b99a
   5:     0x7feb3677fa86 - std::panicking::default_hook::h578dc98dd31a3735
   6:     0x7feb378f087a - rustc_driver[52bb940b4ffe0591]::DEFAULT_HOOK::{closure#0}::{closure#0}
   7:     0x7feb367805c3 - std::panicking::rust_panic_with_hook::hf831cb87f6160c47
   8:     0x7feb367803d7 - std::panicking::begin_panic_handler::{{closure}}::h3962d95fa5d93909
   9:     0x7feb3677bd74 - std::sys_common::backtrace::__rust_end_short_backtrace::hbbae4df880a75e04
  10:     0x7feb36780099 - rust_begin_unwind
  11:     0x7feb36736a53 - core::panicking::panic_fmt::h2cba5c8061a950b9
  12:     0x7feb367e6658 - core::panicking::assert_failed_inner::h19ad8376367d71e3
  13:     0x7feb373fb93b - core[ce52307a21260d31]::panicking::assert_failed::<rustc_codegen_ssa[f59136af200a9e6b]::back::write::MainThreadWorkerState, rustc_codegen_ssa[f59136af200a9e6b]::back::write::MainThreadWorkerState>
  14:     0x7feb37c7df5e - rustc_codegen_ssa[f59136af200a9e6b]::back::write::start_executing_work::<rustc_codegen_llvm[96c54cfd82f181b2]::LlvmCodegenBackend>::{closure#4}
  15:     0x7feb37c9a646 - std[962aeb78bac5d12c]::sys_common::backtrace::__rust_begin_short_backtrace::<<rustc_codegen_llvm[96c54cfd82f181b2]::LlvmCodegenBackend as rustc_codegen_ssa[f59136af200a9e6b]::traits::backend::ExtraBackendMethods>::spawn_thread<rustc_codegen_ssa[f59136af200a9e6b]::back::write::start_executing_work<rustc_codegen_llvm[96c54cfd82f181b2]::LlvmCodegenBackend>::{closure#4}, core[ce52307a21260d31]::result::Result<rustc_codegen_ssa[f59136af200a9e6b]::back::write::CompiledModules, ()>>::{closure#0}, core[ce52307a21260d31]::result::Result<rustc_codegen_ssa[f59136af200a9e6b]::back::write::CompiledModules, ()>>
  16:     0x7feb37c77a39 - <<std[962aeb78bac5d12c]::thread::Builder>::spawn_unchecked_<<rustc_codegen_llvm[96c54cfd82f181b2]::LlvmCodegenBackend as rustc_codegen_ssa[f59136af200a9e6b]::traits::backend::ExtraBackendMethods>::spawn_thread<rustc_codegen_ssa[f59136af200a9e6b]::back::write::start_executing_work<rustc_codegen_llvm[96c54cfd82f181b2]::LlvmCodegenBackend>::{closure#4}, core[ce52307a21260d31]::result::Result<rustc_codegen_ssa[f59136af200a9e6b]::back::write::CompiledModules, ()>>::{closure#0}, core[ce52307a21260d31]::result::Result<rustc_codegen_ssa[f59136af200a9e6b]::back::write::CompiledModules, ()>>::{closure#1} as core[ce52307a21260d31]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  17:     0x7feb3678f3a3 - std::sys::unix::thread::Thread::new::thread_start::h631d624327c9afc0
  18:     0x7feb364b8609 - start_thread
  19:     0x7feb365fa293 - clone
  20:                0x0 - <unknown>
error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.


note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.60.0-nightly (27ee5ca00 2022-02-08) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z threads=1 -C codegen-units=1 -Z ui-testing -Z deduplicate-diagnostics=no -C prefer-dynamic -C rpath -C debuginfo=0 --crate-type lib
query stack during panic:
end of query stack
error: aborting due to previous error

@bors
Copy link
Contributor

bors commented Feb 8, 2022

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 8, 2022
@Aaron1011
Copy link
Member Author

Aaron1011 commented Feb 8, 2022

@bors retry hit spurious failure #87083

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 8, 2022
@bors
Copy link
Contributor

bors commented Feb 9, 2022

⌛ Testing commit 48a48fd with merge 1f0a968...

@bors
Copy link
Contributor

bors commented Feb 9, 2022

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing 1f0a968 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 9, 2022
@bors bors merged commit 1f0a968 into rust-lang:master Feb 9, 2022
@rustbot rustbot added this to the 1.60.0 milestone Feb 9, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1f0a968): comparison url.

Summary: This benchmark run did not return any relevant results.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

oli-obk added a commit to oli-obk/rust that referenced this pull request Feb 11, 2022
…li-obk"

This reverts commit 1f0a968, reversing
changes made to bf242bb.
@oli-obk oli-obk mentioned this pull request Feb 11, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 11, 2022
Revert lazy TAIT PR

Revert rust-lang#92306 (sorry `@Aaron1011,` will include your changes in the fix PR)
Revert rust-lang#93783
Revert rust-lang#92007

fixes rust-lang#93788
fixes rust-lang#93794
fixes rust-lang#93821
fixes rust-lang#93831
fixes rust-lang#93841
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Feb 24, 2022
Revert lazy TAIT PR

Revert rust-lang/rust#92306 (sorry `@Aaron1011,` will include your changes in the fix PR)
Revert rust-lang/rust#93783
Revert rust-lang/rust#92007

fixes rust-lang/rust#93788
fixes rust-lang/rust#93794
fixes rust-lang/rust#93821
fixes rust-lang/rust#93831
fixes rust-lang/rust#93841
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 30, 2022
…sakis

Lazy type-alias-impl-trait take two

### user visible change 1: RPIT inference from recursive call sites

Lazy TAIT has an insta-stable change. The following snippet now compiles, because opaque types can now have their hidden type set from wherever the opaque type is mentioned.

```rust
fn bar(b: bool) -> impl std::fmt::Debug {
    if b {
        return 42
    }
    let x: u32 = bar(false); // this errors on stable
    99
}
```

The return type of `bar` stays opaque, you can't do `bar(false) + 42`, you need to actually mention the hidden type.

### user visible change 2: divergence between RPIT and TAIT in return statements

Note that `return` statements and the trailing return expression are special with RPIT (but not TAIT). So

```rust
#![feature(type_alias_impl_trait)]
type Foo = impl std::fmt::Debug;

fn foo(b: bool) -> Foo {
    if b {
        return vec![42];
    }
    std::iter::empty().collect() //~ ERROR `Foo` cannot be built from an iterator
}

fn bar(b: bool) -> impl std::fmt::Debug {
    if b {
        return vec![42]
    }
    std::iter::empty().collect() // Works, magic (accidentally stabilized, not intended)
}
```

But when we are working with the return value of a recursive call, the behavior of RPIT and TAIT is the same:

```rust
type Foo = impl std::fmt::Debug;

fn foo(b: bool) -> Foo {
    if b {
        return vec![];
    }
    let mut x = foo(false);
    x = std::iter::empty().collect(); //~ ERROR `Foo` cannot be built from an iterator
    vec![]
}

fn bar(b: bool) -> impl std::fmt::Debug {
    if b {
        return vec![];
    }
    let mut x = bar(false);
    x = std::iter::empty().collect(); //~ ERROR `impl Debug` cannot be built from an iterator
    vec![]
}
```

### user visible change 3: TAIT does not merge types across branches

In contrast to RPIT, TAIT does not merge types across branches, so the following does not compile.

```rust
type Foo = impl std::fmt::Debug;

fn foo(b: bool) -> Foo {
    if b {
        vec![42_i32]
    } else {
        std::iter::empty().collect()
        //~^ ERROR `Foo` cannot be built from an iterator over elements of type `_`
    }
}
```

It is easy to support, but we should make an explicit decision to include the additional complexity in the implementation (it's not much, see a721052457cf513487fb4266e3ade65c29b272d2 which needs to be reverted to enable this).

### PR formalities

previous attempt: rust-lang#92007

This PR also includes rust-lang#92306 and rust-lang#93783, as they were reverted along with rust-lang#92007 in rust-lang#93893

fixes rust-lang#93411
fixes rust-lang#88236
fixes rust-lang#89312
fixes rust-lang#87340
fixes rust-lang#86800
fixes rust-lang#86719
fixes rust-lang#84073
fixes rust-lang#83919
fixes rust-lang#82139
fixes rust-lang#77987
fixes rust-lang#74282
fixes rust-lang#67830
fixes rust-lang#62742
fixes rust-lang#54895
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants