222ae8b9bb
The representability-checking routine ```is_type_representable``` failed to detect structural recursion in some cases, leading to stack overflow later on. The first problem was in the loop in the ```find_nonrepresentable``` function. We were improperly terminating the iteration if we saw a ```ContainsRecursive``` condition. We should have kept going in case a later member of the struct (or enum, etc) being examined was ```SelfRecursive```. The example from #17431 triggered this issue: ```rust use std::sync::Mutex; struct Foo { foo: Mutex<Option<Foo>> } impl Foo { fn bar(self) {} } fn main() {} ``` I'm not 100% sure, but I think the ```ty_enum``` case of ```fn type_structurally_recursive``` had a similar problem, since it could ```break``` on ```ContainsRecursive``` before looking at all variants. I've replaced this with a ```flat_map``` call. The second problem was that we were failing to identify code like ```struct Foo { foo: Option<Option<Foo>> }``` as SelfRecursive, even though we correctly identified ```struct Foo { foo: Option<Foo> }```. This was caused by using DefId's for the ```ContainsRecursive``` check, which meant the nested ```Option```s were identified as illegally recursive (because ```ContainsRecursive``` is not an error, we would then keep compiling and eventually hit a stack overflow). In order to make sure that we can recurse through the different ```Option``` invocations, I've changed the type of ```seen``` from ```Vec<DefId>``` to ```Vec<t>``` and added a separate ```same_type``` function to check whether two types are the same when generics are taken into account. Now we only return ```ContainsRecursive``` when this stricter check is satisfied. (There's probably a better way to do this, and I'm not sure my code is entirely correct--but my knowledge of rustc internals is pretty limited, so any help here would be appreciated!) Note that the ```SelfRecursive``` check is still comparing ```DefId```s--this is necessary to prevent code like this from being allowed: ```rust struct Foo { x: Bar<Foo> } struct Bar<T> { x: Bar<Foo> } ``` All four of the new ```issue-17431``` tests cause infinite recursion on master, and errors with this pull request. I wrote the extra ```issue-3008-4.rs``` test to make sure I wasn't introducing a regression. Fixes #17431. |
||
---|---|---|
.. | ||
back | ||
driver | ||
lint | ||
metadata | ||
middle | ||
plugin | ||
util | ||
diagnostics.rs | ||
lib.rs | ||
README.txt |
An informal guide to reading and working on the rustc compiler. ================================================================== If you wish to expand on this document, or have a more experienced Rust contributor add anything else to it, please get in touch: https://github.com/rust-lang/rust/wiki/Note-development-policy ("Communication" subheading) or file a bug: https://github.com/rust-lang/rust/issues Your concerns are probably the same as someone else's. High-level concepts =================== Rustc consists of the following subdirectories: front/ - front-end: attributes, conditional compilation middle/ - middle-end: name resolution, typechecking, LLVM code generation back/ - back-end: linking and ABI metadata/ - encoder and decoder for data required by separate compilation driver/ - command-line processing, main() entrypoint util/ - ubiquitous types and helper functions lib/ - bindings to LLVM The files concerned purely with syntax -- that is, the AST, parser, pretty-printer, lexer, macro expander, and utilities for traversing ASTs -- are in a separate crate called "syntax", whose files are in ./../libsyntax, where . is the current directory (that is, the parent directory of front/, middle/, back/, and so on). The entry-point for the compiler is main() in lib.rs, and this file sequences the various parts together. The 3 central data structures: ------------------------------ #1: ./../libsyntax/ast.rs defines the AST. The AST is treated as immutable after parsing, but it depends on mutable context data structures (mainly hash maps) to give it meaning. - Many -- though not all -- nodes within this data structure are wrapped in the type `spanned<T>`, meaning that the front-end has marked the input coordinates of that node. The member .node is the data itself, the member .span is the input location (file, line, column; both low and high). - Many other nodes within this data structure carry a def_id. These nodes represent the 'target' of some name reference elsewhere in the tree. When the AST is resolved, by middle/resolve.rs, all names wind up acquiring a def that they point to. So anything that can be pointed-to by a name winds up with a def_id. #2: middle/ty.rs defines the datatype sty. This is the type that represents types after they have been resolved and normalized by the middle-end. The typeck phase converts every ast type to a ty::sty, and the latter is used to drive later phases of compilation. Most variants in the ast::ty tag have a corresponding variant in the ty::sty tag. #3: lib/llvm.rs defines the exported types ValueRef, TypeRef, BasicBlockRef, and several others. Each of these is an opaque pointer to an LLVM type, manipulated through the lib::llvm interface. Control and information flow within the compiler: ------------------------------------------------- - main() in lib.rs assumes control on startup. Options are parsed, platform is detected, etc. - ./../libsyntax/parse/parser.rs parses the input files and produces an AST that represents the input crate. - Multiple middle-end passes (middle/resolve.rs, middle/typeck.rs) analyze the semantics of the resulting AST. Each pass generates new information about the AST and stores it in various environment data structures. The driver passes environments to each compiler pass that needs to refer to them. - Finally middle/trans.rs translates the Rust AST to LLVM bitcode in a type-directed way. When it's finished synthesizing LLVM values, rustc asks LLVM to write them out in some form (.bc, .o) and possibly run the system linker.