Commit Graph

587 Commits

Author SHA1 Message Date
Zalathar aa223a0220 Re-export rustc_middle::query::{QuerySystem, QueryVTable}
All of the other public items in `rustc_middle::query::plumbing` are
re-exported from `query`, except for these two, for no particular reason that I
can see.

Re-exporting them allows `rustc_middle::query::plumbing` to have its visibility
reduced to pub(crate).

Imports within `rustc_middle` have also been updated to consistently use the
re-exports in `crate::query`.
2026-03-16 18:20:35 +11:00
Nicholas Nethercote f3f4ab9205 Remove QueryInfo.
`CycleError` has one field containing a `(Span, QueryStackFrame<I>)` and
another field containing a `QueryInfo`, which is a struct containing
just a `Span` and a `QueryStackFrame<I>`.

We already have the `Spanned` type for adding a span to something. This
commit uses it for both fields in `CycleError`, removing the need for
`QueryInfo`. Which is good for the following reasons.
- Any type with `Info` in the name is suspect, IMO.
- `QueryInfo` can no longer be confused with the similar `QueryJobInfo`.
- The doc comment on `QueryInfo` was wrong; it didn't contain a query
  key.
2026-03-16 08:54:18 +11:00
Matthias Krüger e167b9b5c4 Rollup merge of #153897 - Zalathar:use-macro, r=petrochenkov
Use less `#[macro_use]` in the query system

Macro-rules namespacing and import/export is a bit of a nightmare in general. We can tame it a bit by avoiding `#[macro_use]` as much as possible, and instead putting a `pub(crate) use` after the macro-rules declaration to make the macro importable as a normal item.

I've split this PR into two commits. The first one should hopefully be uncontroversial, while the second commit takes the extra step of declaring `rustc_with_all_queries!` as a macros-2.0 macro, which gives much nicer import/export behaviour, at the expense of having to specify `#[rustc_macro_transparency = "semiopaque"]` to still have access to macro-rules hygiene, because the default macros-2.0 hygiene is too strict here.

There should be no change to compiler behaviour.

---

I stumbled into this while investigating some other changes, such as re-exporting `rustc_middle::query::QueryVTable` or moving the big callback macro out of `rustc_middle::query::plumbing`. I think it makes sense to make this change first, to make other module-juggling tasks easier.

r? nnethercote (or compiler)
2026-03-15 16:52:45 +01:00
Zalathar e8ebfcaa2e Declare rustc_with_all_queries! as macros-2.0
Unlike `macro_rules!`, macros-2.0 macros have sensible item-like namespacing
and visibility by default, which avoids the need for `#[macro_export]` and
makes it easier to import the macro.

The tradeoff is having to use `#[rustc_macro_transparency = "semiopaque"]` to
still get macro-rules hygiene, because macros-2.0 hygiene is too strict here.
2026-03-15 13:23:19 +11:00
bors 9f0615c430 Auto merge of #153867 - Zoxc:rem-def_id_for_ty_in_cycle, r=nnethercote
Remove `def_id_for_ty_in_cycle`

This removes `QueryKey::def_id_for_ty_in_cycle` and `QueryStackFrame.def_id_for_ty_in_cycle` by computing it instead from `TaggedQueryKey`.
2026-03-14 23:48:40 +00:00
John Kåre Alsaker 1a30924b17 Remove def_id_for_ty_in_cycle 2026-03-14 14:07:42 +01:00
bors fd2649988f Auto merge of #153690 - nnethercote:find_dep_kind_root, r=petrochenkov
`find_dep_kind_root` tweaks

Two minor changes relating to `find_dep_kind_root`.

r? @SparrowLii
2026-03-14 10:27:50 +00:00
Stuart Cook 22409b471c Rollup merge of #153786 - Zoxc:rm-from-cycle-error-specs, r=nnethercote
Remove `value_from_cycle_error` specializations

This removes `value_from_cycle_error` specializations which are not involved in custom cycle error handling.

