rust/compiler
bors d4d129d566 Auto merge of #85012 - FabianWolff:struct-rec, r=davidtwco
Fix stack overflow when checking for structural recursion

This pull request aims to fix #74224 and fix #84611. The current logic for detecting ADTs with structural recursion is flawed because it only looks at the root type, and then for exact matches. What I mean by this is that for examples such as:
```rust
struct A<T> {
    x: T,
    y: A<A<T>>,
}

struct B {
    z: A<usize>
}

fn main() {}
```
When checking `A`, the compiler correctly determines that it has an infinite size (because the "root" type is `A`, and `A` occurs, albeit with different type arguments, as a nested type in `A`).

However, when checking `B`, it also recurses into `A`, but now `B` is the root type, and it only checks for _exact_ matches of `A`, but since `A` never precisely contains itself (only `A<A<T>>`, `A<A<A<T>>>`, etc.), an endless recursion ensues until the stack overflows.

In this PR, I have attempted to fix this behavior by implementing a two-phase checking: When checking `B`, my code first checks `A` _separately_ and stops if `A` already turns out to be infinite. If not (such as for `Option<T>`), the second phase checks whether the root type (`B`) is ever nested inside itself, e.g.:
```rust
struct Foo { x: Option<Option<Foo>> }
```

Special care needs to be taken for mutually recursive types, e.g.:
```rust
struct A<T> {
    z: T,
    x: B<T>,
}

struct B<T> {
    y: A<T>
}
```
Here, both `A` and `B` both _are_ `SelfRecursive` and _contain_ a recursive type. The current behavior, which I have maintained, is to treat both `A` and `B` as `SelfRecursive`, and accordingly report errors for both.
2021-05-11 00:00:53 +00:00
..
rustc use jemallocator in rustc/rustdoc 2021-04-04 14:10:26 -07:00
rustc_apfloat Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
rustc_arena Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
rustc_ast Auto merge of #83386 - mark-i-m:stabilize-pat2015, r=nikomatsakis 2021-04-28 20:35:17 +00:00
rustc_ast_lowering Make Diagnostic::span_fatal unconditionally raise an error 2021-05-08 23:12:04 -04:00
rustc_ast_passes Implement RFC 2951: Native link modifiers 2021-05-05 16:04:25 -07:00
rustc_ast_pretty parser: Remove support for inner attributes on non-block expressions 2021-05-03 13:33:53 +03:00
rustc_attr Allow specifying alignment for functions 2021-04-05 17:36:51 -04:00
rustc_builtin_macros Disallows #![feature(no_coverage)] on stable and beta 2021-05-05 07:52:26 -07:00
rustc_codegen_cranelift Implement RFC 2951: Native link modifiers 2021-05-05 16:04:25 -07:00
rustc_codegen_llvm Support -C passes in NewPM 2021-05-08 10:58:08 +02:00
rustc_codegen_ssa Adjust target search algorithm for rustlib path 2021-05-10 19:15:19 +03:00
rustc_data_structures Only compute Obligation cache_key once in register_obligation_at 2021-05-04 11:57:53 -07:00
rustc_driver Rollup merge of #83895 - eggyal:issue-83883, r=jyn514 2021-04-06 17:42:31 +02:00
rustc_error_codes Auto merge of #83213 - rylev:update-lints-to-errors, r=nikomatsakis 2021-05-04 08:09:23 +00:00
rustc_errors Rollup merge of #85050 - FabianWolff:issue-84592, r=jackh726 2021-05-10 16:15:00 +02:00
rustc_expand Rollup merge of #84442 - jyn514:doc-cfg, r=petrochenkov 2021-05-07 16:19:19 +02:00
rustc_feature Rollup merge of #84871 - richkadel:no-coverage-unstable-only, r=nagisa 2021-05-07 00:38:40 +02:00
rustc_fs_util Optimize away a fs::metadata call. 2021-01-06 08:33:15 -08:00
rustc_graphviz Remove (lots of) dead code 2021-03-27 22:16:33 -04:00
rustc_hir Add type to differentiate between fake and real DefId's 2021-05-04 19:34:12 +02:00
rustc_hir_pretty Handle pretty printing of else if let clauses 2021-04-23 13:29:18 -04:00
rustc_incremental Add [TRACKED_NO_CRATE_HASH] and [SUBSTRUCT] directives 2021-04-27 16:46:33 +00: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 unused variant 2021-05-06 14:59:53 +02:00
rustc_interface Adjust target search algorithm for rustlib path 2021-05-10 19:15:19 +03:00
rustc_lexer Fix outdated crate names in compiler docs 2021-04-08 11:12:14 -05:00
rustc_lint Rollup merge of #85075 - ptrojahn:panic_warning, r=jackh726 2021-05-10 16:15:01 +02:00
rustc_lint_defs Auto merge of #83213 - rylev:update-lints-to-errors, r=nikomatsakis 2021-05-04 08:09:23 +00:00
rustc_llvm Support -C passes in NewPM 2021-05-08 10:58:08 +02:00
rustc_macros Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
rustc_metadata Implement RFC 2951: Native link modifiers 2021-05-05 16:04:25 -07:00
rustc_middle Auto merge of #84507 - crlf0710:codegen_nonlocal_main_wrapper, r=nagisa 2021-05-10 00:42:31 +00:00
rustc_mir Auto merge of #84507 - crlf0710:codegen_nonlocal_main_wrapper, r=nagisa 2021-05-10 00:42:31 +00:00
rustc_mir_build Auto merge of #84310 - RalfJung:const-fn-feature-flags, r=oli-obk 2021-04-24 23:16:03 +00:00
rustc_parse Auto merge of #85104 - hi-rustin:rustin-patch-typo, r=jonas-schievink 2021-05-10 07:15:23 +00:00
rustc_parse_format Rollup merge of #83348 - osa1:issue83344, r=jackh726 2021-03-28 01:33:13 +09:00
rustc_passes Implement RFC 1260 with feature_name imported_main. 2021-04-29 08:35:08 +08:00
rustc_plugin_impl Remove hir::Item::attrs. 2021-03-09 19:27:50 +01:00
rustc_privacy fix few typos 2021-04-19 15:57:08 +03:00
rustc_query_impl Move iter_results to dyn FnMut rather than a generic 2021-04-29 17:26:46 -04:00
rustc_query_system Auto merge of #84806 - Mark-Simulacrum:try-start-entry, r=cjgillot 2021-05-06 22:35:06 +00:00
rustc_resolve More minor fixes suggested by @jackh726 2021-05-10 15:02:15 +02:00
rustc_save_analysis Remove hir::CrateItem. 2021-03-30 20:31:06 +02:00
rustc_serialize Auto merge of #83465 - michaelwoerister:safe-read_raw_bytes, r=cjgillot 2021-03-26 01:28:59 +00:00
rustc_session Rollup merge of #85152 - nagisa:target-search-rustlib, r=Mark-Simulacrum 2021-05-10 20:05:27 +02:00
rustc_span Rollup merge of #85044 - ChrisDenton:file-exists, r=jackh726 2021-05-08 01:06:26 +02:00
rustc_symbol_mangling Rollup merge of #80525 - devsnek:wasm64, r=nagisa 2021-04-05 00:24:23 +02:00
rustc_target Adjust target search algorithm for rustlib path 2021-05-10 19:15:19 +03:00
rustc_trait_selection Rollup merge of #84987 - lcnr:nits, r=Mark-Simulacrum 2021-05-07 00:38:45 +02:00
rustc_traits Auto merge of #83207 - oli-obk:valtree2, r=lcnr 2021-04-02 10:28:12 +00:00
rustc_ty_utils Implement changes suggested by tmiasko and davidtwco 2021-05-10 17:47:50 +02:00
rustc_type_ir Make TypeFoldable::is_global() false when fresh tys/consts are present 2021-05-01 16:58:33 -04:00
rustc_typeck fulfillment typo 2021-05-07 12:17:58 +02:00