Note scoping differences between *x and *x.deref() , and a[b] and *a.index(b)#2073
Conversation
|
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 |
This comment was marked as resolved.
This comment was marked as resolved.
|
Reminder, once the PR becomes ready for a review, use |
d8c164b to
ddc1d58
Compare
|
@rustbot ready I'll open a separate PR to add text for |
8237642 to
cefcfac
Compare
*x and *x.deref()*x and *x.deref() as well as a[b] and *a.index(b)
src/expressions/array-expr.md
Outdated
| @@ -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. | |||
There was a problem hiding this comment.
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:
| 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.
There was a problem hiding this comment.
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?
cefcfac to
004be03
Compare
This comment has been minimized.
This comment has been minimized.
*x and *x.deref() as well as a[b] and *a.index(b)*x and *x.deref() , and a[b] and *a.index(b)
004be03 to
8e4d81a
Compare
|
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. |
| let y = &*std::ops::Index::index(&vec![()], 0); // ERROR | ||
| # y; |
There was a problem hiding this comment.
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:
| 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; // OKIs it maybe that a bare identifier statement is unusual?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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)
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)
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.