This is a step towards removing query cycle recovery.
2026-03-14 17:30:24 +11:00
Stuart Cook e0bb94ec67 Rollup merge of #153760 - Zalathar:query-impl, r=nnethercote
Move and expand the big `rustc_query_impl` macro into a physical `query_impl.rs`

While looking through https://github.com/rust-lang/rust/pull/153588, I came up with a related but different change that I think resolves a lot of tension in the current module arrangement.

The core idea is that if we both define and expand the big macro in the same physical module `rustc_query_impl::query_impl`, then we no longer need to worry about where `mod query_impl` should be declared, or where its imports should go, because those questions now have simple and obvious answers.

The second commit follows up with some more changes inspired by https://github.com/rust-lang/rust/pull/153588. Those particular follow-ups are not essential to the main idea of this PR.

r? nnethercote
2026-03-14 17:30:23 +11:00
Stuart Cook 1d49de5b06 Rollup merge of #153376 - Zoxc:cycle-waiters-iter, r=nnethercote
Replace `visit_waiters` with `abstracted_waiters_of`

This replaces `visit_waiters` which uses closures to visit waiters with `abstracted_waiters_of` which returns a list of waiters. This makes the control flow of their users a bit clearer.

Additionally `abstracted_waiters_of` includes non-query waiters unlike `visit_waiters`. This means that `connected_to_root` now considers resumable waiters from the compiler root as being connected to root, while previously only non-resumable waiters counted. This can result in some additional entry points being found. Similarly in the loop detecting entry points we now consider queries in the cycle with direct resumable waiters from the compiler root as being entry points.

When looking for entry points we now look at waiters until we found a query to populate the `EntryPoint.waiter` field instead of stopping when we determined it to be an entry point.

cc @petrochenkov @nnethercote
2026-03-14 17:30:23 +11:00
Zalathar 69ea0c506d Use more fully-qualified paths and local imports in query_impl 2026-03-14 15:41:20 +11:00
Zalathar 6699c13683 Move the big rustc_query_impl macro into a physical query_impl.rs
Moving the macro and its expansion into the same physical file resolves a lot
of tension in the current module arrangement.

Code in the macro is now free to use plain imports in the same file, and there
is no longer any question of whether `mod query_impl` should be declared inside
the macro, or surrounding a separate expansion site.
2026-03-14 15:41:00 +11:00
Nicholas Nethercote ddd1a69483 Remove Clone derive on QueryJobInfo.
This requires refactoring `depth_limit_error` to do some extra work
immediately instead of returning a cloned `info`.
2026-03-14 10:25:03 +11:00
Nicholas Nethercote 314e224fa5 Streamline find_dep_kind_root.
`current_id` is removable.
2026-03-14 09:17:03 +11:00
John Kåre Alsaker fed57899b9 Remove value_from_cycle_error specialization for type_of 2026-03-13 18:52:26 +01:00
John Kåre Alsaker cdbe2b35b5 Remove value_from_cycle_error specialization for erase_and_anonymize_regions_ty 2026-03-13 18:52:10 +01:00
John Kåre Alsaker 791f4f9315 Remove value_from_cycle_error specialization for type_of_opaque_hir_typeck 2026-03-13 18:51:53 +01:00
Jonathan Brouwer a77d5c9908 Rollup merge of #153707 - nnethercote:rm-CycleErrorHandling, r=Zalathar
Remove `CycleErrorHandling`.

This PR removes the `cycle_*` query modifiers and `CycleErrorHandling`. Some good simplicity wins. Details in individual commits.

r? @Zalathar
2026-03-13 13:27:49 +01:00
Nicholas Nethercote c6c150b0e0 Remove CycleErrorHandling.
Currently if a cycle error occurs the error is created, then handled
according to `CycleErrorHandling` (which comes from the
`cycle_delay_bug` query modifer, or lack thereof), and then
`value_from_cycle_error` is called.

This commit changes things so the error is created and then just passed
to `value_from_cycle_error`. This gives `value_from_cycle_error` full
control over how it's handled, eliminating the need for
`CycleErrorHandling`. This makes things simpler overall.
2026-03-13 22:00:10 +11:00
Nicholas Nethercote 834b7e7e43 Streamline active job collection.
`collect_active_jobs_from_all_queries` takes a `require_complete` bool,
and then some callers `expect` a full map result while others allow a
partial map result. The end result is four possible combinations, but
only three of them are used/make sense.

