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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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.

Comment on lines +213 to +214
// We have no type corresponding to a shallow borrow, so use
// `&` as an approximation.
Copy link
Member

Choose a reason for hiding this comment

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

This comment looks like it still needs updating.

pub enum RawPtrKind {
Mut,
Not,
FakeForPtrMetadata,
Copy link
Member

Choose a reason for hiding this comment

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

Please add a doc comment explaining this variant.

@@ -250,8 +254,13 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
let src = self.eval_place(place)?;
let place = self.force_allocation(&src)?;
let mut val = ImmTy::from_immediate(place.to_ref(self), dest.layout);
if !place_base_raw {
if !place_base_raw
&& span.desugaring_kind() != Some(DesugaringKind::IndexBoundsCheckReborrow)
Copy link
Member

Choose a reason for hiding this comment

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

Wasn't the point to get rid of this?

Copy link
Member Author

Choose a reason for hiding this comment

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

You are reviewing the wrong code

Copy link
Member Author

Choose a reason for hiding this comment

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

That is to say, you're reviewing the un-revert commit, not the functional changes on top.

compiler/rustc_span/src/hygiene.rs Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

6 participants