Commit fa7e4bb
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 code221 files changed
Lines changed: 4029 additions & 4267 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
- 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 | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | 9 | | |
11 | | - | |
| 10 | + | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
| |||
358 | 357 | | |
359 | 358 | | |
360 | 359 | | |
361 | | - | |
| 360 | + | |
362 | 361 | | |
363 | 362 | | |
| 363 | + | |
364 | 364 | | |
365 | 365 | | |
366 | 366 | | |
| |||
374 | 374 | | |
375 | 375 | | |
376 | 376 | | |
377 | | - | |
| 377 | + | |
378 | 378 | | |
379 | 379 | | |
380 | 380 | | |
| |||
815 | 815 | | |
816 | 816 | | |
817 | 817 | | |
| 818 | + | |
818 | 819 | | |
819 | 820 | | |
820 | 821 | | |
821 | 822 | | |
822 | 823 | | |
823 | 824 | | |
824 | | - | |
| 825 | + | |
825 | 826 | | |
826 | 827 | | |
827 | 828 | | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
828 | 836 | | |
829 | 837 | | |
830 | 838 | | |
831 | 839 | | |
832 | 840 | | |
833 | | - | |
834 | | - | |
835 | | - | |
836 | | - | |
837 | | - | |
838 | 841 | | |
839 | 842 | | |
840 | 843 | | |
| |||
850 | 853 | | |
851 | 854 | | |
852 | 855 | | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
| 856 | + | |
859 | 857 | | |
860 | 858 | | |
861 | 859 | | |
862 | 860 | | |
863 | 861 | | |
864 | 862 | | |
865 | 863 | | |
866 | | - | |
867 | | - | |
| 864 | + | |
868 | 865 | | |
869 | 866 | | |
870 | 867 | | |
| |||
886 | 883 | | |
887 | 884 | | |
888 | 885 | | |
889 | | - | |
| 886 | + | |
890 | 887 | | |
891 | 888 | | |
892 | 889 | | |
| |||
898 | 895 | | |
899 | 896 | | |
900 | 897 | | |
901 | | - | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
902 | 911 | | |
903 | 912 | | |
904 | 913 | | |
| |||
951 | 960 | | |
952 | 961 | | |
953 | 962 | | |
| 963 | + | |
954 | 964 | | |
955 | | - | |
| 965 | + | |
956 | 966 | | |
957 | 967 | | |
958 | 968 | | |
959 | 969 | | |
960 | 970 | | |
961 | 971 | | |
| 972 | + | |
| 973 | + | |
962 | 974 | | |
963 | 975 | | |
964 | 976 | | |
965 | | - | |
966 | | - | |
967 | 977 | | |
968 | 978 | | |
969 | 979 | | |
| |||
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 | | |
991 | 1001 | | |
992 | 1002 | | |
| |||
1645 | 1655 | | |
1646 | 1656 | | |
1647 | 1657 | | |
1648 | | - | |
1649 | | - | |
1650 | | - | |
1651 | | - | |
1652 | | - | |
1653 | | - | |
| 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 | + | |
| 1698 | + | |
| 1699 | + | |
1654 | 1700 | | |
1655 | 1701 | | |
1656 | 1702 | | |
1657 | 1703 | | |
1658 | 1704 | | |
1659 | | - | |
| 1705 | + | |
1660 | 1706 | | |
1661 | 1707 | | |
1662 | 1708 | | |
| |||
1667 | 1713 | | |
1668 | 1714 | | |
1669 | 1715 | | |
1670 | | - | |
1671 | | - | |
1672 | | - | |
1673 | | - | |
1674 | | - | |
1675 | | - | |
1676 | | - | |
1677 | | - | |
1678 | | - | |
| 1716 | + | |
1679 | 1717 | | |
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 | | - | |
1708 | | - | |
1709 | 1718 | | |
1710 | 1719 | | |
1711 | 1720 | | |
| |||
1800 | 1809 | | |
1801 | 1810 | | |
1802 | 1811 | | |
1803 | | - | |
| 1812 | + | |
| 1813 | + | |
| 1814 | + | |
| 1815 | + | |
| 1816 | + | |
| 1817 | + | |
1804 | 1818 | | |
1805 | 1819 | | |
1806 | 1820 | | |
| |||
| 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 | |
|---|---|---|---|
| |||
1450 | 1450 | | |
1451 | 1451 | | |
1452 | 1452 | | |
1453 | | - | |
1454 | | - | |
1455 | | - | |
1456 | | - | |
1457 | | - | |
1458 | | - | |
1459 | | - | |
1460 | | - | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
1461 | 1462 | | |
1462 | 1463 | | |
1463 | | - | |
1464 | | - | |
| 1464 | + | |
1465 | 1465 | | |
1466 | 1466 | | |
1467 | 1467 | | |
| |||
1479 | 1479 | | |
1480 | 1480 | | |
1481 | 1481 | | |
1482 | | - | |
| 1482 | + | |
1483 | 1483 | | |
1484 | 1484 | | |
1485 | 1485 | | |
| |||
1643 | 1643 | | |
1644 | 1644 | | |
1645 | 1645 | | |
| 1646 | + | |
1646 | 1647 | | |
1647 | 1648 | | |
1648 | 1649 | | |
| |||
1651 | 1652 | | |
1652 | 1653 | | |
1653 | 1654 | | |
| 1655 | + | |
1654 | 1656 | | |
1655 | 1657 | | |
1656 | 1658 | | |
| |||
1659 | 1661 | | |
1660 | 1662 | | |
1661 | 1663 | | |
1662 | | - | |
1663 | | - | |
1664 | | - | |
1665 | | - | |
1666 | | - | |
1667 | | - | |
1668 | | - | |
| 1664 | + | |
1669 | 1665 | | |
1670 | 1666 | | |
1671 | 1667 | | |
| |||
0 commit comments