This commit introduces `CollectActiveJobsKind`, a three-value enum that
describes the three sensible combinations, and rewrites
`collect_active_jobs_from_all_queries` around it. This makes it and its
call sites much clearer, and removes the weird `Option<()>` and
`Result<QueryJobMap, QueryJobMap>` return types.

Other changes of note.
- `active` is removed. The comment about `make_frame` is out of date,
  and `create_deferred_query_stack_frame` *is* safe to call with the
  query state locked.
- When shard locking failure is allowed, collection no longer stops on
  the first failed shard.
2026-03-13 21:57:59 +11:00
John Kåre Alsaker 1f44a11518 Replace visit_waiters with abstracted_waiters_of 2026-03-13 11:47:57 +01:00
Stuart Cook aa2efbc60a Rollup merge of #153492 - Zoxc:querykeyid, r=nnethercote
Add a `TaggedQueryKey` to identify a query instance

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.
2026-03-13 18:28:11 +11:00
John Kåre Alsaker 85967c4de4 Add a TaggedQueryKey to identify a query instance 2026-03-13 07:49:38 +01:00
Nicholas Nethercote f0c27602b1 Some tiny improvements to try_execute_query.
The previous commit inlined `execute_job` without changing any of its
code. This commit does a few tiny follow-up changes.
2026-03-13 13:59:31 +11:00
Nicholas Nethercote 8b0c2591fa Inline and remove execute_job.
`execute_job` has a single call site in `try_execute_query`. This commit
inlines and removes `execute_job`, but also puts the part that checks
feedable results in its own separate function, `check_feedable`, because
it's a nicely separate piece of logic.

The big win here is that all the code dealing with the `QueryState` is
now together in `try_execute_query`: get the lock, do the lookup, drop
the lock, create the job guard, and complete the job guard. Previously
these steps were split across two functions which I found hard to
follow.

This commit purely moves code around, there are no other changes.
2026-03-13 13:59:29 +11:00
Nicholas Nethercote 73e56e8e86 Don't recompute key_hash in wait_for_query.
We compute `key_hash` in `try_execute_query`, so we can just pass the
value in.
2026-03-13 13:41:32 +11:00
Tony Kan cfcbcb715c fix(query): Pass query key to value_from_cycle_error
Co-authored-by: Daria Sukhonina <zetanumbers@users.noreply.github.com>
Co-authored-by: Nicholas Nethercote <nnethercote@users.noreply.github.com>
2026-03-11 19:19:24 -07:00
Jonathan Brouwer 844950343e Rollup merge of #153685 - Zalathar:for-each-query, r=nnethercote
Introduce `for_each_query_vtable!` to move more code out of query macros

After https://github.com/rust-lang/rust/pull/153114 moved a few for-each-query functions into the big `rustc_query_impl::plumbing` macro, I have found that those functions became much harder to navigate and modify, because they no longer have access to ordinary IDE features in rust-analyzer. Even *finding* the functions is considerably harder, because a plain go-to-definition no longer works smoothly.

This PR therefore tries to move as much of that code back out of the macro as possible, with the aid of a smaller `for_each_query_vtable!` helper macro. A typical use of that macro looks like this:

```rust
for_each_query_vtable!(ALL, tcx, |query| {
    query_key_hash_verify(query, tcx);
});
```

The result is an outer function consisting almost entirely of plain Rust code, with all of the usual IDE affordances expected of normal Rust code. Because it uses plain Rust syntax, it can also be formatted automatically by rustfmt.

