Skip to content

Implement RFC 3323: restrictions #106074

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

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

Conversation

jhpratt
Copy link
Member

@jhpratt jhpratt commented Dec 23, 2022

This PR implements RFC 3323, which proposed impl and mut restrictions. Both are implemented here.

The current state of the PR is insufficient to merge. mut restrictions are implemented without any known bugs. impl restrictions, however, are not currently enforced on external traits — they are only enforced on same-crate traits. I do not know why this is the case, and would appreciate some help in narrowing down what needs to change to fix this. This is the only known issue with the implementation, and is why this PR is currently marked as a draft.

Once the bug is fixed, I'll also need to clean up the commit history. The current history is due to having it implemented while the RFC was still in progress.

CI will fail for a number of reasons. I still have TODO in the code as opposed to FIXME (because it needs to be done before this gets merged). I haven't added the tracking issue yet. And the bug will also cause a few errors by itself.

I am currently refactoring some code and cleaning up the commit history. After that is done, this PR will be ready to merge.

cc #105077

@rustbot
Copy link
Collaborator

rustbot commented Dec 23, 2022

r? @eholk

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

@rustbot rustbot added T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic 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 Dec 23, 2022
@rustbot
Copy link
Collaborator

rustbot commented Dec 23, 2022

Error: Label B-rfc-approved can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@jhpratt jhpratt mentioned this pull request Dec 23, 2022
10 tasks
@jhpratt

This comment was marked as resolved.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 23, 2022
@joshtriplett
Copy link
Member

Reassigning to T-compiler, as this doesn't need further T-lang review yet.

@joshtriplett joshtriplett added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Dec 23, 2022
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Dec 29, 2022

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

@rustbot rustbot added T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic labels Jan 5, 2023
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms rust-cloud-vms bot force-pushed the restrictions branch 2 times, most recently from 0f88945 to c0c8d21 Compare January 5, 2023 23:53
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 19, 2023

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

@@ -1,5 +1,5 @@
Visibility is restricted to a module which isn't an ancestor of the current
item.
Visibility or restriction is restricted to a module which isn't an ancestor of
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe

Suggested change
Visibility or restriction is restricted to a module which isn't an ancestor of
Visibility or restriction is limited to a module which isn't an ancestor of

to avoid repetition?

@apiraino
Copy link
Contributor

I'll reassign to author(s) to rebase and address a few comments but tbh I'm not sure about who should have the ball right now. Feel free to request a review with @rustbot ready, thanks!

@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 Aug 24, 2023
@petrochenkov
Copy link
Contributor

When it gets to actual landing it will be better to split this PR into separate PRs for individual sub-features, it's too large right now.

@WaffleLapkin
Copy link
Member

Agree with @petrochenkov, one of the reasons I'm procrastinating reviewing this (sorry!) is that this PR adds a lot. Maybe an approach closer to what we are doing with the explicit tail calls experiment (a PR per level — one for parsing, one for HIR, ...; maybe a PR "per feature" would make more sense here, I'm not sure) would be a bit easier to manage.

@jhpratt
Copy link
Member Author

jhpratt commented Aug 24, 2023

No worries on the delay — I know it's a large PR. I can definitely split it up into multiple PRs when I get the chance. That may be a little while, unfortunately, as I've got a few different things going on.

@compiler-errors
Copy link
Member

For the record: feel free to follow the new nightly style policy and open a separate formatting PR against rustfmt once this has landed in the compiler.

@Dylan-DPC
Copy link
Member

@jhpratt any updates on this?

@jhpratt
Copy link
Member Author

jhpratt commented Feb 5, 2024

@Dylan-DPC Not at the moment. I have splitting it up on my personal backlog.

@Dylan-DPC
Copy link
Member

@jhpratt any updates on this? It's just collecting conflicts at the moment :D

@WaffleLapkin
Copy link
Member

r? compiler
(I know this is waiting on author, but I do not plan to review anymore, so rerolling)

@rustbot rustbot assigned pnkfelix and unassigned WaffleLapkin Jul 25, 2024
@compiler-errors
Copy link
Member

