Skip to content

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

Merged
ehuss merged 2 commits intorust-lang:masterfrom
dianne:deref-desugar-scoping-difference
Mar 23, 2026
Merged

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

Conversation

@dianne
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment on lines +100 to +101
let y = &*std::ops::Index::index(&vec![()], 0); // ERROR
# y;
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'm curious about the thinking for hiding the "use" here? My thinking is that with it hidden it could be confusing since assigning to y isn't an error, it's not until the later use. For example, it could be:

Suggested change
let y = &*std::ops::Index::index(&vec![()], 0); // ERROR
# y;
let y = &*std::ops::Index::index(&vec![()], 0);
y; // ERROR

And to contrast in the other example, it could be:

let x = &vec![()][0];
x; // OK

Is it maybe that a bare identifier statement is unusual?

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 was just going for stylistic consistency with the examples in https://doc.rust-lang.org/nightly/reference/destructors.html#r-destructors.scope.lifetime-extension; they also hide usages and put the ok/error comments a line early, relying on the text to clarify. There's a lot more examples there, so there may be more of an argument to made for brevity. I wouldn't be opposed to making the usages explicit here (or even there too) for clarity.

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.

Ok, I'm not going to block on that. If you (or someone else) see an improvement that could be made to all those examples, we can tackle that in a separate PR.

Copy link
Copy Markdown
Contributor

@ehuss ehuss left a comment

Choose a reason for hiding this comment

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

@ehuss ehuss added this pull request to the merge queue Mar 23, 2026
Merged via the queue into rust-lang:master with commit b07b925 Mar 23, 2026
6 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Mar 23, 2026
@dianne dianne deleted the deref-desugar-scoping-difference branch March 24, 2026 11:10
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 6, 2026
Update books

## rust-lang/reference

6 commits in 7446bf9697c95d155eef33c6a9d91fbd29a5e359..d2715c07e9dd9839c0c7675ecfa18bec539a6ee9
2026-03-31 18:18:16 UTC to 2026-03-23 21:49:16 UTC

- Guarantee size and alignment of more integer primitives (rust-lang/reference#2205)
- [type layout] usize and isize have the same size and alignment (rust-lang/reference#2200)
- Update reference for attribute order changes (rust-lang/reference#2213)
- Fix note for non_exhaustive enum read (rust-lang/reference#2211)
- Update link_name duplicates note (rust-lang/reference#2216)
- Note scoping differences between `*x` and `*x.deref()` , and `a[b]` and `*a.index(b)` (rust-lang/reference#2073)

## rust-lang/rust-by-example

8 commits in 5383db524711c0c9c43c3ca9e5e706089672ed6a..b31e3b8da01eeba0460f86a52a55af82709fadf5
2026-04-01 11:59:18 UTC to 2026-03-30 12:17:12 UTC

- fix: correct into_iter() example to compile properly (rust-lang/rust-by-example#2001)
- fix: correct destruction order comments in Drop TempFile example (rust-lang/rust-by-example#2002)
- fix: replace confusing 'module' with 'item' in formatted print docs (rust-lang/rust-by-example#2003)
- fix: clarify misleading 'Error' comment in print_display example (rust-lang/rust-by-example#2004)
- fix: clarify comment about tuple struct field access in testcase_list (rust-lang/rust-by-example#2005)
- fix: correct iter/into_iter type comments in iter_find example (rust-lang/rust-by-example#2006)
- fix: replace year-to-days conversion with miles-to-km in newtype example (rust-lang/rust-by-example#2007)
- Add a pub use example (rust-lang/rust-by-example#2000)
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 6, 2026
Update books

## rust-lang/reference

6 commits in 7446bf9697c95d155eef33c6a9d91fbd29a5e359..d2715c07e9dd9839c0c7675ecfa18bec539a6ee9
2026-03-31 18:18:16 UTC to 2026-03-23 21:49:16 UTC

- Guarantee size and alignment of more integer primitives (rust-lang/reference#2205)
- [type layout] usize and isize have the same size and alignment (rust-lang/reference#2200)
- Update reference for attribute order changes (rust-lang/reference#2213)
- Fix note for non_exhaustive enum read (rust-lang/reference#2211)
- Update link_name duplicates note (rust-lang/reference#2216)
- Note scoping differences between `*x` and `*x.deref()` , and `a[b]` and `*a.index(b)` (rust-lang/reference#2073)

## rust-lang/rust-by-example

8 commits in 5383db524711c0c9c43c3ca9e5e706089672ed6a..b31e3b8da01eeba0460f86a52a55af82709fadf5
2026-04-01 11:59:18 UTC to 2026-03-30 12:17:12 UTC

- fix: correct into_iter() example to compile properly (rust-lang/rust-by-example#2001)
- fix: correct destruction order comments in Drop TempFile example (rust-lang/rust-by-example#2002)
- fix: replace confusing 'module' with 'item' in formatted print docs (rust-lang/rust-by-example#2003)
- fix: clarify misleading 'Error' comment in print_display example (rust-lang/rust-by-example#2004)
- fix: clarify comment about tuple struct field access in testcase_list (rust-lang/rust-by-example#2005)
- fix: correct iter/into_iter type comments in iter_find example (rust-lang/rust-by-example#2006)
- fix: replace year-to-days conversion with miles-to-km in newtype example (rust-lang/rust-by-example#2007)
- Add a pub use example (rust-lang/rust-by-example#2000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants