Skip to content
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

Lower index bounds checking to PtrMetadata, this time with the right fake borrow semantics 😸 #135748

Merged
merged 3 commits into from
Jan 28, 2025

Conversation

compiler-errors
Copy link
Member

Change Rvalue::RawRef to take a RawRefKind instead of just a Mutability. Then introduce RawRefKind::FakeForPtrMetadata and use that for lowering index bounds checking to a PtrMetadata. This new RawRefKind::FakeForPtrMetadata acts like a shallow fake borrow in borrowck, which mimics the semantics of the old Rvalue::Len operation we're replacing.

We can then use this RawRefKind instead of using a span desugaring hack in CTFE.

cc @scottmcm @RalfJung

@rustbot
Copy link
Collaborator

rustbot commented Jan 19, 2025

r? @lcnr

rustbot has assigned @lcnr.
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 Jan 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jan 19, 2025

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

This PR changes Stable MIR

cc @oli-obk, @celinval, @ouz-a

Some changes occurred to the CTFE machinery

cc @rust-lang/wg-const-eval

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @vakaras

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

// though. FIXME: Do we really *need* to count this as a use?
// Could partial array tracking work off something else instead?
self.cfg.push_fake_read(block, source_info, FakeReadCause::ForIndex, place);
let const_ = Const::from_ty_const(*len_const, usize_ty, self.tcx);
Copy link
Member

@scottmcm scottmcm Jan 19, 2025

Choose a reason for hiding this comment

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

With things like #134371 and #134352, you might want something here to handle weirdness.

Not sure what the right way to do that is, but you could consider something like

Suggested change
let const_ = Const::from_ty_const(*len_const, usize_ty, self.tcx);
let const_ =
if let Some(err) = self.infcx.is_tainted_by_errors() {
Const::new_error(self.tcx, err)
} else {
Const::from_ty_const(*len_const, usize_ty, self.tcx)
};

(or whatever context is the right place to check for problems that we've already detected -- maybe self.tcx.dcx().has_errors() is better? No idea.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, this inadvertently reapplies that bug I pointed out. I can fix that too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, I don't really know what this has to do with error tainting?

Lowering this directly to a mir::Const::Ty causes ICEs even on code that's expected to pass, like when doing reads on a slice of &[(); T::ASSOC_CONST] when GCE is enabled (seetests/ui/const-generics/issues/issue-89146.rs) .

Anyways, this ICE is unrelated, and I'm going to fix it separately. I'm tracking that work in #135753. Either that one lands first, which will cause a conflict on this, or this one lands first and I'll fix the approach in this PR -- I don't think it needs to block this PR in any case.

Copy link
Member

Choose a reason for hiding this comment

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

The case that has to do with error tainting is when you get a len_const that's not actually a usize, like in the repro in #134352 where the len_const is actually a u64. There's already been an error about the type being wrong, but that doesn't keep the MIR from being very unhappy about a type mismatch after building returns a u64 constant for something that's supposed to be usize.

Copy link
Member

@scottmcm scottmcm 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 putting this together, and so quickly too! Much appreciated.

compiler/rustc_middle/src/mir/syntax.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/mir/syntax.rs Show resolved Hide resolved
compiler/rustc_const_eval/src/interpret/step.rs Outdated Show resolved Hide resolved
compiler/rustc_span/src/hygiene.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

The const-checking and interpreter changes LGTM.

@bors
Copy link
Contributor

bors commented Jan 26, 2025

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

@compiler-errors
Copy link
Member Author

r? oli-obk

@rust-log-analyzer

This comment has been minimized.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 28, 2025

@bors r=RalfJung,oli-obk

@bors
Copy link
Contributor

bors commented Jan 28, 2025

📌 Commit eeecb56 has been approved by RalfJung,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 Jan 28, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 28, 2025
…oli-obk

Lower index bounds checking to `PtrMetadata`, this time with the right fake borrow semantics 😸

Change `Rvalue::RawRef` to take a `RawRefKind` instead of just a `Mutability`. Then introduce `RawRefKind::FakeForPtrMetadata` and use that for lowering index bounds checking to a `PtrMetadata`. This new `RawRefKind::FakeForPtrMetadata` acts like a shallow fake borrow in borrowck, which mimics the semantics of the old `Rvalue::Len` operation we're replacing.

We can then use this `RawRefKind` instead of using a span desugaring hack in CTFE.

cc `@scottmcm` `@RalfJung`
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 28, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#133151 (Trim extra whitespace in fn ptr suggestion span)
 - rust-lang#133829 (Implement `AtomicT::update` & `AtomicT::try_update`)
 - rust-lang#135367 (Enable `unreachable_pub` lint in `alloc`)
 - rust-lang#135748 (Lower index bounds checking to `PtrMetadata`, this time with the right fake borrow semantics 😸)
 - rust-lang#135805 (Add missing allocator safety in alloc crate)
 - rust-lang#135886 (Document purpose of closure in from_fn.rs more clearly)
 - rust-lang#135961 (Fix 2/4 tests skipped by opt-dist)
 - rust-lang#136012 (Document powf and powi values that are always 1.0)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 21ddd7a into rust-lang:master Jan 28, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 28, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 28, 2025
Rollup merge of rust-lang#135748 - compiler-errors:len-2, r=RalfJung,oli-obk

Lower index bounds checking to `PtrMetadata`, this time with the right fake borrow semantics 😸

Change `Rvalue::RawRef` to take a `RawRefKind` instead of just a `Mutability`. Then introduce `RawRefKind::FakeForPtrMetadata` and use that for lowering index bounds checking to a `PtrMetadata`. This new `RawRefKind::FakeForPtrMetadata` acts like a shallow fake borrow in borrowck, which mimics the semantics of the old `Rvalue::Len` operation we're replacing.

We can then use this `RawRefKind` instead of using a span desugaring hack in CTFE.

cc ``@scottmcm`` ``@RalfJung``
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Feb 6, 2025
… r=compiler-errors

Implement RustcInternal for RawPtrKind

Implement `RustcInternal` for `RawPtrKind`. rust-lang#135748 introduced a `Stable` implementation [here](https://github.com/rust-lang/rust/pull/135748/files#diff-60f5e8edf69e04e89ef0c7f576363a91fa141e1db969484cef00063ed39c62e4R235).
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 6, 2025
Rollup merge of rust-lang#136590 - carolynzech:raw-ptr-kind-internal, r=compiler-errors

Implement RustcInternal for RawPtrKind

Implement `RustcInternal` for `RawPtrKind`. rust-lang#135748 introduced a `Stable` implementation [here](https://github.com/rust-lang/rust/pull/135748/files#diff-60f5e8edf69e04e89ef0c7f576363a91fa141e1db969484cef00063ed39c62e4R235).
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.

8 participants