Adding another layer of macro-defined macros is not something I propose lightly, but in this case I think the improvement is well worth it:
- The outer functions can once again be defined as “normal” Rust functions, right next to their corresponding inner functions, making navigation and modification much easier.
- The closure expression is ordinary Rust code that simply gets repeated ~300 times in the expansion, once for each query, in order to account for the variety of key/value/cache types used by different queries. Even within the closure expression, IDE features still *mostly* work, which is an improvement over the status quo.
- For future maintainers looking at the call site, the macro's effect should hopefully be pretty obvious and intuitive, reducing the need to even look at the helper macro. And the helper macro itself is largely straightforward, with its biggest complication being that it necessarily uses the `$name` metavar from the outer macro.

There should be no change to compiler behaviour.

r? nnethercote (or compiler)
2026-03-11 22:05:46 +01:00
Jonathan Brouwer 4e64a8b568 Rollup merge of #153581 - nnethercote:rm-cycle_stash, r=oli-obk
Simplify `type_of_opaque`.

There is a bunch of complexity supporting the "cannot check whether the hidden type of opaque type satisfies auto traits" error that shows up in `tests/ui/impl-trait/auto-trait-leak.rs`. This is an obscure error that shows up in a single test. If we are willing to downgrade that error message to a cycle error, we can do the following.

- Simplify the `type_of_opaque` return value.
- Remove the `cycle_stash` query modifier.
- Remove the `CyclePlaceholder` type.
- Remove the `SelectionError::OpaqueTypeAutoTraitLeakageUnknown` variant.
- Remove a `FromCycleError` impl.
- Remove `report_opaque_type_auto_trait_leakage`.
- Remove the `StashKey::Cycle` variant.
- Remove the `CycleErrorHandling::Stash` variant.

That's a lot! I think this is a worthwhile trade-off.

r? @oli-obk
2026-03-11 22:05:43 +01:00
Zalathar d066ff7263 Pass the vtable to alloc_self_profile_query_strings_for_query_cache
This simplifies the inner function's signature, and makes it more consistent
with other uses of `for_each_query_vtable!`.
2026-03-11 22:18:02 +11:00
Zalathar 0059012ab2 Introduce for_each_query_vtable! to move more code out of query macros 2026-03-11 22:18:00 +11:00
Nicholas Nethercote 3399ed3c9a Simplify type_of_opaque.
There is a bunch of complexity supporting the "cannot check whether the
hidden type of opaque type satisfies auto traits" error that shows up in
`tests/ui/impl-trait/auto-trait-leak.rs`. This is an obscure error that
shows up in a single test. If we are willing to downgrade that error
message to a cycle error, we can do the following.

- Simplify the `type_of_opaque` return value.
- Remove the `cycle_stash` query modifier.
- Remove the `CyclePlaceholder` type.
- Remove the `SelectionError::OpaqueTypeAutoTraitLeakageUnknown`
  variant.
- Remove a `FromCycleError` impl.
- Remove `report_opaque_type_auto_trait_leakage`.
- Remove the `StashKey::Cycle` variant.
- Remove the `CycleErrorHandling::Stash` variant.

That's a lot! I think this is a worthwhile trade-off.
2026-03-11 21:54:42 +11:00
Jonathan Brouwer 53e67f248d Rollup merge of #153689 - nnethercote:rm-QueryLatchInfo, r=petrochenkov
Eliminate `QueryLatchInfo`.

The boolean `complete` flag indicates whether the `waiters` vec is still in use, which means the `waiters` vec must be empty when `complete` is true. This is achieved by using `drain` on the waiters just after `complete` is set.

We can do better by using the type system to make invalid combinations impossible. This commit removes `complete` and puts the waiters inside an `Option`. `Some` means the query job is still active, and `None` once it is complete. And `QueryLatchInfo` is eliminated.

(The `Arc<Mutex<Option<Vec<Arc<QueryWaiter<'tcx>>>>>>` type is a mouthful, but when it's all in one place I find it easier to understand than when it's split across two types. And we can use `Option` methods like `as_ref`, `as_mut`, and `take`, which is nice.)

