A deep technical exploration of non-lexical lifetimes (NLL) in Rust, focusing on Problem Case #3 involving cross-function lifetime constraints. The post explains why RFC 396's dominator-tree-based lifetime definition fails for certain patterns where a lifetime must 'gap' across a conditional branch. It introduces the concept of location-aware outlives relations — written as ('a: 'b) at P — where the outlives constraint is evaluated relative to a specific point in the control-flow graph. This allows lifetimes to exclude unreachable CFG points, enabling the borrow checker to accept valid code that would otherwise be incorrectly rejected. Detailed MIR-like pseudocode and CFG diagrams illustrate how outlives constraints are derived from subtyping rules and liveness analysis.

18m read timeFrom smallcultfollowing.com
Post cover image
Table of contents
Problem case #3 revisitedRunning example: inlineWhy RFC 396 alone can’t handle this exampleThe outlives relation in light of control-flowRunning example in pseudo-MIRSimple superset considered insufficientLocation-area outlivesConclusion and some discussion of alternative approachesCommentsAppendix A: An alternative: variables have multiple typesAppendix B: How subtyping links the lifetime of arguments and the return value
25 Impressions