Skip to content

Semantic checks of impl restrictions#154661

Open
CoCo-Japan-pan wants to merge 12 commits intorust-lang:mainfrom
CoCo-Japan-pan:impl-restriction-check
Open

Semantic checks of impl restrictions#154661
CoCo-Japan-pan wants to merge 12 commits intorust-lang:mainfrom
CoCo-Japan-pan:impl-restriction-check

Conversation

@CoCo-Japan-pan
Copy link
Copy Markdown
Contributor

@CoCo-Japan-pan CoCo-Japan-pan commented Apr 1, 2026

View all comments

This PR implements semantic checks for impl restrictions proposed in the Restrictions RFC (Tracking Issue #105077), and linked to a GSOC idea/proposal.

It lowers the resolved paths of impl restrictions from the AST to HIR and into TraitDef, and integrates the checks into the coherence phase by extending check_impl. As parsing (#152943) and path resolution (#153556) have already been implemented, this PR provides a working implementation of impl restrictions.

r? @Urgau
cc @jhpratt

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 1, 2026

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Apr 1, 2026
@CoCo-Japan-pan CoCo-Japan-pan changed the title Impl restriction check impl restriction check Apr 1, 2026
@CoCo-Japan-pan CoCo-Japan-pan changed the title impl restriction check Semantic checks of impl restrictions Apr 1, 2026
Copy link
Copy Markdown
Member

@Urgau Urgau left a comment

Choose a reason for hiding this comment

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

Looks promising. Left some preliminary comments.

View changes since this review

@Urgau Urgau 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 Apr 3, 2026
@Urgau Urgau 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 Apr 5, 2026
Copy link
Copy Markdown
Member

@Urgau Urgau left a comment

Choose a reason for hiding this comment

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

Looks good to me. I will let @jhpratt do the final review.

View changes since this review

@Urgau Urgau mentioned this pull request Apr 5, 2026
9 tasks

error: trait cannot be implemented outside `external_impl_restriction`
--> $DIR/impl-restriction-check.rs:10:1
error: trait cannot be implemented outside `external::inner`
Copy link
Copy Markdown
Member

@jhpratt jhpratt Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

As a separate crate, shouldn't this be simply outside `external`? A downstream user shouldn't need to know the specific module that it can't be implemented outside of, as it's not in their control. Naming the external crate should be sufficient — anywhere they try to implement it in their crate will fail.

At least, I'm fairly certain that's why I added this test in my original PR; I wanted to be sure the conditional was working properly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've asked for the condition to be changed in #154661 (comment) as it was refering to the wrong crate name (external_impl_restriction instead of external).

Copy link
Copy Markdown
Member

@jhpratt jhpratt Apr 9, 2026

Choose a reason for hiding this comment

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

I'm referring to the ::inner being present, not the crate name. A downstream user presumably doesn't care if it's restricted to the crate or something within the crate; they can't implement it regardless.

Not sure it's the best of ideas, but as it's simply a String this could be truncated to just before :: (presumably checking for leading :: as well).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't mind only mentionning the crate name in the main message, as you said it's probably not useful.

It's still nevertheless interesting that the privacy error do mention the other crate modules:

error[E0603]: module `sealed` is private
   --> src/lib.rs:1:17
    |
  1 | trait Foo: std::sealed::Sealed {}
    |                 ^^^^^^  ------ trait `Sealed` is not publicly re-exported
    |                 |
    |                 private module
    |
note: the module `sealed` is defined here
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/lib.rs:750:1
    |
750 | mod sealed {
    | ^^^^^^^^^^

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Admittedly that's probably because there currently isn't any better way to point at where something was sealed, where here we are doing so natively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants