Skip to content

Derive GenericTypeVisitable for RegionConstraint _correctly_ - #160914

Open
ada4a wants to merge 5 commits into
rust-lang:mainfrom
ada4a:GenericTypeVisitable-bounds
Open

Derive GenericTypeVisitable for RegionConstraint _correctly_#160914
ada4a wants to merge 5 commits into
rust-lang:mainfrom
ada4a:GenericTypeVisitable-bounds

Conversation

@ada4a

@ada4a ada4a commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

The derive added in #160164 was incorrect -- it resulted in an overflow during trait solving. This is because #[derive(GenericTypeVisitable)] automatically adds a : GenericTypeVisitable bound to every field of a type -- in this case, Box<[RegionConstraint<I>]>: GenericTypeVisitable<V>.

To fix this, I added a #[generic_type_visitable(bounds(..))] attribute to the derive macro, which allows overriding the added bounds.

I also made the derive macro no longer a no-op in rustc, so that errors like this can be caught on r-l/r CI in the future.

cc @ChayimFriedman2

ada4a added 3 commits August 11, 2026 15:50
This is motivated by rust-lang#160164,
which added the derive which wouldn't actually work, due to recusrive
trait bounds (more on this in a later commit). This change will make it
so that these errors are caught in rustc CI.
Becuase its implementations must uphold a soundness-critical invariant.
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 11, 2026
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 11, 2026
@rustbot

rustbot commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
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 75 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

This comment has been minimized.

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

It doesn't need to be unsafe. If the bounds are wrong it won't compile (at least in r-a).

@ada4a

ada4a commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

What about a case like this?

#[derive(GenericTypeVisitable)]
struct Foo {
    #[generic_type_visitable(unsafe(bounds()))]
    contains_self: (Box<Self>, Bar),
}

struct Bar;

AFAICT bounds are incorrectly missing Bar: GenericTypeVisitable, which leads to unsoundness.

@ada4a
ada4a force-pushed the GenericTypeVisitable-bounds branch from 87bfdc9 to fc37746 Compare August 11, 2026 14:52
@ChayimFriedman2

Copy link
Copy Markdown
Contributor

No, because then calling GenericTypeVisitable::visit(&self.contains_self) will fail compilation.

@ada4a
ada4a force-pushed the GenericTypeVisitable-bounds branch 2 times, most recently from 64616ec to ec68072 Compare August 11, 2026 15:25
@ada4a

ada4a commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

The newly added complexity of the derive macro makes me wonder if it deserves some ui tests now.. Not sure where they would go though

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Note that only making the derive not-no-op won't make forgetting to derive it a CI failure, since rustc does not use it for anything.

@ada4a

ada4a commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Yes, that will be covered by a future PR. I thought I'd get this one merged now, so that the fixed derive already gets into the source code -- otherwise the list of ra-ap crate versions I need to catch up on will only continue to rise over time 😅

@rust-log-analyzer

This comment has been minimized.

@ada4a
ada4a force-pushed the GenericTypeVisitable-bounds branch from ec68072 to 958b222 Compare August 11, 2026 16:25
@ada4a

ada4a commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Apparently, a backtick in an ignore description breaks everything... How silly

@JonathanBrouwer

Copy link
Copy Markdown
Contributor

The newly added complexity of the derive macro makes me wonder if it deserves some ui tests now.. Not sure where they would go though

I think this does deserve some tests.
You can write tests that depend on rustc crates in tests/ui-fulldeps, I think these make sense.

///
/// Therefore, it is advised to instead derive this using the derive
/// macro located in `rustc_macros`.
pub unsafe trait GenericTypeVisitable<V> {

@JonathanBrouwer JonathanBrouwer Aug 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is the correctness of this trait actually relied on for soundness anywhere? If not this doesn't need to be unsafe

View changes since the review

/// ```ignore (would need to import GenericTypeVisitable to get this to compile)
/// #[derive(GenericTypeVisitable)]
/// struct Foo {
/// #[generic_type_visitable(bounds())]

@JonathanBrouwer JonathanBrouwer Aug 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How hard would it be to automatically detect bounds that would overflow the trait solver in the proc macro and remove them, rather than having to manually specify the bounds?

View changes since the review

@rustbot rustbot 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 Aug 14, 2026
@rustbot

rustbot commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

5 participants