From 0ee92840837df517f78de6f37f7846c2db54dd8f Mon Sep 17 00:00:00 2001 From: lcnr Date: Mon, 7 Nov 2022 15:17:06 +0100 Subject: [PATCH 1/2] extract regions --- src/type-inference.md | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/type-inference.md b/src/type-inference.md index 10f1dd5ef..5681352f2 100644 --- a/src/type-inference.md +++ b/src/type-inference.md @@ -218,29 +218,33 @@ algorithms. [`region_constraints`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/infer/region_constraints/index.html [`opportunistic_resolve_var`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/infer/region_constraints/struct.RegionConstraintCollector.html#method.opportunistic_resolve_var -## Extracting region constraints +## Solving region constraints -Ultimately, region constraints are only solved at the very end of -type-checking, once all other constraints are known. There are two +Region constraints are only solved at the very end of +typechecking, once all other constraints are known and +all other obligations have been proven. There are two ways to solve region constraints right now: lexical and non-lexical. Eventually there will only be one. +An exception here is the leak-check which is used during trait solving +and relies on region constraints containing higher-ranked regions. Region +constraints in the root universe (i.e. not arising from a `for<'a>`) must +not influence the trait system, as these regions are all erased during +codegen. + To solve **lexical** region constraints, you invoke [`resolve_regions_and_report_errors`]. This "closes" the region constraint process and invokes the [`lexical_region_resolve`] code. Once this is done, any further attempt to equate or create a subtyping relationship will yield an ICE. -Non-lexical region constraints are not handled within the inference -context. Instead, the NLL solver (actually, the MIR type-checker) -invokes [`take_and_reset_region_constraints`] periodically. This -extracts all of the outlives constraints from the region solver, but -leaves the set of variables intact. This is used to get *just* the -region constraints that resulted from some particular point in the -program, since the NLL solver needs to know not just *what* regions -were subregions, but also *where*. Finally, the NLL solver invokes -[`take_region_var_origins`], which "closes" the region constraint -process in the same way as normal solving. +The NLL solver (actually, the MIR type-checker) invokes does things slightly +differently. It uses canonical queries for trait solving which use +[`take_and_reset_region_constraints`] at the end. This extracts all of the +outlives constraints added during the canonical query. This is required +as the NLL solver must not only know *what* regions outlive each other, +but also *where*. Finally, the NLL solver invokes [`take_region_var_origins`], +providing all region variables to the solver. [`resolve_regions_and_report_errors`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/infer/struct.InferCtxt.html#method.resolve_regions_and_report_errors [`lexical_region_resolve`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/infer/lexical_region_resolve/index.html From 0c26684100ae2c37a17ff6445e7c3550fdd5585a Mon Sep 17 00:00:00 2001 From: lcnr Date: Wed, 9 Nov 2022 10:21:21 +0100 Subject: [PATCH 2/2] Update src/type-inference.md Co-authored-by: Tshepang Mbambo --- src/type-inference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/type-inference.md b/src/type-inference.md index 5681352f2..ca88c1686 100644 --- a/src/type-inference.md +++ b/src/type-inference.md @@ -238,7 +238,7 @@ constraint process and invokes the [`lexical_region_resolve`] code. Once this is done, any further attempt to equate or create a subtyping relationship will yield an ICE. -The NLL solver (actually, the MIR type-checker) invokes does things slightly +The NLL solver (actually, the MIR type-checker) does things slightly differently. It uses canonical queries for trait solving which use [`take_and_reset_region_constraints`] at the end. This extracts all of the outlives constraints added during the canonical query. This is required