Skip to content

Two small query improvements#154067

Closed
nnethercote wants to merge 2 commits intorust-lang:mainfrom
nnethercote:two-small-query-improvements
Closed

Two small query improvements#154067
nnethercote wants to merge 2 commits intorust-lang:mainfrom
nnethercote:two-small-query-improvements

Conversation

@nnethercote
Copy link
Contributor

Improving a couple of pieces of code I found a little hard to read.

r? @Zalathar

It permits four combinations: `false+None`, `false+Some`, `true+None`,
and `true+Some`. That final case is not useful; when the bool is true
the `Option` is not looked at.

This commit changes it to an enum that encodes three combination: `Yes`,
`No(None)`, `No(Some)`. I find this easier to read.
It currently uses chaining which is concise but hard to read. There are
up to four implicit matches occurring after the call to
`execute_query_fn`.
- The first `map` on `Option<Erased<Result<T, ErrorGuaranteed>>>`.
- The second `map` on `Option<Result<T, ErrorGuaranteed>>`.
- The third `map` on `Result<T, ErrorGuaranteed>`.
- The `unwrap_or` on `Option<Result<(), ErrorGuaranteed>>`.

This commit rewrites it to use at most two matches.
- An explicit match on `Option<Erased<Result<T, ErrorGuaranteed>>>`.
- A `map` on `Result<T, ErrorGuaranteed>`.

This is easier to read. It's also more efficient, though the code isn't
hot enough for that to matter.
@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 19, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 19, 2026

Zalathar is not on the review rotation at the moment.
They may take a while to respond.

Comment on lines +579 to +580
// We may have created a `DepNode` while determining whether we can skip.
No(Option<DepNode>),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd prefer a named field here (e.g. dep_node), especially since the field's meaning is already non-obvious enough to require a comment.

(In general I think positional enum fields are bad news most of the time, if their meaning isn't overwhelmingly obvious.)

@Zalathar
Copy link
Member

Looks good overall, besides the nit.

I can imagine some further improvements to query_ensure_result (e.g. doing something about the cryptic use of drop), but no need to scope-creep this particular PR.

@nnethercote
Copy link
Contributor Author

Actually, I'll close this and split it into two separate PRs. #154075 is for the query_ensure_result change, and I've eliminated the drop.

I'll file another one for the EnsureCanSkip changes later, because I think there are some more changes I can make there.

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants