Skip to content

Integrate privacy into autoderef loop with overloaded deref #12808

Closed
@nikomatsakis

Description

@nikomatsakis

Once PR #12610 lands, the autoderef loop will be generalized to permit dereferencing through user-defined structures that implement the Deref trait. As such, the termination condition becomes less clear around privacy. Currently, when searching for fields, we autoderef until finding a struct and then search for a field. In PR #12610, we will continue searching if no field with the desired name is found. Unfortunately, the PR does not consider privacy, and thus adding a field to a smart pointer can shadow fields in the referent. The same applies to methods. This is suboptimal.

The desired behavior is that private fields and methods are ignored as part of the autoderef loop, so long as the expression being autoderef'd is not part of the module where the field/method was defined. This is a relatively simple check: if the field/method is declared private, you find the module M containing the declaration. You then check the whether current module N is a equal to M or a submodule of M. If not, you ignore the field/method.

For better error messages, it would be nice to at least track whether a private field/method with a suitable name was seen, so that we can report an error like "no accessible field f found (note that private fields were found)" or something like that.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions