Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: Cleanup visibility resolution for enum variants and trait items #80762

Merged
merged 2 commits into from
Feb 11, 2021

Conversation

petrochenkov
Copy link
Contributor

by always delegating it to fn resolve_visibility.

Also remove some special treatment of visibility on enum variants and trait items remaining from pre-pub(restricted) times.

@rust-highfive
Copy link
Collaborator

r? @varkor

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 6, 2021
@petrochenkov
Copy link
Contributor Author

cc @camelid

@camelid camelid added A-resolve Area: Path resolution A-visibility Area: Visibility / privacy. labels Jan 6, 2021
@bors

This comment has been minimized.

mod rank {
pub use self::Professor::*;
//~^ ERROR enum is private and its variants cannot be re-exported
//~^ ERROR glob import doesn't reexport anything
Copy link
Member

Choose a reason for hiding this comment

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

Is this a change in behaviour, or is the lint taking precedence over the "enum is private" error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a change in behavior, now glob import from enum with private variants behaves identically to a glob import from a module with private items.
Previously, in the world without pub(restricted), this required special logic and separate error.

Copy link
Member

Choose a reason for hiding this comment

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

This definitely seems like better behaviour, but is this something that would need to undergo a lang team FCP, or is this a change that has already been discussed?

Copy link
Contributor Author

@petrochenkov petrochenkov Jan 9, 2021

Choose a reason for hiding this comment

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

Maybe.
When the error was added in 8f359d5 (as a part of hardening private-in-public errors), the glob case wasn't really discussed.

The glob error was added pretty conservatively to seal any possible sources of leakage, but even then it was inconsistent with behavior of other glob imports.

Copy link
Member

Choose a reason for hiding this comment

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

Let's cc them just to make sure, though it's likely this isn't large enough to warrant an FCP.

@varkor
Copy link
Member

varkor commented Jan 9, 2021

@rust-lang/lang: this pull request changes the behaviour of glob imports so that enums with private variants behave identically to modules with private items (see #80762 (comment)). Is this change small enough not to warrant an FCP? If not, could someone kick one off?

@petrochenkov petrochenkov added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 9, 2021
m-ou-se added a commit to m-ou-se/rust that referenced this pull request Jan 16, 2021
…ewjasper

resolve: Simplify collection of traits in scope

"Traits in scope" for a given location are collected by walking all scopes in type namespace, collecting traits in them and pruning traits that don't have an associated item with the given name and namespace.

Previously we tried to prune traits using some kind of hygienic resolution for associated items, but that was complex and likely incorrect, e.g. in rust-lang#80762 correction to visibilites of trait items caused some traits to not be in scope anymore.
I previously had some comments and concerns about this in rust-lang#65351.

In this PR we are doing some much simpler pruning based on `Symbol` and `Namespace` comparisons, it should be enough to throw away 99.9% of unnecessary traits.
It is not necessary for pruning to be precise because for trait aliases, for example, we don't do any pruning at all, and precise hygienic resolution for associated items needs to be done in typeck anyway.

The somewhat unexpected effect is that trait imports introduced by macros 2.0 now bring traits into scope due to the removed hygienic check on associated item names.
I'm not sure whether it is desirable or not, but I think it's acceptable for now.
The old check was certainly incorrect because macros 2.0 did bring trait aliases into scope.
If doing this is not desirable, then we should come up with some other way to avoid bringing traits from macros 2.0 into scope, that would accommodate for trait aliases as well.

---

The PR also contains a couple of pure refactorings
- Scope walk is done by using `visit_scopes` instead of a hand-rolled version.
- Code is restructured to accomodate for rustdoc that also wants to query traits in scope, but doesn't want to filter them by associated items at all.

r? `@matthewjasper`
m-ou-se added a commit to m-ou-se/rust that referenced this pull request Jan 17, 2021
…ewjasper

resolve: Simplify collection of traits in scope

"Traits in scope" for a given location are collected by walking all scopes in type namespace, collecting traits in them and pruning traits that don't have an associated item with the given name and namespace.

Previously we tried to prune traits using some kind of hygienic resolution for associated items, but that was complex and likely incorrect, e.g. in rust-lang#80762 correction to visibilites of trait items caused some traits to not be in scope anymore.
I previously had some comments and concerns about this in rust-lang#65351.

In this PR we are doing some much simpler pruning based on `Symbol` and `Namespace` comparisons, it should be enough to throw away 99.9% of unnecessary traits.
It is not necessary for pruning to be precise because for trait aliases, for example, we don't do any pruning at all, and precise hygienic resolution for associated items needs to be done in typeck anyway.

The somewhat unexpected effect is that trait imports introduced by macros 2.0 now bring traits into scope due to the removed hygienic check on associated item names.
I'm not sure whether it is desirable or not, but I think it's acceptable for now.
The old check was certainly incorrect because macros 2.0 did bring trait aliases into scope.
If doing this is not desirable, then we should come up with some other way to avoid bringing traits from macros 2.0 into scope, that would accommodate for trait aliases as well.

---

The PR also contains a couple of pure refactorings
- Scope walk is done by using `visit_scopes` instead of a hand-rolled version.
- Code is restructured to accomodate for rustdoc that also wants to query traits in scope, but doesn't want to filter them by associated items at all.

r? ``@matthewjasper``
m-ou-se added a commit to m-ou-se/rust that referenced this pull request Jan 17, 2021
…ewjasper

resolve: Simplify collection of traits in scope

"Traits in scope" for a given location are collected by walking all scopes in type namespace, collecting traits in them and pruning traits that don't have an associated item with the given name and namespace.

Previously we tried to prune traits using some kind of hygienic resolution for associated items, but that was complex and likely incorrect, e.g. in rust-lang#80762 correction to visibilites of trait items caused some traits to not be in scope anymore.
I previously had some comments and concerns about this in rust-lang#65351.

In this PR we are doing some much simpler pruning based on `Symbol` and `Namespace` comparisons, it should be enough to throw away 99.9% of unnecessary traits.
It is not necessary for pruning to be precise because for trait aliases, for example, we don't do any pruning at all, and precise hygienic resolution for associated items needs to be done in typeck anyway.

The somewhat unexpected effect is that trait imports introduced by macros 2.0 now bring traits into scope due to the removed hygienic check on associated item names.
I'm not sure whether it is desirable or not, but I think it's acceptable for now.
The old check was certainly incorrect because macros 2.0 did bring trait aliases into scope.
If doing this is not desirable, then we should come up with some other way to avoid bringing traits from macros 2.0 into scope, that would accommodate for trait aliases as well.

---

The PR also contains a couple of pure refactorings
- Scope walk is done by using `visit_scopes` instead of a hand-rolled version.
- Code is restructured to accomodate for rustdoc that also wants to query traits in scope, but doesn't want to filter them by associated items at all.

r? ```@matthewjasper```
@bors

This comment has been minimized.

Special treatment like this was necessary before `pub(restricted)` had been implemented and only two visibilities existed - `pub` and non-`pub`.
Now it's no longer necessary and the desired behavior follows from `pub(restricted)`-style visibilities naturally assigned to enum variants and trait items.
@petrochenkov
Copy link
Contributor Author

No response for a month, I'll interpret this as a lack of interest.

The current rule is a result of an ad hoc compiler future-proofing rather than a T-lang decision, and this PR removes it in favor of a more general non ad hoc rule, so I think we can just land it.

@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Feb 10, 2021
@varkor
Copy link
Member

varkor commented Feb 10, 2021

I'm inclined to agree.

@bors r+

@bors
Copy link
Contributor

bors commented Feb 10, 2021

📌 Commit 8e74842 has been approved by varkor

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 10, 2021
@bors
Copy link
Contributor

bors commented Feb 10, 2021

⌛ Testing commit 8e74842 with merge 178108b...

@bors
Copy link
Contributor

bors commented Feb 11, 2021

☀️ Test successful - checks-actions
Approved by: varkor
Pushing 178108b to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 11, 2021
@bors bors merged commit 178108b into rust-lang:master Feb 11, 2021
@rustbot rustbot added this to the 1.52.0 milestone Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-resolve Area: Path resolution A-visibility Area: Visibility / privacy. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants