Skip to content

Don't emit no-op &T → &T reborrows - #164014

Open
Jules-Bertholet wants to merge 4 commits into
rust-lang:mainfrom
Jules-Bertholet:no-noop-reborrows
Open

Jules-Bertholet wants to merge 4 commits into
rust-lang:mainfrom
Jules-Bertholet:no-noop-reborrows

Conversation

@Jules-Bertholet

@Jules-Bertholet Jules-Bertholet commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Except inside closures, where we need to keep capture inference stable. Gets rid of MIR bloat, and improves diagnostics.

See also #163717 (would allow doing this even inside closures), #163494 (would allow doing this for mutable references outside closures).

r? compiler

@rustbot label A-coercions

@rustbot

rustbot commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 9, 2026
@rustbot rustbot added the A-coercions Area: implicit and explicit `expr as Type` coercions label Oct 9, 2026
where
T: TrivialClone,
{
#[inline]

@Jules-Bertholet Jules-Bertholet Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To make tests/codegen-llvm/lib-optimizations/append-elements.rs happy. That test is really fragile…

View changes since the review

@jieyouxu

jieyouxu commented Oct 9, 2026

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 9, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 9, 2026
Don't emit no-op `&T` → `&T` reborrows
@rust-bors

rust-bors Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: d77c4b4 (d77c4b4f551a87fc763f35c94b4177493a25ab83)
Base parent: 0f6e5bf (0f6e5bf4c63a0ab336cc62de446b77123effaffc)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (d77c4b4): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.7% [-3.9%, -0.1%] 175
Improvements ✅
(secondary)
-1.2% [-7.0%, -0.1%] 151
All ❌✅ (primary) -0.7% [-3.9%, 0.2%] 176

Max RSS (memory usage)

Results (primary -2.1%, secondary -2.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.1% [2.1%, 4.2%] 2
Improvements ✅
(primary)
-2.1% [-6.2%, -0.8%] 16
Improvements ✅
(secondary)
-3.7% [-6.7%, -0.7%] 16
All ❌✅ (primary) -2.1% [-6.2%, -0.8%] 16

Cycles

Results (primary -2.7%, secondary -4.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
7.1% [7.1%, 7.1%] 1
Improvements ✅
(primary)
-2.7% [-3.1%, -2.2%] 5
Improvements ✅
(secondary)
-5.4% [-8.9%, -1.8%] 15
All ❌✅ (primary) -2.7% [-3.1%, -2.2%] 5

Binary size

Results (primary 0.1%, secondary -0.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.3% [0.1%, 1.1%] 27
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 1
Improvements ✅
(primary)
-0.3% [-1.0%, -0.0%] 22
Improvements ✅
(secondary)
-0.2% [-0.5%, -0.1%] 12
All ❌✅ (primary) 0.1% [-1.0%, 1.1%] 49

Bootstrap: 486.533s -> 486.402s (-0.03%)
Artifact size: 406.44 MiB -> 406.63 MiB (0.05%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 9, 2026
@theemathas

Copy link
Copy Markdown
Contributor

Does this have implications on what Miri can catch?

@Jules-Bertholet

Copy link
Copy Markdown
Contributor Author

Does this have implications on what Miri can catch?

It shouldn't. Miri does retags on every typed copy.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

Except inside closures, where we need to keep capture inference stable.
Gets rid of MIR bloat, and improves diagnostics.
@rustbot

rustbot commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@adwinwhite adwinwhite left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the intention of this PR is to skip more no-op reborrows?
Some explanation on what the "more" part is would be awesome.
As someone who's not super familiar with coercions, this isn't too obvious :>

View changes since this review

// FIXME: Ideally we would also skip `&mut` -> `&mut` reborrow,
// and handle that in MIR.
let can_skip_noop_reborrow = mutbl_b.is_not()
&& if self.tcx.is_closure_like(self.body_def_id.to_def_id()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

style: the large if expr is kinda difficult to read as a conjunction component?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've rewritten this if, hopefully it is more clear now.

Comment on lines +529 to +532
// FIXME: Inside a closure, reborrows can affect capture inference.
// So we have to keep the existing elision behavior stable.
// Ideally we wouldn't have to do this, it makes lifetime
// annotations affect codegen

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A little bit confused here and also about other similar comments.
Immutable reborrow doesn't affect capture inference since they're copy anyway?

@Jules-Bertholet Jules-Bertholet Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The relevant test is https://github.com/rust-lang/rust/blob/701ce39ddc3b02efdf5146fd76c861b06c52aeb3/tests/ui/coercion/structural_identity_dependent_reborrows.rs.

Consider the following example:

let x: &u8 = &42;
let closure = || some_function(/* Implicit `&*` reborrow here? */ x);

If there is no reborrow, we capture x by reference. But if there is one, with edition 2021 precise capturing, we capture *x by reference, which is equivalent to capturing x by value.

Comment thread compiler/rustc_hir_typeck/src/coercion.rs Outdated
@theemathas

This comment has been minimized.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-coercions Area: implicit and explicit `expr as Type` coercions S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library 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