Skip to content

Add per query cycle handlers and use them for representability#153028

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

Add per query cycle handlers and use them for representability#153028
Zoxc wants to merge 1 commit intorust-lang:mainfrom
Zoxc:cycle-handlers

Conversation

@Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Feb 23, 2026

This adds a per query cycle handler where queries which are part of a cycle can choose to emit a suitable cycle error instead of the default cycle error format.

The representability query is changed to make use of it instead of doing it using Value which is not intended for that purpose.

The representability query is also changed so that the error is fatal, so we no longer resume after recursive type errors. This improves error output for our tests since we no longer get multiple related cycle errors.

This is a first step towards removing query cycle recovery and making all cycle errors fatal.

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

rustbot commented Feb 23, 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
  • compiler expanded to 68 candidates
  • Random selection from 13 candidates

@mati865
Copy link
Member

mati865 commented Feb 24, 2026

@rustbot reroll

@rustbot rustbot assigned nnethercote and unassigned mati865 Feb 24, 2026
Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

Ah yea, that's actually better than dispatching on types. I think we can nuke most if not all fallbacks with such a scheme.

Still not sure we can make all query cycles fatal, but worst case we make the cycle handlers return the query return type, too

View changes since this review

mod ty;

pub fn provide(providers: &mut Providers) {
abi::provide(providers);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please do changes like this in a separate commit

for variant in tcx.adt_def(def_id).variants() {
for field in variant.fields.iter() {
rtry!(tcx.representability(field.did.expect_local()));
let _ = tcx.representability(field.did.expect_local());
Copy link
Contributor

Choose a reason for hiding this comment

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

The let _ is not necessary for unit returning queries

/// Checks whether a type is representable or infinitely sized
query representability(key: LocalDefId) -> rustc_middle::ty::Representability {
/// Checks whether a type is representable or infinitely sized, emits a cycle error if it's not.
query representability(key: LocalDefId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably be check_representable or sth now

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

rustbot commented Feb 24, 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 Feb 24, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 24, 2026

☔ The latest upstream changes (presumably #153047) made this pull request unmergeable. Please resolve the merge conflicts.

@zetanumbers
Copy link
Contributor

@Zoxc could you please explain how did you come up with this idea?

@zetanumbers
Copy link
Contributor

zetanumbers commented Feb 25, 2026

Or just clarify what you did or did not come up with on your own?

@nnethercote
Copy link
Contributor

This is a first step towards removing query cycle recovery and making all cycle errors fatal.

I think this is a good goal, could make a lot of things simpler. I don't understand a lot of what's going on in this PR though. There is some new stuff added (why?) and lots of code moved around, all in a single commit.

@zetanumbers: Do you have thoughts on the implementation approach? If you wanted to remove query cycle recovery, how would you go about it?

@zetanumbers
Copy link
Contributor

zetanumbers commented Feb 26, 2026

@zetanumbers: Do you have thoughts on the implementation approach? If you wanted to remove query cycle recovery, how would you go about it?

I am not sure if "removing query cycle recovery" also means removing break_query_cycles. If it does then I would like to hear what plan does @Zoxc have for it. I have major concerns about it.

And "making all cycle errors fatal" would require untangling all of diagnostics code relying on query cycle detection. It might also require drawbacks to diagnostics or added complexity to the query system as it is written the way it is for some reason, that's the "new stuff added". So I don't see this goal to be inherently positive and required changes should be judged case by case. But maybe Zoxc has some insight about other queries too.

These are my general concerns. I should give it a closer look before giving a proper guidance for further changes. We should check if we lost any information by removing those diagnostic messages.

@zetanumbers
Copy link
Contributor

zetanumbers commented Feb 27, 2026

I don't like the fact that representability is not a pure function anymore. Every day. We stray further from god. As the matter of fact removing value_from_cycle_error entirely won't really reduce amount of code as we would need to retain all the diagnostics code from there, as it is the majority of that code.

I wouldn't have personally bought this partial change for myself, as I doubt that reworking these diagnostics would be possible without some substantial drawback. On the other hand if we had all of required code to reach this goal, that could be a pretty sweet deal.

Also I think I've figured what Zoxc really wants to do there. I would like @Zoxc to give explanation for himself, so please elaborate a bit on this part:

https://github.com/rust-lang/rust/pull/153028/changes#diff-d1f11c402fb3b78c4c31beada5ff2a06660894e9062810a9d05fa26a92b79301R110-R118

Although I am struggling to understand why this loop stops on the first cycle handler to return ErrorGuaranteed. I thought you would like to run every cycle handler for every query in the cycle instead, since we won't get rid of non-determinism (#142063) this way.

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