Skip to content

Remove FromCycleError trait#153493

Open
zetanumbers wants to merge 2 commits intorust-lang:mainfrom
zetanumbers:remove_from_cycle_error
Open

Remove FromCycleError trait#153493
zetanumbers wants to merge 2 commits intorust-lang:mainfrom
zetanumbers:remove_from_cycle_error

Conversation

@zetanumbers
Copy link
Contributor

@zetanumbers zetanumbers commented Mar 6, 2026

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 PR directly modifies value_from_cycle_error function pointer upon vtable initialization and gets rid of FromCycleError.

Removal of FromCycleError might also be a desired change for some developers involved in the query system development due to some other reasons.

This PR is split up into two commits. Code formatting changes are isolated into the second commit for better git diffs in the first one. Nice and simple refactor PR.

I got this idea from #153470 (comment) discussion and thought it would be a pretty quick change to implement.

r? @nnethercote

I think this PR would benefit from your reviewer polish.

@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
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.

Thanks for splitting in into two commits, that made the review easier.

This looks good! Simpler than what I proposed.

  • specialize_query_vtables modifies the necessary vtable entries. This puts some per-query initialization information in a different place to the query definitions. I don't love this, but it avoids having to change the proc macro at all.
  • It doesn't change the signature of the value_from_cycle_error function, so it doesn't affect CycleErrorHandling or the cycle_* query modifiers.

The macro and signature changes could still be done as follow-ups, so I think it's fine that they weren't done in this PR.

Good things:

  • It gets rid of the specialization and the associated unsafe blocks and the leaks.
  • It makes the connection between specific queries and the from_cycle_error behaviour clearer -- you no longer have to look at the type and then see which queries have that as their return type.

Some minor suggestions below, but overall I'm happy with this.

View changes since this review

vtables.variances_of.value_from_cycle_error =
|tcx, cycle, guar| erase_val(variances_of(tcx, cycle, guar));
vtables.layout_of.value_from_cycle_error =
|tcx, cycle, guar| erase_val(layout_of(tcx, cycle, guar));
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 put a blank line between each entry? It's currently quite dense and hard to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 29c6494


vtables.fn_sig.value_from_cycle_error = |tcx, cycle, guar| erase_val(fn_sig(tcx, cycle, guar));
vtables.check_representability.value_from_cycle_error =
|tcx, cycle, guar| erase_val(check_representability(tcx, cycle, guar));
Copy link
Contributor

Choose a reason for hiding this comment

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

check_representability aborts, so there is no need to erase_val here. Also, check_representability's return type can be changed to ! and Representability can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 1e4669e

@nnethercote
Copy link
Contributor

Also, can you expand the commit message on the first commit? Just a couple of sentences to explain the current situation, and how the commit changes things, and why that's an improvement. Thanks.

@nnethercote
Copy link
Contributor

And I'm wondering whether this overlaps with #149319 at all. That PR fixes two bugs, are those bugs relevant here?

@zetanumbers
Copy link
Contributor Author

zetanumbers commented Mar 10, 2026

And I'm wondering whether this overlaps with #149319 at all. That PR fixes two bugs, are those bugs relevant here?

It does overlap, however those bugs were fixed by passing a query key as an argument to the value_from_cycle_error function pointer call. It's just like you have pointed out for a new cycle handler design in #153470 (comment):

Change value_from_cycle_error to this:

value_from_cycle_error(tcx, key?, cycle_error, error)

I have restrained myself from this change to minimize this PR.

@nnethercote
Copy link
Contributor

I have restrained myself from this change to minimize this PR.

Fair enough. Just a few comments to address and then we can merge.

@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 10, 2026
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`.
@zetanumbers zetanumbers force-pushed the remove_from_cycle_error branch from 4d776a9 to 9418745 Compare March 10, 2026 12:29
@zetanumbers
Copy link
Contributor Author

Also, can you expand the commit message on the first commit? Just a couple of sentences to explain the current situation, and how the commit changes things, and why that's an improvement. Thanks.

Ok, I've only squashed commits with suggested changes into the first commit and added some info to its commit message. Should be good now.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 10, 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) 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants