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

Add a visibility suggestion in private-in-public errors #113983

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

nyurik
Copy link
Contributor

@nyurik nyurik commented Jul 23, 2023

This modifies the original #112540 by @aryan-debug to fix #112284

This PR introduces a new field vis_sugg to suggest the proper visibility for the error.

TODO: What should the TODO values be here? Note that not a single test anywhere has used these ???TODO??? values, so it is unclear how they can be generated. Maybe this whole match should be simplified to map Restricted(_) => "pub(crate)" ?

let vis_sugg = match self.required_visibility {
    ty::Visibility::Public => "pub",
    ty::Visibility::Restricted(vis_def_id) => {
        if vis_def_id == self.tcx.parent_module(hir_id) {
            "???TODO???"
        } else if vis_def_id.is_top_level_module() {
            "pub(crate)"
        } else {
            "???TODO???"
        }
    }
};

@rustbot
Copy link
Collaborator

rustbot commented Jul 23, 2023

r? @WaffleLapkin

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 23, 2023
@nyurik nyurik changed the title Pub in priv err Add a visibility suggestion in private-in-public errors Jul 23, 2023
@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin
Copy link
Member

r? compiler-errors

@petrochenkov
Copy link
Contributor

I don't think this change is still relevant after #113126.

@nyurik
Copy link
Contributor Author

nyurik commented Jul 24, 2023

@petrochenkov thx for the heads up! I looked through that PR (even left a comment), but I'm not certain I understand if it will cover all cases?

I see this change adds self.in_assoc_ty bool, so clearly the error will still exist if its an associated type? Is this planned to change as well? Should the suggestion I did in this PR be addapted to #113126?

if self.in_assoc_ty && !vis.is_at_least(self.required_visibility, self.tcx) {

@compiler-errors
Copy link
Member

r? petrochenkov

@petrochenkov
Copy link
Contributor

Blocked on #113126.
@rustbot blocked

@rustbot rustbot added S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 26, 2023
@bors

This comment was marked as resolved.

@petrochenkov
Copy link
Contributor

#113126 has landed.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. labels Sep 1, 2023
@Dylan-DPC
Copy link
Member

@nyurik any updates?

@nyurik
Copy link
Contributor Author

nyurik commented Nov 2, 2023

@Dylan-DPC thx for the ping, i have updated the PR, but it still has some outstanding questions that need some feedback. Thx!

@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 3, 2023
@nyurik
Copy link
Contributor Author

nyurik commented Nov 13, 2023

@petrochenkov hi, would you be the right person to review this? I would love to move forward, but it is a bit unclear. Plus @JohnCSimon was pinging the original author of this PR (before my refactoring). Hopefully the outstanding questions can be easily addressed. Thx!!

@petrochenkov
Copy link
Contributor

@petrochenkov hi, would you be the right person to review this?

Yes, but maybe a bit later, I'm busy this week.

@@ -1475,12 +1475,31 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
}
};

// FIXME: this code was adapted from the above `vis_descr` computation,
// but it's not clear if it's correct.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think fn vis_to_string should be usable here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure it will work. I tried to

  • add rustc_ast_pretty = { path = "../rustc_ast_pretty" } to Cargo.toml
  • use it with let vis_sugg = rustc_ast_pretty::pprust::vis_to_string(&self.required_visibility);

But the ty::Visibility that I have here -- enum { Default, Hidden, Protected } is not the same as the one vis_to_string expects -- a struct ast::Visibility with kind/span/tokens. Is there a way i can get the struct?

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant ty::Visibility::to_string, not vis_to_string from rustc_ast_pretty, sorry.

ty::Visibility::to_string was also called vis_to_string when this comment was written, but it was renamed in #115993.

pub descr: DiagnosticArgFromDisplay<'a>,
#[label(privacy_visibility_label)]
pub vis_span: Span,
#[suggestion(code = "", applicability = "maybe-incorrect")]
Copy link
Contributor

Choose a reason for hiding this comment

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

The suggested visibility should probably go to the code here, rather than to the suggestion message.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 24, 2023
@bors

This comment was marked as resolved.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Jan 12, 2024
@rustbot
Copy link
Collaborator

rustbot commented Jan 12, 2024

There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

You can start a rebase with the following commands:

$ # rebase
$ git rebase -i master
$ # delete any merge commits in the editor that appears
$ git push --force-with-lease

The following commits are merge commits:

@dtolnay dtolnay removed the has-merge-commits PR has merge commits, merge with caution. label Jan 21, 2024
@Dylan-DPC
Copy link
Member

@nyurik any updates on replying to the changes requested in the review and implement them accordingly?

Copy link
Contributor Author

@nyurik nyurik left a comment

Choose a reason for hiding this comment

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

Thx for the ping! I am still interested in this PR, but got a bit stuck on implementing it

@@ -1475,12 +1475,31 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
}
};

// FIXME: this code was adapted from the above `vis_descr` computation,
// but it's not clear if it's correct.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure it will work. I tried to

  • add rustc_ast_pretty = { path = "../rustc_ast_pretty" } to Cargo.toml
  • use it with let vis_sugg = rustc_ast_pretty::pprust::vis_to_string(&self.required_visibility);

But the ty::Visibility that I have here -- enum { Default, Hidden, Protected } is not the same as the one vis_to_string expects -- a struct ast::Visibility with kind/span/tokens. Is there a way i can get the struct?

@bors
Copy link
Contributor

bors commented Feb 28, 2024

☔ The latest upstream changes (presumably #121489) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest code correction for E0446 - private type in public interface
10 participants