Skip to content

Add a TaggedQueryKey to identify a query instance#153492

Open
Zoxc wants to merge 1 commit intorust-lang:mainfrom
Zoxc:querykeyid
Open

Add a TaggedQueryKey to identify a query instance#153492
Zoxc wants to merge 1 commit intorust-lang:mainfrom
Zoxc:querykeyid

Conversation

@Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Mar 6, 2026

View all comments

This adds back a TaggedQueryKey enum which represents a query kind and the associated key. This is used to replace QueryStackDeferred and QueryStackFrameExtra and the associated lifting operation for cycle errors

This approach has a couple of benefits:

  • We only run description queries when printing the query stack trace in the panic handler
  • The unsafe code for QueryStackDeferred is removed
  • Cycle handles have access to query keys, which may be handy

Some further work could be replacing QueryStackFrame with TaggedQueryKey as the extra information can be derived from it.

@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 6, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 6, 2026

r? @mati865

rustbot has assigned @mati865.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, query-system
  • compiler, query-system expanded to 69 candidates
  • Random selection from 16 candidates

@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 6, 2026

cc @nnethercote @Zalathar

@Zalathar
Copy link
Member

Zalathar commented Mar 6, 2026

If we can get rid of a lot of the QueryStackFrame complexity by just storing the query key in an enum, that seems great.

The name QueryKeyId seems strange to me, though. It’s not the ID of a query key; it’s the key itself, “tagged” with the query that it belongs to.

@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 6, 2026

The name QueryKeyId seems strange to me, though. It’s not the ID of a query key; it’s the key itself, “tagged” with the query that it belongs to.

Yeah, QueryId was taken, maybe QueryInstance is better?

@rust-bors

This comment has been minimized.

@nnethercote nnethercote assigned nnethercote and unassigned mati865 Mar 6, 2026
@nnethercote
Copy link
Contributor

This looks great! I hate the deferred/extra split, this seems like a really clean way to eliminate it.

@Zoxc: can you expand the first paragraph of the PR description to explain (a) the current situation, and (b) how this PR changes that situation? I've skimmed the changes and they look good, but I will need to look again more closely on Monday to get my head fully around how the new QueryKeyId is used and how it lets so much of the existing complexity be removed. Extra description will help with that.

cc @zetanumbers

@Zalathar
Copy link
Member

Zalathar commented Mar 6, 2026

Yeah, QueryId was taken, maybe QueryInstance is better?

I was thinking of something like TaggedQueryKey, because the value being stored is a query key, tagged with its corresponding query.

I think it's easier to say “this is a key that also indicates what query it came from” than to try to come up with an intuitive name for query+key.

Or we could go for something like QueryAndKey or QueryWithKey.

@nnethercote
Copy link
Contributor

TaggedQueryKey sounds good to me -- that's exactly what it is!

@nnethercote
Copy link
Contributor

Just a little more work required to get this into shape.

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 10, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 10, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 10, 2026
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@Zoxc Zoxc changed the title Add a QueryKeyId to identify a query instance Add a TaggedQueryKey to identify a query instance Mar 11, 2026
@rust-bors

This comment has been minimized.

Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

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

Getting closer! Please squash the two commits together.

View changes since this review

}
match self {
$(
TaggedQueryKey::$name(key) => inner(key, tcx),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why have inner? Why not just call key.key_as_def_id()... here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It reduces code generation as inner can be reused for shared key types.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a comment explaining this?

pub info: I,

pub struct QueryStackFrame<'tcx> {
pub id: TaggedQueryKey<'tcx>,
Copy link
Contributor

Choose a reason for hiding this comment

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

This field needs renaming. Maybe tagged_key?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is used as an identifier though?

Copy link
Member

Choose a reason for hiding this comment

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

In the compiler, an “id” is usually an opaque or mostly-opaque integer that indexes into some other table, so I don’t understand the motivation behind calling this value an id.

query: &'tcx QueryVTable<'tcx, C>,
tcx: TyCtxt<'tcx>,
require_complete: bool,
make_id: fn(C::Key) -> TaggedQueryKey<'tcx>,
Copy link
Contributor

Choose a reason for hiding this comment

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

This parameter needs renaming. Maybe mk_tagged_key?

vtable: &'tcx QueryVTable<'tcx, C>,
key: C::Key,
) -> QueryStackFrame<QueryStackDeferred<'tcx>>
make_id: fn(C::Key) -> TaggedQueryKey<'tcx>,
Copy link
Contributor

Choose a reason for hiding this comment

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

mk_tagged_key?

@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 12, 2026

#153685 conflicts a bit as $name is no longer accessible in collect_active_jobs_from_all_queries. Adding a TaggedQueryKey constructor as a closure argument in for_each_query_vtable! doesn't seem very clean either.

@Zalathar
Copy link
Member

Would it work to add the enum constructor as a function in QueryVTable?

@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 12, 2026

I think so, but I'd probably prefer reverting the use of for_each_query_vtable! for collect_active_jobs_from_all_queries over that though.

@Zalathar
Copy link
Member

I think so, but I'd probably prefer reverting the use of for_each_query_vtable! for collect_active_jobs_from_all_queries over that though.

Given that the variant constructor is already being passed around as a fn, I don’t see why we wouldn’t want to put it in the vtable.

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Mar 12, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

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) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

6 participants