Skip to content

Commit e26f208

Browse files
Rollup merge of #153422 - nnethercote:tcx-comment, r=bjorn3
Add a comment explaining the 'tcx lifetime. I wrote this after thinking about where the `'tcx` lifetime actually comes from. r? @bjorn3
2 parents 1e70647 + b3fddcb commit e26f208

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

compiler/rustc_interface/src/passes.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,17 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
964964

965965
let incremental = dep_graph.is_fully_enabled();
966966

967+
// Note: this function body is the origin point of the widely-used 'tcx lifetime.
968+
//
969+
// `gcx_cell` is defined here and `&gcx_cell` is passed to `create_global_ctxt`, which then
970+
// actually creates the `GlobalCtxt` with a `gcx_cell.get_or_init(...)` call. This is done so
971+
// that the resulting reference has the type `&'tcx GlobalCtxt<'tcx>`, which is what `TyCtxt`
972+
// needs. If we defined and created the `GlobalCtxt` within `create_global_ctxt` then its type
973+
// would be `&'a GlobalCtxt<'tcx>`, with two lifetimes.
974+
//
975+
// Similarly, by creating `arena` here and passing in `&arena`, that reference has the type
976+
// `&'tcx WorkerLocal<Arena<'tcx>>`, also with one lifetime. And likewise for `hir_arena`.
977+
967978
let gcx_cell = OnceLock::new();
968979
let arena = WorkerLocal::new(|_| Arena::default());
969980
let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());

0 commit comments

Comments
 (0)