reference icon indicating copy to clipboard operation
reference copied to clipboard

Add a note about scoping differences between `*x` and `*x.deref()` as well as `a[b]` and `*a.index(b)`

Open dianne opened this issue 2 months ago • 4 comments

The section on * operators states that an overloaded *x is equivalent in immutable place expression contexts to *std::ops::Deref::deref(&x) and that it is equivalent in mutable place expression contexts to *std::ops::DerefMut::deref_mut(&mut x). That isn't quite true for their temporary scopes, so this PR adds a note including an example where the difference affects static semantics.

There's a similar section for index expressions defining a[b] in terms of associated function calls, so I've added a comparable example for temporary scoping differences there as well.

dianne avatar Nov 03 '25 10:11 dianne

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 avatar Nov 04 '25 00:11 dianne

@rustbot author

Looking at this again, it should probably be framed in terms of static semantics rather than runtime semantics, since one of the examples makes an unusable binding. I'll split that part off into a compile_fail test and remove the assertions.

While I'm at it, the wording of the * operator section should probably account for Box. It's covered in lang.types.box.deref; I'll add a reference to that.

dianne avatar Nov 04 '25 03:11 dianne

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

rustbot avatar Nov 04 '25 03:11 rustbot

@rustbot ready

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

dianne avatar Nov 04 '25 08:11 dianne