Commit 9d2911f
committed
Auto merge of #157309 - cjgillot:coroutine-hir-desugar, r=oli-obk
Desugar async blocks in HIR instead of MIR
Implements MCP rust-lang/compiler-team#997
Based on #157166
In the current implementation, `gen`/`async`/`async gen` blocks and closures have type `Coroutine(..)` and `CoroutineClosure(..)`. Those types implement `Iterator`, `Future` or `AsyncIterator` depending on the initial desugaring.
This creates a lot of complexity:
- trait solvers must check which kind of coroutine each time;
- MIR StateTransform needs to fixup types depending on the coroutine kind.
I propose to change the desugaring for coroutines to:
- `gen { .. }` becomes `CoroutineIterator::from_coroutine(#[coroutine] { .. })`;
- `async { .. }` becomes `CoroutineFuture::from_coroutine(#[coroutine] { .. })`;
- `async gen { .. }` becomes `CoroutineAsyncIterator::from_coroutine(#[coroutine] { .. })`.
This way, all coroutines implement `std::ops::Coroutine` and `core` is responsible for translating this to user-friendly traits. All the complexity is pushed to error-reporting code, which is not soundness-critical.
Coroutine closures are a little more complex, as we need to keep the `CoroutineClosure` type for borrow-checking.
Main design point: I create two methods on `TyCtxt` that are meant to do the back-and-forth between wrapped and unwrapped coroutines. `coroutine_desugared_type` wraps a coroutine inside the adapter struct. `try_unwrap_desugared_coroutine` unwraps it.
r? @oli-obk
cc @lcnr @RalfJung
cc @estebank as I modify quite a lot of diagnostic code217 files changed
Lines changed: 4023 additions & 4258 deletions
File tree
- compiler
- rustc_ast_lowering/src
- expr
- rustc_borrowck/src
- diagnostics
- region_infer
- type_check
- rustc_hir_analysis/src
- rustc_hir_typeck/src
- fn_ctxt
- rustc_hir/src
- rustc_lint/src
- unused
- rustc_middle/src/ty
- context
- print
- rustc_mir_transform/src
- coroutine
- shim
- rustc_next_trait_solver/src/solve
- assembly
- normalizes_to
- rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi
- rustc_span/src
- rustc_trait_selection/src
- error_reporting
- infer
- traits
- traits
- select
- rustc_ty_utils/src
- rustc_type_ir/src
- library/core/src
- async_iter
- future
- ops
- coroutine
- src/tools/clippy
- clippy_lints/src
- doc
- utils
- clippy_utils/src
- tests/ui
- author
- tests
- codegen-llvm
- coverage
- mir-opt
- coroutine
- inline
- ui-fulldeps/rustc_public
- ui
- async-await
- async-closures
- future-sizes
- multiple-lifetimes
- attributes
- cmse-nonsecure/cmse-nonsecure-entry
- const-generics/generic_const_exprs
- consts
- min_const_fn
- coroutine
- impl-trait/issues
- lifetimes
- pattern
- print_type_sizes
- rustc_public-ir-print
- suggestions
- traits
- next-solver
- non_lifetime_binders
- type-alias-impl-trait
- unpretty
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | 10 | | |
12 | | - | |
| 11 | + | |
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
| |||
359 | 358 | | |
360 | 359 | | |
361 | 360 | | |
362 | | - | |
| 361 | + | |
363 | 362 | | |
364 | 363 | | |
| 364 | + | |
365 | 365 | | |
366 | 366 | | |
367 | 367 | | |
| |||
375 | 375 | | |
376 | 376 | | |
377 | 377 | | |
378 | | - | |
| 378 | + | |
379 | 379 | | |
380 | 380 | | |
381 | 381 | | |
| |||
813 | 813 | | |
814 | 814 | | |
815 | 815 | | |
| 816 | + | |
816 | 817 | | |
817 | 818 | | |
818 | 819 | | |
819 | 820 | | |
820 | 821 | | |
821 | 822 | | |
822 | | - | |
| 823 | + | |
823 | 824 | | |
824 | 825 | | |
825 | 826 | | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
826 | 834 | | |
827 | 835 | | |
828 | 836 | | |
829 | 837 | | |
830 | 838 | | |
831 | | - | |
832 | | - | |
833 | | - | |
834 | | - | |
835 | | - | |
836 | 839 | | |
837 | 840 | | |
838 | 841 | | |
| |||
848 | 851 | | |
849 | 852 | | |
850 | 853 | | |
851 | | - | |
852 | | - | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
| 854 | + | |
857 | 855 | | |
858 | 856 | | |
859 | 857 | | |
860 | 858 | | |
861 | 859 | | |
862 | 860 | | |
863 | 861 | | |
864 | | - | |
865 | | - | |
| 862 | + | |
866 | 863 | | |
867 | 864 | | |
868 | 865 | | |
| |||
884 | 881 | | |
885 | 882 | | |
886 | 883 | | |
887 | | - | |
| 884 | + | |
888 | 885 | | |
889 | 886 | | |
890 | 887 | | |
| |||
896 | 893 | | |
897 | 894 | | |
898 | 895 | | |
899 | | - | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
900 | 909 | | |
901 | 910 | | |
902 | 911 | | |
| |||
949 | 958 | | |
950 | 959 | | |
951 | 960 | | |
| 961 | + | |
952 | 962 | | |
953 | | - | |
| 963 | + | |
954 | 964 | | |
955 | 965 | | |
956 | 966 | | |
957 | 967 | | |
958 | 968 | | |
959 | 969 | | |
| 970 | + | |
| 971 | + | |
960 | 972 | | |
961 | 973 | | |
962 | 974 | | |
963 | | - | |
964 | | - | |
965 | 975 | | |
966 | 976 | | |
967 | 977 | | |
| |||
972 | 982 | | |
973 | 983 | | |
974 | 984 | | |
975 | | - | |
| 985 | + | |
976 | 986 | | |
977 | 987 | | |
978 | 988 | | |
979 | 989 | | |
980 | 990 | | |
981 | 991 | | |
982 | 992 | | |
983 | | - | |
| 993 | + | |
984 | 994 | | |
985 | 995 | | |
986 | 996 | | |
987 | | - | |
| 997 | + | |
988 | 998 | | |
989 | 999 | | |
990 | 1000 | | |
| |||
1643 | 1653 | | |
1644 | 1654 | | |
1645 | 1655 | | |
1646 | | - | |
1647 | | - | |
1648 | | - | |
1649 | | - | |
1650 | | - | |
1651 | | - | |
| 1656 | + | |
| 1657 | + | |
| 1658 | + | |
| 1659 | + | |
| 1660 | + | |
| 1661 | + | |
| 1662 | + | |
| 1663 | + | |
| 1664 | + | |
| 1665 | + | |
| 1666 | + | |
| 1667 | + | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
| 1683 | + | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
| 1694 | + | |
| 1695 | + | |
| 1696 | + | |
| 1697 | + | |
1652 | 1698 | | |
1653 | 1699 | | |
1654 | 1700 | | |
1655 | 1701 | | |
1656 | 1702 | | |
1657 | | - | |
| 1703 | + | |
1658 | 1704 | | |
1659 | 1705 | | |
1660 | 1706 | | |
| |||
1665 | 1711 | | |
1666 | 1712 | | |
1667 | 1713 | | |
1668 | | - | |
1669 | | - | |
1670 | | - | |
1671 | | - | |
1672 | | - | |
1673 | | - | |
1674 | | - | |
1675 | | - | |
1676 | | - | |
| 1714 | + | |
1677 | 1715 | | |
1678 | | - | |
1679 | | - | |
1680 | | - | |
1681 | | - | |
1682 | | - | |
1683 | | - | |
1684 | | - | |
1685 | | - | |
1686 | | - | |
1687 | | - | |
1688 | | - | |
1689 | | - | |
1690 | | - | |
1691 | | - | |
1692 | | - | |
1693 | | - | |
1694 | | - | |
1695 | | - | |
1696 | | - | |
1697 | | - | |
1698 | | - | |
1699 | | - | |
1700 | | - | |
1701 | | - | |
1702 | | - | |
1703 | | - | |
1704 | | - | |
1705 | | - | |
1706 | | - | |
1707 | 1716 | | |
1708 | 1717 | | |
1709 | 1718 | | |
| |||
1798 | 1807 | | |
1799 | 1808 | | |
1800 | 1809 | | |
1801 | | - | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
| 1813 | + | |
| 1814 | + | |
| 1815 | + | |
1802 | 1816 | | |
1803 | 1817 | | |
1804 | 1818 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
327 | 327 | | |
328 | 328 | | |
329 | 329 | | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
340 | 343 | | |
341 | | - | |
| 344 | + | |
342 | 345 | | |
343 | 346 | | |
344 | 347 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1504 | 1504 | | |
1505 | 1505 | | |
1506 | 1506 | | |
1507 | | - | |
1508 | | - | |
1509 | | - | |
1510 | | - | |
1511 | | - | |
1512 | | - | |
1513 | | - | |
1514 | | - | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
1515 | 1516 | | |
1516 | 1517 | | |
1517 | | - | |
1518 | | - | |
| 1518 | + | |
1519 | 1519 | | |
1520 | 1520 | | |
1521 | 1521 | | |
| |||
1533 | 1533 | | |
1534 | 1534 | | |
1535 | 1535 | | |
1536 | | - | |
| 1536 | + | |
1537 | 1537 | | |
1538 | 1538 | | |
1539 | 1539 | | |
| |||
1697 | 1697 | | |
1698 | 1698 | | |
1699 | 1699 | | |
| 1700 | + | |
1700 | 1701 | | |
1701 | 1702 | | |
1702 | 1703 | | |
| |||
1705 | 1706 | | |
1706 | 1707 | | |
1707 | 1708 | | |
| 1709 | + | |
1708 | 1710 | | |
1709 | 1711 | | |
1710 | 1712 | | |
| |||
1713 | 1715 | | |
1714 | 1716 | | |
1715 | 1717 | | |
1716 | | - | |
1717 | | - | |
1718 | | - | |
1719 | | - | |
1720 | | - | |
1721 | | - | |
1722 | | - | |
| 1718 | + | |
1723 | 1719 | | |
1724 | 1720 | | |
1725 | 1721 | | |
| |||
0 commit comments