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

Don't capture &[T; N] when contents isn't read #112636

Merged
merged 1 commit into from Jun 18, 2023

Conversation

clubby789
Copy link
Contributor

Fixes the check in #111831
Fixes #112607, although I decided to test the root cause rather than including the example in the issue as a test.
cc @BoxyUwU

@rustbot
Copy link
Collaborator

rustbot commented Jun 14, 2023

r? @wesleywiser

(rustbot has picked a reviewer for you, use r? to override)

@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 Jun 14, 2023
@@ -443,7 +443,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
if matches!((lhs, wild, rhs), (&[], Some(_), &[]))
// Arrays have a statically known size, so
// there is no need to read their length
|| discr_place.place.base_ty.is_array()
|| discr_place.place.base_ty.peel_refs().is_array()
Copy link
Member

Choose a reason for hiding this comment

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

should we be doing discr_place.place.ty() insead of checking base_ty? Idk if that's right, will need checking...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't able to produce any incorrect behaviour with this but ty() sounds like the better choice so I'll switch to that

@BoxyUwU
Copy link
Member

BoxyUwU commented Jun 15, 2023

Does this make matching like: if let Foo([_, _, _]) = &Foo(&[10; 3]) not do a read?

@clubby789
Copy link
Contributor Author

clubby789 commented Jun 15, 2023

Does this make matching like: if let Foo([_, _, _]) = &Foo(&[10; 3]) not do a read?

The closure capture analysis of this

struct Foo<T>(T);

fn main() {
    let f = &Foo(&[10; 3]);
    let c = #[rustc_capture_analysis]
    || {
        match f {
            Foo([_, _, _]) => ()
        }
    };
    c();
}

is the same between current nightly and this PR (ImmBorrow of f). The ty() of this place is Foo so we fall into the else branch

EDIT: Actually, this check is run specifically for match, not if let. The behaviour is still the same with a match though

@clubby789
Copy link
Contributor Author

Actually, I think this should be using place.place.ty() (as the other arms do), not discr_place.place.ty(). However, that does change the behaviour of the above example as f is no longer captured, and makes it inconsistent with the behaviour of if let

@clubby789
Copy link
Contributor Author

My mistake 😓 This example does not capture f on stable. #111831 regressed it, so I'll apply the fix

@cjgillot
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jun 18, 2023

📌 Commit e72618a has been approved by cjgillot

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 Jun 18, 2023
@bors
Copy link
Contributor

bors commented Jun 18, 2023

⌛ Testing commit e72618a with merge 9397862...

@bors
Copy link
Contributor

bors commented Jun 18, 2023

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 9397862 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 18, 2023
@bors bors merged commit 9397862 into rust-lang:master Jun 18, 2023
12 checks passed
@rustbot rustbot added this to the 1.72.0 milestone Jun 18, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9397862): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 656.279s -> 656.699s (0.06%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

ICE: expected usize, got Const
8 participants