coverage: Prepare for upcoming changes to counter creation by Zalathar · Pull Request #135873 · rust-lang/rust (original) (raw)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request
…piler-errors
Allow arena_cache
queries to return Option<&'tcx T>
Currently, arena_cache
queries always have to return &'tcx T
[^deref]. This means that if an arena-cached query wants to return an optional value, it has to return &'tcx Option<T>
, which has a few negative consequences:
- It goes against normal Rust style, where
Option<&T>
is preferred over&Option<T>
. - Callers that actually want an
Option<&T>
have to manually call.as_ref()
on the query result. - When the query result is
None
, a full-sizedOption<T>
still needs to be stored in the arena.
This PR solves that problem by introducing a helper trait ArenaCached
that is implemented for both &T
and Option<&T>
, and takes care of bridging between the provided type, the arena-allocated type, and the declared query return type.
To demonstrate that this works, I have converted the two existing arena-cached queries that currently return &Option<T>
: mir_coroutine_witnesses
and diagnostic_hir_wf_check
. Only the query declarations need to be modified; existing providers and callers continue to work with the new query return type.
(My real goal is to apply this to coverage_ids_info
, which will return Option as of rust-lang#135873, but that PR hasn't landed yet.)
[^deref]: Technically they could return other types that implement Deref
, but it's hard to imagine this working well with anything other than &T
.