Skip to content

Ignore fake borrows for packed field check #137257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2025

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Feb 19, 2025

We should not emit unaligned packed field reference errors for the fake borrows that we generate during match lowering.

These fake borrows are there to ensure in borrow-checking that we don't modify the value being matched (which is why this only occurs when there's a match guard, in this case if true), but they are removed after the MIR is processed by CleanupPostBorrowck, since they're really just there to cause borrowck errors if necessary.

I modified PlaceContext::is_borrow since that's used by the packed field check:

if context.is_borrow() && util::is_disaligned(self.tcx, self.body, self.typing_env, *place)

It's only used in one other place, in the SROA optimization (by which fake borrows are removed, so it doesn't matter):

Fixes #137250

@rustbot
Copy link
Collaborator

rustbot commented Feb 19, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
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

@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 Feb 19, 2025
@compiler-errors
Copy link
Member Author

cc @RalfJung who added this check

@compiler-errors
Copy link
Member Author

r? oli-obk who reviewed it

@rustbot rustbot assigned oli-obk and unassigned petrochenkov Feb 19, 2025
@@ -1394,9 +1394,8 @@ impl PlaceContext {
pub fn is_borrow(self) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth updating the doc comment to explicitly say that only "real" borrows (that exist operationally) are considered here?

Another option might be to move the packed-place-borrow-checker to after SharedBorrow deletion, but that might be a bit too late.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I presumed that the packed checker was early on so as to avoid any optimizations getting in the way, though we shouldn't be applying any semantics-affecting optimizations until after borrowck, so perhaps this could be moved to right after post-borrowck cleanup 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll change the docs anyways.

Copy link
Member

Choose a reason for hiding this comment

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

Do fake borrows get removed somewhere within analysis MIR? As ling as we can stay on analysis MIR, it should be fine, that one should not be affected by optimizations at all, right?

@oli-obk
Copy link
Contributor

oli-obk commented Feb 21, 2025

r=me with docs. I was just being overly conservative, looking at the uses of is_borrow I don't see why we need to handle the fake borrows

@compiler-errors compiler-errors force-pushed the fake-borrow-of-packed-field branch from 44c5f7e to 0713bbc Compare February 21, 2025 17:50
@compiler-errors
Copy link
Member Author

@bors r=oli-obk rollup

@bors
Copy link
Collaborator

bors commented Feb 21, 2025

📌 Commit 0713bbc has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 21, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 21, 2025
…ked-field, r=oli-obk

Ignore fake borrows for packed field check

We should not emit unaligned packed field reference errors for the fake borrows that we generate during match lowering.

These fake borrows are there to ensure in *borrow-checking* that we don't modify the value being matched (which is why this only occurs when there's a match guard, in this case `if true`), but they are removed after the MIR is processed by `CleanupPostBorrowck`, since they're really just there to cause borrowck errors if necessary.

I modified `PlaceContext::is_borrow` since that's used by the packed field check:
https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_transform/src/check_packed_ref.rs#L40

It's only used in one other place, in the SROA optimization (by which fake borrows are removed, so it doesn't matter):
https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_dataflow/src/value_analysis.rs#L922

Fixes rust-lang#137250
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 21, 2025
…ked-field, r=oli-obk

Ignore fake borrows for packed field check

We should not emit unaligned packed field reference errors for the fake borrows that we generate during match lowering.

These fake borrows are there to ensure in *borrow-checking* that we don't modify the value being matched (which is why this only occurs when there's a match guard, in this case `if true`), but they are removed after the MIR is processed by `CleanupPostBorrowck`, since they're really just there to cause borrowck errors if necessary.

I modified `PlaceContext::is_borrow` since that's used by the packed field check:
https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_transform/src/check_packed_ref.rs#L40

It's only used in one other place, in the SROA optimization (by which fake borrows are removed, so it doesn't matter):
https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_dataflow/src/value_analysis.rs#L922

Fixes rust-lang#137250
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 21, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#136458 (Do not deduplicate list of associated types provided by dyn principal)
 - rust-lang#136474 ([`compiletest`-related cleanups 3/7] Make the distinction between sources root vs test suite sources root in compiletest less confusing)
 - rust-lang#136592 (Make sure we don't overrun the stack in canonicalizer)
 - rust-lang#136787 (Remove `lifetime_capture_rules_2024` feature)
 - rust-lang#137180 (Give `global_asm` a fake body to store typeck results, represent `sym fn` as a hir expr to fix `sym fn` operands with lifetimes)
 - rust-lang#137257 (Ignore fake borrows for packed field check)
 - rust-lang#137348 (More sophisticated span trimming for suggestions)
 - rust-lang#137399 (fix ICE in layout computation with unnormalizable const)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 22, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#136458 (Do not deduplicate list of associated types provided by dyn principal)
 - rust-lang#136474 ([`compiletest`-related cleanups 3/7] Make the distinction between sources root vs test suite sources root in compiletest less confusing)
 - rust-lang#136592 (Make sure we don't overrun the stack in canonicalizer)
 - rust-lang#136787 (Remove `lifetime_capture_rules_2024` feature)
 - rust-lang#137207 (Add #[track_caller] to Duration Div impl)
 - rust-lang#137245 (Tweak E0277 when predicate comes indirectly from ?)
 - rust-lang#137257 (Ignore fake borrows for packed field check)
 - rust-lang#137399 (fix ICE in layout computation with unnormalizable const)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit fa62fbe into rust-lang:master Feb 22, 2025
6 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Feb 22, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 22, 2025
Rollup merge of rust-lang#137257 - compiler-errors:fake-borrow-of-packed-field, r=oli-obk

Ignore fake borrows for packed field check

We should not emit unaligned packed field reference errors for the fake borrows that we generate during match lowering.

These fake borrows are there to ensure in *borrow-checking* that we don't modify the value being matched (which is why this only occurs when there's a match guard, in this case `if true`), but they are removed after the MIR is processed by `CleanupPostBorrowck`, since they're really just there to cause borrowck errors if necessary.

I modified `PlaceContext::is_borrow` since that's used by the packed field check:
https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_transform/src/check_packed_ref.rs#L40

It's only used in one other place, in the SROA optimization (by which fake borrows are removed, so it doesn't matter):
https://github.com/rust-lang/rust/blob/17c1c329a5512d718b67ef6797538b154016cd34/compiler/rustc_mir_dataflow/src/value_analysis.rs#L922

Fixes rust-lang#137250
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

‘reference to packed field is unaligned’ in nested field of match pattern
6 participants