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

Account for sealed traits in privacy and trait bound errors #112686

Merged
merged 2 commits into from
Jun 22, 2023

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Jun 16, 2023

On trait bound errors caused by super-traits, identify if the super-trait is publicly accessibly and if not, explain "sealed traits".

error[E0277]: the trait bound `S: Hidden` is not satisfied
  --> $DIR/sealed-trait-local.rs:17:20
   |
LL | impl a::Sealed for S {}
   |                    ^ the trait `Hidden` is not implemented for `S`
   |
note: required by a bound in `Sealed`
  --> $DIR/sealed-trait-local.rs:3:23
   |
LL |     pub trait Sealed: self::b::Hidden {
   |                       ^^^^^^^^^^^^^^^ required by this bound in `Sealed`
   = note: `Sealed` is a "sealed trait", because to implement it you also need to implelement `a::b::Hidden`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it

Deduplicate privacy errors that point to the same path segment even if their deduplication span are different.

When encountering a path that is not reachable due to privacy constraints path segments other than the last, keep metadata for the last path segment's Res in order to look for alternative import paths for that item to suggest. If there are none, be explicit that the item is not accessible.

error[E0603]: module `b` is private
  --> $DIR/re-exported-trait.rs:11:9
   |
LL | impl a::b::Trait for S {}
   |         ^ private module
   |
note: the module `b` is defined here
  --> $DIR/re-exported-trait.rs:5:5
   |
LL |     mod b {
   |     ^^^^^
help: consider importing this trait through its public re-export instead
   |
LL | impl a::Trait for S {}
   |      ~~~~~~~~
error[E0603]: module `b` is private
  --> $DIR/private-trait.rs:8:9
   |
LL | impl a::b::Hidden for S {}
   |         ^  ------ trait `b` is not publicly reachable
   |         |
   |         private module
   |
note: the module `b` is defined here
  --> $DIR/private-trait.rs:2:5
   |
LL |     mod b {
   |     ^^^^^

@rustbot
Copy link
Collaborator

rustbot commented Jun 16, 2023

r? @fee1-dead

(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 Jun 16, 2023
@rustbot

This comment was marked as resolved.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment was marked as resolved.

compiler/rustc_resolve/src/imports.rs Outdated Show resolved Hide resolved
tests/ui/macros/issue-88228.stderr Outdated Show resolved Hide resolved
error[E0603]: module `index` is private
--> $DIR/private-trait-non-local.rs:2:18
|
LL | use core::slice::index::private_slice_index::Sealed;
Copy link
Member

@compiler-errors compiler-errors Jun 16, 2023

Choose a reason for hiding this comment

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

I quite like this framing, though I wonder if we should either link to somewhere with privacy rules, or at least drive this home by noting something like "an item is only reachable via a path if all of the segments are reachable" -- with maybe better wording than what I wrote...

Just want something that folks can take away from the error message as intuition for why public-unreachable error happen the way they do?

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'd like to add some note about it, but I'm not sure what it should look like at the moment.

@petrochenkov
Copy link
Contributor

This touches name resolution infra, not only diagnostics.
r? @petrochenkov

@petrochenkov
Copy link
Contributor

You guys really want me to suffer.

In a typical estebank's fashion the PR

  1. makes several independent changes but merges them together, so it's not clear a) what effect each change has on diagnostics (test outputs) and b) what cost benefit ratio each independent change has.
  2. introduces ad hoc code in random places and technical debt so I have to understand which changes are not worth it and are better dropped, which I cannot do due to 1.
  3. has no PR description summarizing what you want to achieve

As a result, I don't want to see this PR merged due to 2. but I also don't want to review it to suggest improvements due to 1-3..

@petrochenkov
Copy link
Contributor

Could you split this into parts small enough to make it digestable?

  • one commit introducing new tests
  • several commits introducing minimal orthogonal changes to name resolution infra (non-diagnostics.rs code, this is what I want to keep maintainable and garbage-free) and showing their effect on test outputs
  • commit with remaining changes in diagnostics.rs files

(And also add a PR description.)
@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-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 16, 2023
@khionu
Copy link
Member

khionu commented Jun 16, 2023

@petrochenkov This review was really in bad form. You have valid critique that could have carried its own weight without either of the first two lines. Slipping in a public, passive-aggressive callout is no way to talk things out with your fellow project members. Please avoid that in the future, and if you feel you need help resolving issues with other people in the community, please reach out to Mods. We're here to help issues before they escalate, not just after.

@estebank
Copy link
Contributor Author

I've split this PR to the best of my abilities into easily digestible units. That being done, I do not believe initial PR was unreasonably unfocused, the changes were tightly related. Given the nature of the work when I'm doing these kind of changes, I sometimes have to experiment with multiple alternative ways of accomplishing the result I'm looking for looking for the most minimally invasive way of doing so. The above separation of commits is ahistorical and somewhat artificial and the ask requires more additional work from me than what would otherwise be required from the reviewer.

In more general terms, I understand that we have different perspectives on where different changes fall in the "increased complexity"/"user focus" axes, but I must say, I do not appreciate the antagonistic way you interact with me, here or in the past. If you desire to talk about it, with mods or on our own, you can find me in Zulip. I do not believe this is the appropriate place for that discussion.

@estebank estebank 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 Jun 17, 2023
@petrochenkov
Copy link
Contributor

@estebank
I prefer transparency and no mod involvement is required here.
I've been talking about this not once, not twice, not even three times, but yesterday evening I opened this PR and saw all the same stuff, and it was pretty infuriating.
I'll review the new version in a couple of days.

Comment on lines 1665 to 1656
err.span_label(
outer_ident.span,
format!("{} `{}` is not publicly reachable", this_res.descr(), ident),
);
Copy link
Member

Choose a reason for hiding this comment

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

can this use translatable diagnostics?

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'll do this as part of a separate PR.

@petrochenkov
Copy link
Contributor

petrochenkov commented Jun 20, 2023

Some more detailed meta-comments to clarify what I'd like to see from PRs like this as a reviewer:

  • 9f1279030655143b244aeb0f750641654a8da969 34c7c130987c12f11db222743da8bf62ed352e42 761d2b8c5937d72d3622e4664bf3280875e5df6d
  • 55ef88eb8ab59351eb9a5406c8a51ad66776ae84
    • Nit: moving code (sort) and changing code (dedup) in a same commit, so it's hard to see what changes.
  • 9f1279030655143b244aeb0f750641654a8da969 24dbcab241c1e9c1558a840da2ceeaddf27d5aaa 196fbc6d425665072034c4e16cf0e6cf024ec087
    • This change is more independent from the rest of the PR than others, and touches a different area of the compiler maintained by different people, so it would be better submitted as a separate PR.
    • That's probably too much splitting into commits, the commits don't touch non-diagnostic infra and all of them add a single diagnostic in a single place, could be merged into one.
  • Otherwise the current split into commits is great and makes it much easier to understand what happens, compared to the original version, even if it seemed artificial to you.
    • It still took me hours to review and write this post though, because there are many "logical units" of changes.
    • Some GitHub review tools work great at PR level, but not at individual commit levels, that's why I ask people to split large PRs (not necessarily large in terms of code amount, but more in terms of these logical units of changes). This PR would be fine two PRs, as I mentioned a few lines above.

I'll post the comments on code a bit later.

tests/ui/privacy/privacy1.rs Outdated Show resolved Hide resolved
tests/ui/privacy/privacy-in-paths.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/ident.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/ident.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/diagnostics.rs Show resolved Hide resolved
compiler/rustc_resolve/src/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/imports.rs Outdated Show resolved Hide resolved
@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 Jun 20, 2023
compiler/rustc_resolve/src/diagnostics.rs Show resolved Hide resolved
compiler/rustc_resolve/src/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/diagnostics.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/ident.rs Show resolved Hide resolved
compiler/rustc_resolve/src/imports.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/imports.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/ident.rs Outdated Show resolved Hide resolved
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jun 21, 2023
resolve: Minor cleanup to `fn resolve_path_with_ribs`

A single-use closure is inlined and one unnecessary enum is removed.

Noticed when reviewing rust-lang#112686.
@estebank estebank 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 Jun 21, 2023
compiler/rustc_resolve/src/lib.rs Outdated Show resolved Hide resolved
compiler/rustc_resolve/src/diagnostics.rs Outdated Show resolved Hide resolved
@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 Jun 22, 2023
@estebank estebank 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 Jun 22, 2023
@petrochenkov
Copy link
Contributor

r=me after addressing #112686 (comment) and cleaning up git history.
@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-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 22, 2023
@estebank
Copy link
Contributor Author

Addressed the last review comments. Rebased on top of a recent master to incorporate #112892.

Do you want me to merge everything into a single commit?

estebank added 2 commits June 22, 2023 16:50
When implementing a public trait with a private super-trait, we now emit
a note that the missing bound is not going to be able to be satisfied,
and we explain the concept of a sealed trait.
Suggest publicly accessible paths for items in private mod:

  When encountering a path in non-import situations that are not reachable
  due to privacy constraints, search for any public re-exports that the
  user could use instead.

Track whether an import suggestion is offering a re-export.

When encountering a path with private segments, mention if the item at
the final path segment is not publicly accessible at all.

Add item visibility metadata to privacy errors from imports:

  On unreachable imports, record the item that was being imported in order
  to suggest publicly available re-exports or to be explicit that the item
  is not available publicly from any path.

  In order to allow this, we add a mode to `resolve_path` that will not
  add new privacy errors, nor return early if it encounters one. This way
  we can get the `Res` corresponding to the final item in the import,
  which is used in the privacy error machinery.
@petrochenkov
Copy link
Contributor

Well, ideally it would still be commit per independent change, but the PR is now much smaller so it's not too bad as is.
@bors r+

@bors
Copy link
Contributor

bors commented Jun 22, 2023

📌 Commit 7dffd24 has been approved by petrochenkov

It is now in the queue for this repository.

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 22, 2023
@bors
Copy link
Contributor

bors commented Jun 22, 2023

⌛ Testing commit 7dffd24 with merge 04075b3...

@bors
Copy link
Contributor

bors commented Jun 22, 2023

☀️ Test successful - checks-actions
Approved by: petrochenkov
Pushing 04075b3 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 22, 2023
@bors bors merged commit 04075b3 into rust-lang:master Jun 22, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 22, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (04075b3): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.6% [-1.6%, -1.6%] 1
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.4% [1.0%, 1.8%] 3
Regressions ❌
(secondary)
3.1% [2.5%, 3.7%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.4% [1.0%, 1.8%] 3

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 661.969s -> 663.045s (0.16%)

@estebank estebank deleted the sealed-traits branch November 9, 2023 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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-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.

9 participants