rust/compiler
许杰友 Jieyou Xu (Joe) 2ebf40719a
Rollup merge of #136824 - lcnr:yeet, r=compiler-errors
solver cycles are coinductive once they have one coinductive step

Implements the new cycle semantics in the new solver, dealing with the fallout from https://github.com/rust-lang/trait-system-refactor-initiative/issues/10.

The first commit has been extensively fuzzed via https://github.com/lcnr/search_graph_fuzz.

A trait solver cycle is now coinductive if it has at least one *coinductive step*. A step is only considered coinductive if it's a where-clause of an impl of a coinductive trait. The only coinductive traits are `Sized` and auto traits.

This differs from the current stable because where a cycle had to consist of exclusively coinductive goals. This is overly limiting and wasn't properly enforced as it (mostly) ignored all non-trait goals.

A more in-depth explanation of my reasoning can be found in this separate doc: https://gist.github.com/lcnr/c49d887bbd34f5d05c36d1cf7a1bf5a5. A summary:
- imagine using dictionary passing style: map where-bounds to additional "dictonary" fn arguments instead of monomorphization
- impls are the only source of truth and introduce a *constructor* of the dictionary type
- a trait goal holds if mapping its proof tree to dictionary passing style results in a valid corecursive function
- a corecursive function is valid if it is guarded: matching on it should result in a constructor in a finite amount of time. This property should recursively hold for all fields of the constructor
    - a function is guarded if the recursive call is *behind* a constructor
    - **and** this constructor is not *moved out of*, e.g. by accessing a field of the dictionary
- the "not moved out of" condition is difficult to guarantee in general, e.g. for item bounds of associated types. However, there is no way to *move out* of an auto trait as there is no information you can get from *the inside of* an auto trait bound in the trait system
- if we encounter a cycle/recursive call which involves an auto trait, we can always convert the proof tree into a non-recursive function which calls a corecursive function whose first step is the construction of the auto trait dict and which only recursively depends on itself (by inlining the original function until they reach the uses of the auto trait)

**we can therefore make any cycle during which we step into an auto trait (or `Sized`) impl coinductive**

----

To fix https://github.com/rust-lang/trait-system-refactor-initiative/issues/10 we could go with a more restrictive version which tries to restrict cycles to only allow code already supported on stable, potentially forcing cycles to be ambiguous if they step through an impl-where clause of a non-coinductive trait.

`PathKind` should be a strictly ordered set to allow merging paths without worry. We could therefore add another variant `PathKind::ForceUnknown` which is greater than `PathKind::Coinductive`. We already have to add such a third `PathKind` in #137314 anyways.

I am not doing this here due to multiple reasons:
- I cannot think of a principled reason why cycles using an impl to normalize differ in any way from simply using that impl to prove a trait bound. It feels unnecessary and like it makes it more difficult to reason about our cycle semantics :<
- This PR does not affect stable as coherence doesn't care about whether a goal holds or is ambiguous. So we don't yet have to make a final decision