r? @petrochenkov
2026-03-11 10:58:52 +01:00
bors b2fabe39bd Auto merge of #153673 - JonathanBrouwer:rollup-cGOKonI, r=JonathanBrouwer
Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#153560 (Introduce granular tidy_ctx's check in extra_checks)
 - rust-lang/rust#153666 (Add a regression test for rust-lang/rust#153599)
 - rust-lang/rust#153493 (Remove `FromCycleError` trait)
 - rust-lang/rust#153549 (tests/ui/binop: add annotations for reference rules)
 - rust-lang/rust#153641 (Move `Spanned`.)
 - rust-lang/rust#153663 (Remove `TyCtxt::node_lint` method and `rustc_middle::lint_level` function)
 - rust-lang/rust#153664 (Add test for rust-lang/rust#109804)
2026-03-11 05:12:10 +00:00
Nicholas Nethercote 3a558f6389 Eliminate QueryLatchInfo.
The boolean `complete` flag indicates whether the `waiters` vec is still
in use, which means the `waiters` vec must be empty when `complete` is
true. This is achieved by using `drain` on the waiters just after
`complete` is set.

We can do better by using the type system to make invalid combinations
impossible. This commit removes `complete` and puts the waiters inside
an `Option`. `Some` means the query job is still active, and `None` once
it is complete. And `QueryLatchInfo` is eliminated.

(The `Arc<Mutex<Option<Vec<Arc<QueryWaiter<'tcx>>>>>>` type is a
mouthful, but when it's all in one place I find it easier to understand
than when it's split across two types. And we can use `Option` methods
like `as_ref`, `as_mut`, and `take`, which is nice.)
2026-03-11 14:41:22 +11:00
Daria Sukhonina 9418745354 Run code formatter 2026-03-10 15:15:14 +03:00
Daria Sukhonina b707e7ae52 Remove FromCycleError trait before code formatting
Currently `QueryVTable`'s `value_from_cycle_error` function pointer uses the `FromCycleError` trait to call query cycle handling code and to construct an erroneous query output value.
This trick however relies too heavily on trait impl specialization and makes those impls inconsistent (which is bad AFAIK).
Instead this commit directly modifies `value_from_cycle_error` function pointer upon vtable initialization and gets rid of `FromCycleError`.
2026-03-10 15:13:47 +03:00
Nicholas Nethercote 18ade8478e Streamline ActiveJobGuard completion.
`ActiveJobGuard::complete` and `ActiveJobGuard::drop` have very similar
code, though non-essential structural differences obscure this a little.

This commit factors out the common code into a new method
`ActiveJobGuard::complete_inner`. It also inlines and remove
`expect_job`, which ends up having a single call site.
2026-03-09 09:53:57 +11:00
Nicholas Nethercote e41bf75d1e Move ActiveJobGuard definition next to its impls. 2026-03-09 09:43:47 +11:00
Jonathan Brouwer 35152ef343 Rollup merge of #153561 - Zalathar:try-mark-green, r=nnethercote
Replace the `try_mark_green` hook with direct calls to `tcx.dep_graph`

All of the existing call sites are directly touching `tcx.dep_graph` anyway, so the extra layer of indirection provides no real benefit.

There should be no change to compiler behaviour.

r? nnethercote (or compiler)
2026-03-08 19:04:37 +01:00
Nicholas Nethercote face186874 Use &C::Key less in queries.
Currently we use a mix of `C::Key` and `&C::Key` parameters. The former
is more common and a bit nicer, so convert some of the latter. This
results in less converting between the two types, and fewer sigils.
2026-03-08 22:54:12 +11:00
Zalathar 0a369a799f Replace the try_mark_green hook with direct calls to tcx.dep_graph
All of the existing call sites are directly touching `tcx.dep_graph` anyway, so
the extra layer of indirection provides no real benefit.
2026-03-08 21:53:13 +11:00
bors c7b206bba4 Auto merge of #153383 - nnethercote:overhaul-ensure_ok, r=Zalathar
Overhaul `ensure_ok`

The interaction of `ensure_ok` and the `return_result_from_ensure_ok` query modifier is weird and hacky. This PR cleans it up. Details in the individual commits.

