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

On privacy error caused by private reexport, use spans to show the use chain #67951

Closed
wants to merge 1 commit into from

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Jan 6, 2020

Fix #63942, fix #57619. Partially address #42909.

@rust-highfive
Copy link
Collaborator

r? @zackmdavis

(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, 2020
src/librustc/infer/error_reporting/mod.rs Outdated Show resolved Hide resolved
Comment on lines 2628 to 2630
self.definitions
.def_path(def_id.index)
.to_string_no_crate(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@petrochenkov would you have some advice on how to get a better suited source for a usable path string in resolve?

Copy link
Contributor

Choose a reason for hiding this comment

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

There are zero guarantees that a direct path won't have private segments, especially given that module structures with facades are pretty popular.
So trying to guess and use full paths here means inviting false positives.

I'd rather keep the status quo and say only the name.
(In this case the changes in src/librustc/hir/map/definitions.rs can be removed as well, those strings are not user-facing otherwise and are used only for debugging.)

Comment on lines 2619 to 2621
// FIXME: we should verify that this def is actually
// reachable from the user's crate, as the parent modules
// of this ADT might be private.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@petrochenkov would you have some advice on how to properly check the visibility of a given ADT here? I'm guessing I would have to ferry some scope information in PrivacyError.

Copy link
Contributor

Choose a reason for hiding this comment

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

This issue doesn't apply to the simpler version of this PR (#67951 (comment)) which I'd like to see implemented first.
I'm not sure we'll need anything beyond that.

src/test/ui/imports/issue-55884-2.stderr Outdated Show resolved Hide resolved
src/test/ui/export-tag-variant.stderr Outdated Show resolved Hide resolved
src/librustc_resolve/lib.rs Outdated Show resolved Hide resolved
@petrochenkov petrochenkov self-assigned this Jan 8, 2020
Copy link
Member

@varkor varkor left a comment

Choose a reason for hiding this comment

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

I really like these more informative errors! They could just do with a little tweaking for readability.

src/librustc_resolve/lib.rs Outdated Show resolved Hide resolved
src/test/ui/error-codes/E0603.stderr Outdated Show resolved Hide resolved
src/test/ui/extern/extern-crate-visibility.stderr Outdated Show resolved Hide resolved
src/test/ui/imports/issue-55884-2.stderr Outdated Show resolved Hide resolved
@petrochenkov
Copy link
Contributor

r? @petrochenkov

@petrochenkov
Copy link
Contributor

I'm not sure that reporting the full reexports chains is useful.
The important thing to report here is that the import we are trying to access is private.

So, I'd replace this:

error[E0603]: module `bar` is private
  --> $DIR/shadowed-use-visibility.rs:9:14
   |
LL |     use foo::bar::f as g;
   |              ^^^ this module is private
   |
note: used restricted re-export of `bar`
  --> $DIR/shadowed-use-visibility.rs:4:9
   |
LL |     use foo as bar;
   |         ^^^^^^^^^^
note: re-exported module `bar` is declared here
  --> $DIR/shadowed-use-visibility.rs:1:1
   |
LL | / mod foo {
LL | |     pub fn f() {}
LL | |
LL | |     use foo as bar;
LL | |     pub use self::f as bar;
LL | | }
   | |_^

with this

error[E0603]: module import `bar` is private
  --> $DIR/shadowed-use-visibility.rs:9:14
   |
LL |     use foo::bar::f as g;
   |              ^^^ this import is private
   |
note: the module import `bar` is declared here
  --> $DIR/shadowed-use-visibility.rs:4:9
   |
LL |     use foo as bar;
   |         ^^^^^^^^^^

This would be a pretty minimal change to the code existing on master.
This PR is trying to be too smart an do too much in a single step, so it's hard to see what is essential and what can be removed to keep the code simple.

@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 Jan 11, 2020
@estebank
Copy link
Contributor Author

The important thing to report here is that the import we are trying to access is private.

The concern I have is that by only stating "you're importing something private, and this is the private re-export" and nothing else we don't give users a chance of changing their use statement. By pointing the whole chain of re-exports all the way to the original definition, we might be giving a lot of unnecessary information, but we're giving the user a good idea of whether they want to import the original struct directly, or one of the public reexports. An option would be to only show the chain of public re-export and hide the private ones, but that "inconsistency" on what we show could be confusing.

@petrochenkov
Copy link
Contributor

I'd still want to see the diff between the basic version and what this PR does, so I made #68153.

@petrochenkov
Copy link
Contributor

Blocked on #68153 and #67951 (comment).

@petrochenkov petrochenkov added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 12, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jan 16, 2020
resolve: Point at the private item definitions in privacy errors

A basic version of rust-lang#67951.
r? @estebank
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jan 16, 2020
resolve: Point at the private item definitions in privacy errors

A basic version of rust-lang#67951.
r? @estebank
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jan 17, 2020
resolve: Point at the private item definitions in privacy errors

A basic version of rust-lang#67951.
r? @estebank
@bors

This comment has been minimized.

@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-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Jan 27, 2020
@Dylan-DPC-zz
Copy link

@estebank this is unblocked now

…se` chain

Use full path for direct `use` of ADT instead of private re-export
Point at enum defs and  modules on private re-exports
Use span notes to denote order
Account for `use` of private `extern crate foo;`
@estebank
Copy link
Contributor Author

@Dylan-DPC thanks for the ping.

@petrochenkov rebased and squashed on top of latest master, the diff is much smaller now.

--> $DIR/issue-55884-2.rs:6:13
|
LL | pub use options::*;
| ^^^^^^^^^^
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 think this clarification in particular will be incredibly useful when trying to understand these errors.

@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 Feb 18, 2020
@petrochenkov
Copy link
Contributor

I will get to this next weekend.

@petrochenkov
Copy link
Contributor

I'll close this in favor of #69811 which does roughly the same thing.

Centril added a commit to Centril/rust that referenced this pull request Mar 10, 2020
resolve: Print import chains on privacy errors

A part of rust-lang#67951 that doesn't require hacks.
r? @estebank
Centril added a commit to Centril/rust that referenced this pull request Mar 16, 2020
resolve: Print import chains on privacy errors

A part of rust-lang#67951 that doesn't require hacks.
r? @estebank
Centril added a commit to Centril/rust that referenced this pull request Mar 17, 2020
resolve: Print import chains on privacy errors

A part of rust-lang#67951 that doesn't require hacks.
r? @estebank
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 17, 2020
resolve: Print import chains on privacy errors

A part of rust-lang#67951 that doesn't require hacks.
r? @estebank
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
8 participants