I can review this but I'd rather we split it as agreed above (not to seem repetitive, and obviously take your time).

@jhpratt
Copy link
Member Author

jhpratt commented Jul 25, 2024

The update would be that I'm quite busy, unfortunately. Believe me when I say this is something that I still want to happen 😄

Between work, RustConf prep, and other open source things, this isn't happening in the next month. However, I hope to get around to it within the next few months. It'll likely be a series of PRs, each being an incremental step towards the full implementation that this PR is.

@alex-semenyuk alex-semenyuk 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 12, 2024
@JohnCSimon JohnCSimon 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 29, 2024
@Dylan-DPC
Copy link
Member

@jhpratt any updates on this?

@jhpratt
Copy link
Member Author

jhpratt commented Jan 27, 2025

@Dylan-DPC I'll be working on it in the coming weeks 😄

github-merge-queue bot pushed a commit to rust-lang/rust-clippy that referenced this pull request Apr 1, 2025
…items (#14516)

Found this while reviewing PR
rust-lang/rust#138384: See
rust-lang/rust#138384 (comment) in
which I suggested a FIXME to be added that I'm now fixing in this PR.

---

Before this PR, `redundant_pub_crate` computed a broken primary Span
when flagging items (`hir::Item`s) that never bear a name (`ForeignMod`,
`GlobalAsm`, `Impl`, `Use(UseKind::Glob)`, `Use(UseKind::ListStem)`).
Namely, it created a span whose high byte index is `DUMMY_SP.hi()` which
is quite broken (`DUMMY_SP` is synonymous with `0..0` wrt. the entire
`SourceMap` meaning it points at the start of the very first source file
in the `SourceMap`).

Why did this happen? Before PR
rust-lang/rust#138384, the offending line looked
like `let span = item.span.with_hi(item.ident.span.hi());`. For nameless
items, `item.ident` used to be set to `Ident(sym::empty, DUMMY_SP)`.
This is where the `DUMMY_SP` came from.

The code means to compute a "shorter item span" that doesn't include the
"body" of items, only the "head" (similar to `TyCtxt::def_span`).

<details><summary>Examples of Clippy's butchered output on
master</summary>

```rs
#![deny(clippy::redundant_pub_crate)]

mod m5 {
    pub mod m5_1 {}
    pub(crate) use m5_1::*;
}
```

```
error: pub(crate) import inside private module
 --> file.rs:1:1
  |
1 | / #![deny(clippy::redundant_pub_crate)]
2 | |
3 | | mod m5 {
4 | |     pub mod m5_1 {}
5 | |     pub(crate) use m5_1::*;
  | |    ^---------- help: consider using: `pub`
  | |____|
  |
```

Or if the `SourceMap` contains multiple files (notice how it leaks
`clippy.toml`!):

```
error: pub(crate) import inside private module
 --> /home/fmease/programming/rust/clippy/clippy.toml:1:1
  |
1 | / avoid-breaking-exported-api = false
2 | |
3 | | check-inconsistent-struct-field-initializers = true
... |
6 | |
  | |_^
  |
 ::: file.rs:6:5
  |
6 |       pub(crate) use m5_1::{*}; // Glob
  |       ---------- help: consider using: `pub`
  |
```

</details>

---

**Note**: Currently, the only nameless item kind that can also have a
visibility is `Use(UseKind::{Glob,ListStem})`. Thus I'm just falling
back to the entire item's Span which wouldn't be that verbose. However,
in the future Rust will feature impl restrictions (like `pub(crate) impl
Trait for Type {}`, see [RFC
3323](https://rust-lang.github.io/rfcs/3323-restrictions.html) and
rust-lang/rust#106074). Using `item.span` for
these would be quite bad (it would include all associated items). Should
I add a FIXME?

---

changelog: [`redundant_pub_crate`]: Fix the code highlighting for
nameless items.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic F-impl_restriction `#![feature(impl_restriction)]` F-mut_restriction `#![feature(mut_restriction)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) 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.