r? `@compiler-errors` `@nikomatsakis`
2025-02-28 21:41:59 +08:00
..
rustc Fix overcapturing, unsafe extern blocks, and new unsafe ops 2025-02-22 00:01:48 +00:00
rustc_abi Rollup merge of #137334 - compiler-errors:edition-2024-fresh-2, r=saethlin,traviscross 2025-02-23 02:44:18 -05:00
rustc_arena Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_ast Rename AssocOp::As as AssocOp::Cast. 2025-02-27 09:53:18 +11:00
rustc_ast_ir Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_ast_lowering lower attr spans and inline some functions to hopefully mitigate perf regressions 2025-02-24 14:37:58 +01:00
rustc_ast_passes Teach structured errors to display short Ty 2025-02-25 16:56:03 +00:00
rustc_ast_pretty Introduce AssocOp::Binary. 2025-02-27 09:53:17 +11:00
rustc_attr_data_structures Spruce up AttributeKind docs 2025-02-26 22:21:36 +00:00
rustc_attr_parsing fix #137589 2025-02-25 18:17:32 +01:00
rustc_baked_icu_data Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_borrowck Rollup merge of #137201 - estebank:structured-errors-long-ty, r=oli-obk 2025-02-26 19:03:55 +01:00
rustc_builtin_macros Introduce new-style attribute parsers for several attributes 2025-02-24 14:31:17 +01:00
rustc_codegen_cranelift Auto merge of #137608 - fmease:rollup-h4siso6, r=fmease 2025-02-25 19:36:17 +00:00
rustc_codegen_gcc Rollup merge of #137595 - folkertdev:remove-simd-pow-powi, r=RalfJung 2025-02-25 13:07:40 +01:00
rustc_codegen_llvm Rollup merge of #137603 - davidtwco:extern-types-no-deref, r=lcnr 2025-02-26 04:15:06 +01:00
rustc_codegen_ssa Rollup merge of #137201 - estebank:structured-errors-long-ty, r=oli-obk 2025-02-26 19:03:55 +01:00
rustc_const_eval require trait impls to have matching const stabilities as the traits 2025-02-27 04:56:27 +00:00
rustc_data_structures Rollup merge of #136579 - bjorn3:fix_thinvec_ext_ub, r=BoxyUwU 2025-02-27 08:56:36 +01:00
rustc_driver Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_driver_impl Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_error_codes Rollup merge of #137489 - RalfJung:no-more-rustc_intrinsic_must_be_overridden, r=oli-obk 2025-02-24 19:21:47 -05:00
rustc_error_messages Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_errors Fix rebase 2025-02-25 17:27:22 +00:00
rustc_expand Introduce new-style attribute parsers for several attributes 2025-02-24 14:31:17 +01:00
rustc_feature Rollup merge of #137489 - RalfJung:no-more-rustc_intrinsic_must_be_overridden, r=oli-obk 2025-02-24 19:21:47 -05:00
rustc_fluent_macro Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_fs_util Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_graphviz Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_hashes Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_hir Introduce AssocOp::Binary. 2025-02-27 09:53:17 +11:00
rustc_hir_analysis Rollup merge of #137631 - TaKO8Ki:issue-137508, r=compiler-errors 2025-02-26 19:03:56 +01:00
rustc_hir_pretty Introduce AssocOp::Binary. 2025-02-27 09:53:17 +11:00
rustc_hir_typeck Rollup merge of #137201 - estebank:structured-errors-long-ty, r=oli-obk 2025-02-26 19:03:55 +01:00
rustc_incremental Change span field accesses to method calls 2025-02-24 14:22:31 +01:00
rustc_index Rollup merge of #136610 - Jarcho:range_idx, r=Noratrieb 2025-02-24 02:11:32 -05:00
rustc_index_macros Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_infer Rollup merge of #136610 - Jarcho:range_idx, r=Noratrieb 2025-02-24 02:11:32 -05:00
rustc_interface Auto merge of #137420 - matthiaskrgr:rollup-rr0q37f, r=matthiaskrgr 2025-02-22 13:32:44 +00:00
rustc_lexer Greatly simplify lifetime captures in edition 2024 2025-02-22 22:24:52 +00:00
rustc_lint Rollup merge of #136424 - 11happy:overflow.hex.fix, r=fmease 2025-02-28 21:41:58 +08:00
rustc_lint_defs Introduce new-style attribute parsers for several attributes 2025-02-24 14:31:17 +01:00
rustc_llvm Auto merge of #137271 - nikic:gep-nuw-2, r=scottmcm 2025-02-24 03:06:16 +00:00
rustc_log Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_macros pretty print hir attributes 2025-02-24 14:31:19 +01:00
rustc_metadata Rollup merge of #137201 - estebank:structured-errors-long-ty, r=oli-obk 2025-02-26 19:03:55 +01:00
rustc_middle review 2025-02-28 12:16:48 +01:00
rustc_mir_build Teach structured errors to display short Ty 2025-02-25 16:56:03 +00:00
rustc_mir_dataflow Greatly simplify lifetime captures in edition 2024 2025-02-22 22:24:52 +00:00
rustc_mir_transform Print out destructor 2025-02-26 19:03:29 +00:00
rustc_monomorphize Rollup merge of #137601 - davidtwco:deduplicate-type-has-metadata, r=fmease,bjorn3 2025-02-26 04:15:05 +01:00
rustc_next_trait_solver review 2025-02-28 12:16:48 +01:00
rustc_parse Rollup merge of #136846 - nnethercote:make-AssocOp-more-like-ExprKind, r=spastorino 2025-02-27 08:56:37 +01:00
rustc_parse_format Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_passes require trait impls to have matching const stabilities as the traits 2025-02-27 04:56:27 +00:00
rustc_pattern_analysis Greatly simplify lifetime captures in edition 2024 2025-02-22 22:24:52 +00:00
rustc_privacy Introduce new-style attribute parsers for several attributes 2025-02-24 14:31:17 +01:00
rustc_query_impl Introduce new parsing infrastructure and types for parsed attributes 2025-02-24 14:26:06 +01:00
rustc_query_system Introduce new parsing infrastructure and types for parsed attributes 2025-02-24 14:26:06 +01:00
rustc_resolve Rollup merge of #137201 - estebank:structured-errors-long-ty, r=oli-obk 2025-02-26 19:03:55 +01:00
rustc_sanitizers Introduce new-style attribute parsers for several attributes 2025-02-24 14:31:17 +01:00
rustc_serialize rename sub_ptr 😅 2025-02-23 23:11:00 +07:00
rustc_session Teach structured errors to display short Ty 2025-02-25 16:56:03 +00:00
rustc_smir change smir attributes getters to only support tool attributes 2025-02-24 14:31:19 +01:00
rustc_span Auto merge of #137608 - fmease:rollup-h4siso6, r=fmease 2025-02-25 19:36:17 +00:00
rustc_symbol_mangling Introduce new-style attribute parsers for several attributes 2025-02-24 14:31:17 +01:00
rustc_target Rollup merge of #137370 - RalfJung:x86-abi-fallback, r=SparrowLii 2025-02-25 13:07:23 +01:00
rustc_trait_selection review 2025-02-28 12:16:48 +01:00
rustc_traits Rollup merge of #137333 - compiler-errors:edition-2024-fresh, r=Nadrieril 2025-02-22 11:36:43 +01:00
rustc_transmute Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
rustc_ty_utils Don't infer unwinding of virtual calls based on the function attributes 2025-02-27 12:58:18 +08:00
rustc_type_ir review 2025-02-28 12:16:48 +01:00
rustc_type_ir_macros Upgrade the compiler to edition 2024 2025-02-22 00:01:48 +00:00
stable_mir change smir attributes getters to only support tool attributes 2025-02-24 14:31:19 +01:00