Skip to content

Check unsafe impls on safe EIIs#159783

Open
AsakuraMizu wants to merge 1 commit into
rust-lang:mainfrom
AsakuraMizu:safe-eii-unsafe-impl
Open

Check unsafe impls on safe EIIs#159783
AsakuraMizu wants to merge 1 commit into
rust-lang:mainfrom
AsakuraMizu:safe-eii-unsafe-impl

Conversation

@AsakuraMizu

Copy link
Copy Markdown
Contributor

Tracking issue: #125418

Unsafe EIIs require implementations to use #[unsafe(...)], but safe EIIs currently accept this form as well. This should be rejected, just like unsafe impl on a safe trait.

This also fixes existing EII UI tests using this pattern and adds a new regression test.

@rustbot

rustbot commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

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

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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, 2026
@rustbot

rustbot commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 74 candidates
  • Random selection from 19 candidates

@mejrs

mejrs commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Another approach is to emit the unused_unsafe lint instead.

Right now, it is a breaking change to declare a unsafe trait but later remove the unsafe requirement. On the other hand, it is not a breaking change to have a unsafe function and then later remove the unsafe requirement.

This PR proposes to go the former way, where both adding and removing the unsafe requirement are breaking changes.

I'm undecided on what's best (so please don't go off making changes now), but note that this PR is a change to the semantics of the feature, not merely a "fix".

@bjorn3

bjorn3 commented Jul 23, 2026

Copy link
Copy Markdown
Member

I think it makes sense to follow the example of traits given that EIIs are kinda traits that can have a single global impl. If we decide to make removing unsafe from traits no longer a breaking change, we can adjust EII to follow this.

@rust-bors

This comment has been minimized.

@mejrs mejrs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That sounds good to me, let's do it.

r? me

View changes since this review

}

#[derive(Diagnostic)]
#[diag("`#[{$name}]` is not unsafe to implement")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As of #159755 we don't mention the #[/] parts

Suggested change
#[diag("`#[{$name}]` is not unsafe to implement")]
#[diag("`{$name}` is not unsafe to implement")]

Comment on lines +1088 to +1090
#[primary_span]
#[label("`unsafe` is not allowed here")]
pub span: Span,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For consistency with regular attributes I'd prefer if this message pointed at the entire attribute, and then the label only to the redundant unsafe

Suggested change
#[primary_span]
#[label("`unsafe` is not allowed here")]
pub span: Span,
#[primary_span]
pub impl_span: Span,
#[label("`unsafe` is not allowed here")]
pub unsafe_span: Span,

@rustbot rustbot assigned mejrs and unassigned oli-obk Jul 24, 2026
@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 Jul 24, 2026
@rustbot

rustbot commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@AsakuraMizu
AsakuraMizu force-pushed the safe-eii-unsafe-impl branch from e8c3cb3 to bae7e21 Compare July 24, 2026 09:48
@rustbot

rustbot commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@AsakuraMizu

Copy link
Copy Markdown
Contributor Author

Resolved conflicts and updated according to review comments.

@rustbot ready

@rustbot rustbot 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 Jul 24, 2026
@AsakuraMizu

Copy link
Copy Markdown
Contributor Author

@rustbot label +F-extern_item_impls

@rustbot rustbot added the F-extern_item_impls `#![feature(extern_item_impls)]` label Jul 24, 2026

@mejrs mejrs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment on lines +158 to +161
impl_unsafe_span: match *impl_safety {
Safety::Unsafe(span) => Some(self.lower_span(span)),
Safety::Safe(_) | Safety::Default => None,
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm fine with this change and would prefer to address the concept of how we carry around "safety" in the compiler in a more comprehensive manner (i.e., not here and not today).

@JonathanBrouwer are you okay with this change or would you rather we lower+carry Safety here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Using rustc_hir::Safety would be nicer as the type is more descriptive, but it does not contain the span so unless we change that (which is too much for this PR) it's not usable. This is fine for now

@JonathanBrouwer JonathanBrouwer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@bors r=mejrs,JonathanBrouwer rollup

View changes since this review

@rust-bors

rust-bors Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

📌 Commit bae7e21 has been approved by mejrs,JonathanBrouwer

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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-review Status: Awaiting review from the assignee but also interested parties. labels Jul 25, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 25, 2026
…r=mejrs,JonathanBrouwer

Check unsafe impls on safe EIIs

Tracking issue: rust-lang#125418

Unsafe EIIs require implementations to use `#[unsafe(...)]`, but safe EIIs currently accept this form as well. This should be rejected, just like `unsafe impl` on a safe trait.

This also fixes existing EII UI tests using this pattern and adds a new regression test.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 25, 2026
…r=mejrs,JonathanBrouwer

Check unsafe impls on safe EIIs

Tracking issue: rust-lang#125418

Unsafe EIIs require implementations to use `#[unsafe(...)]`, but safe EIIs currently accept this form as well. This should be rejected, just like `unsafe impl` on a safe trait.

This also fixes existing EII UI tests using this pattern and adds a new regression test.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 26, 2026
…r=mejrs,JonathanBrouwer

Check unsafe impls on safe EIIs

Tracking issue: rust-lang#125418

Unsafe EIIs require implementations to use `#[unsafe(...)]`, but safe EIIs currently accept this form as well. This should be rejected, just like `unsafe impl` on a safe trait.

This also fixes existing EII UI tests using this pattern and adds a new regression test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) F-extern_item_impls `#![feature(extern_item_impls)]` 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.

6 participants