r? @Zalathar
2026-03-08 07:03:35 +00:00
Nicholas Nethercote d4503b017e Overhaul ensure_ok.
`ensure_ok` provides a special, more efficient way of calling a query
when its return value isn't needed. But there is a complication: if the
query is marked with the `return_result_from_ensure_ok` modifier, then
it will return `Result<(), ErrorGuaranteed`. This is clunky and feels
tacked on. It's annoying to have to add a modifier to a query to declare
information present in its return type, and it's confusing that queries
called via `ensure_ok` have different return types depending on the
modifier.

This commit:

- Eliminates the `return_result_from_ensure_ok` modifier. The proc macro
  now looks at the return type and detects if it matches `Result<_,
  ErrorGuarantee>`. If so, it adds the modifier
  `returns_error_guaranteed`. (Aside: We need better terminology to
  distinguish modifiers written by the user in a `query` declaration
  (e.g. `cycle_delayed_bug`) from modifiers added by the proc macro
  (e.g. `cycle_error_handling`.))

- Introduces `ensure_result`, which replaces the use of `ensure_ok` for
  queries that return `Result<_, ErrorGuarantee>`. As a result,
  `ensure_ok` can now only be used for the "ignore the return value"
  case.
2026-03-08 09:39:39 +11:00
bors 085c58f2c0 Auto merge of #153387 - Zalathar:call-query, r=nnethercote
Get rid of `QueryVTable::call_query_method_fn`



Calling the query method to promote a value is equivalent to doing a cache lookup and then calling `execute_query_fn`, so we can just do that manually instead.

There are two “functional” differences here: If a cache hit occurs, we don't record the hit for self-profiling, and we don't register a read of the dep node. In the context of promotion, which touches *all* eligible cache entries just before writing the memory-cached values to disk, those two steps should be unnecessary overhead anyway.

r? nnethercote (or compiler)
2026-03-07 13:06:24 +00:00
bors ff086354c9 Auto merge of #153316 - nnethercote:rm-with_related_context, r=oli-obk
Remove `tls::with_related_context`.

This function gets the current `ImplicitCtxt` and checks that its `tcx` matches the passed-in `tcx`. It's an extra bit of sanity checking: when you already have a `tcx`, and you need access to the non-`tcx` parts of `ImplicitCtxt`, check that your `tcx` matches the one in `ImplicitCtxt`.

However, it's only used in two places: `start_query` and `current_query_job`. The non-checked alternatives (`with_context`, `with_context_opt`) are used in more places, including some where a `tcx` is available. And things would have to go catastrophically wrong for the check to fail -- e.g. if we somehow end up with multiple `TyCtxt`s. In my opinion it's just an extra case to understand in `tls.rs` that adds little value.

This commit removes it. This avoids the need for `tcx` parameters in a couple of places. The commit also adjusts how `start_query` sets up its `ImplicitCtxt` to more closely match how similar functions do it, i.e. with `..icx.clone()` for the unchanged fields.

r? @oli-obk
2026-03-06 10:21:32 +00:00
Zalathar 743d442845 Rename QueryCache::iter to for_each
Methods that perform internal iteration are typically named `for_each`.
2026-03-05 23:39:59 +11:00
Zalathar 9012d4ecb8 Get rid of QueryVTable::call_query_method_fn
Calling the query method is equivalent to doing a cache lookup and then calling
`execute_query_fn`, so we can just do that manually instead.
2026-03-05 23:35:09 +11:00
Jonathan Brouwer 62d1a712e5 Rollup merge of #153323 - nnethercote:rm-impl-QueryVTable, r=Zalathar
Remove `impl QueryVTable`

Some simplifications to `QueryVTable`, partly extracted from rust-lang/rust#153065.

r? @Zalathar
2026-03-05 06:31:37 +01:00
Jonathan Brouwer fdb18d9782 Rollup merge of #153276 - Zoxc:rem-fatal-cycle, r=nnethercote
Remove `cycle_fatal` query modifier

This removes the `cycle_fatal` query modifier as it has no effect on its current users.

The default `CycleErrorHandling::Error` mode does the same as `cycle_fatal` when the default impl of `FromCycleError` is used. The return types of queries using `cycle_fatal` however have no specialized `FromCycleError` impl.
2026-03-04 19:30:38 +01:00