Skip to content

Filter PhantomData fields in CoerceShared borrowck field matching#156785

Closed
ikow wants to merge 2 commits into
rust-lang:mainfrom
ikow:fix/coerce-shared-field-matching
Closed

Filter PhantomData fields in CoerceShared borrowck field matching#156785
ikow wants to merge 2 commits into
rust-lang:mainfrom
ikow:fix/coerce-shared-field-matching

Conversation

@ikow
Copy link
Copy Markdown

@ikow ikow commented May 20, 2026

Summary

The add_generic_reborrow_constraint function in compiler/rustc_borrowck/src/type_check/mod.rs matches source and dest struct fields by name when establishing borrow constraints for CoerceShared. However, it uses all_fields() which includes PhantomData fields, while the coherence check (coerce_shared_info) filters them out via collect_struct_data_fields.

This mismatch means PhantomData fields with different names between source and dest structs are silently skipped via continue (line 2524), potentially dropping lifetime constraints that should be established.

Fix

Filter out PhantomData fields before matching, consistent with the coherence check's use of collect_struct_data_fields.

Test plan

  • Added tests/ui/reborrow/coerce_shared_phantom_field_names.rs — a run-pass test with CoerceShared source/dest structs that have differently-named PhantomData fields (_marker vs _lifetime)
  • This test verifies the borrow constraints are correctly established even with mismatched PhantomData field names

Tracking issue: #145612 (reborrow feature)

The `add_generic_reborrow_constraint` function matches source and dest
struct fields by name when establishing borrow constraints for
CoerceShared. However, it uses `all_fields()` which includes PhantomData
fields, while the coherence check (`coerce_shared_info`) filters them
out via `collect_struct_data_fields`. This mismatch means PhantomData
fields with different names between source and dest are silently skipped,
potentially dropping lifetime constraints.

Fix: filter out PhantomData fields before matching, consistent with the
coherence check.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@rustbot rustbot added 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 May 20, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 20, 2026

r? @JohnTitor

rustbot has assigned @JohnTitor.
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 73 candidates
  • Random selection from 20 candidates

@rust-log-analyzer

This comment has been minimized.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@workingjubilee
Copy link
Copy Markdown
Member

@ikow Hello! Could you cite the actual code pattern in Rust source that should or should not compile but (on the current main) does or does not due to the bug in question?

It is not actually clear to me what your regression test is actually testing, because it is a run-pass test but the code also compiles and passes using a nightly compiler. I did not build main's b954122 to verify since nightly's e50aa6f worked. There is no regression that this test can prevent because a run-pass test first has to be something that doesn't compile on some previous Rust version.

@jieyouxu
Copy link
Copy Markdown
Member

@rustbot author

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

rustbot commented May 21, 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 May 21, 2026
@oli-obk
Copy link
Copy Markdown
Contributor

oli-obk commented May 21, 2026

Please do not send us PRs that were generated that. We're a community, not just a code repository. We want contributors who want to meaningfully contribute, not just bugs that are fixed (which in your case the generated tests and code don't do). Feel free to reach out on Zulip to ask for how to best contribute and do read the rustc-dev-guide and our contribution guidelines.

This is a moderation warning. If you want to discuss your warning, reach out to moderation via email or on Zulip.

@oli-obk oli-obk closed this May 21, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

7 participants