Skip to content

Note scoping differences between *x and *x.deref() , and a[b] and *a.index(b)#2073

Open
dianne wants to merge 2 commits intorust-lang:masterfrom
dianne:deref-desugar-scoping-difference
Open

Note scoping differences between *x and *x.deref() , and a[b] and *a.index(b)#2073
dianne wants to merge 2 commits intorust-lang:masterfrom
dianne:deref-desugar-scoping-difference

Conversation

@dianne
Copy link
Contributor

@dianne dianne commented Nov 3, 2025

The equivalences between the overloaded operator expressions and their associated function call forms don't hold in the presence of temporary lifetime extension; this adjusts the rules accordingly and adds examples.

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Nov 3, 2025
@dianne
Copy link
Contributor Author

dianne commented Nov 4, 2025

The sections on field access expressions and tuple indexing expressions may also benefit from similar examples or links to that example, in case it needs clarifying that the way they autoderef is scoped more like repeatedly applying * than repeatedly applying a deref method. The autoderef section on the field access expressions page is in line with that, but doesn't clarify. The tuple indexing expressions section doesn't account for autoderef at all, so it would need to be reworked first.

@dianne

This comment was marked as resolved.

@rustbot rustbot added S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. and removed S-waiting-on-review Status: The marked PR is awaiting review from a maintainer labels Nov 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 4, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@dianne dianne force-pushed the deref-desugar-scoping-difference branch from d8c164b to ddc1d58 Compare November 4, 2025 08:22
@dianne
Copy link
Contributor Author

dianne commented Nov 4, 2025

@rustbot ready

I'll open a separate PR to add text for Box to the * operator expression documentation.

@rustbot rustbot added S-waiting-on-review Status: The marked PR is awaiting review from a maintainer and removed S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. labels Nov 4, 2025
@dianne dianne force-pushed the deref-desugar-scoping-difference branch from 8237642 to cefcfac Compare November 14, 2025 08:22
@dianne dianne changed the title Add a note about scoping differences between *x and *x.deref() Add a note about scoping differences between *x and *x.deref() as well as a[b] and *a.index(b) Nov 14, 2025
@@ -90,6 +90,30 @@ r[expr.array.index.trait]
For other types an index expression `a[b]` is equivalent to `*std::ops::Index::index(&a, b)`, or `*std::ops::IndexMut::index_mut(&mut a, b)` in a mutable place expression context.
Copy link
Contributor

Choose a reason for hiding this comment

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

When looking at this, we felt that this probably shouldn't be part of a note, but instead should be incorporated into the rule itself. The "is equivalent to" is not entirely true due to this exception. How about just adding this text here:

Suggested change
For other types an index expression `a[b]` is equivalent to `*std::ops::Index::index(&a, b)`, or `*std::ops::IndexMut::index_mut(&mut a, b)` in a mutable place expression context.
For other types an index expression `a[b]` is equivalent to `*std::ops::Index::index(&a, b)`, or `*std::ops::IndexMut::index_mut(&mut a, b)` in a mutable place expression context, except that when the index expression undergoes [temporary lifetime extension], the indexed expression `a` also has its temporary lifetime extended.

and then include the rust example as you have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems good to me! I couldn't figure out a way to keep the link to destructors.scope.lifetime-extension.sub-expressions without it getting overly cluttered, but maybe it's fine to leave it out?

@dianne dianne force-pushed the deref-desugar-scoping-difference branch from cefcfac to 004be03 Compare March 18, 2026 07:09
@rustbot

This comment has been minimized.

@dianne dianne changed the title Add a note about scoping differences between *x and *x.deref() as well as a[b] and *a.index(b) Note scoping differences between *x and *x.deref() , and a[b] and *a.index(b) Mar 18, 2026
@dianne dianne force-pushed the deref-desugar-scoping-difference branch from 004be03 to 8e4d81a Compare March 18, 2026 07:17
@rustbot
Copy link
Collaborator

rustbot commented Mar 18, 2026

This PR was rebased onto a different master 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.

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

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants