Skip to content

Merge and reframe strict_provenance_lints#157575

Open
hanna-kruppe wants to merge 1 commit into
rust-lang:mainfrom
hanna-kruppe:merge-strict-provenance-lints
Open

Merge and reframe strict_provenance_lints#157575
hanna-kruppe wants to merge 1 commit into
rust-lang:mainfrom
hanna-kruppe:merge-strict-provenance-lints

Conversation

@hanna-kruppe

@hanna-kruppe hanna-kruppe commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

There does not seem to be any good reason to have separate lints for ptr2int and int2ptr casts. The main reason to enable this lint is to help with replacing as casts with strict provenance APIs (if possible) or explicit provenance APIs (when needed). That applies equally to both directions.

Merging the lints requires coming up with new name and lint explanation. This is a good chance to reconsider the purpose and messaging of the lints which have been essentially unchanged since the early days of the "strict provenance experiment". For reasons described below, I called the merged lint implicit_provenance_casts, rewrote its explanation from scratch, and made some changes to diagnostics.

First, the lint is not (only) about strict provenance any more. While strict provenance is often the best fix, migrating as casts to exposed provenance APIs also silences the lint. The exposed provenance APIs aren't any better for Miri and CHERI, but at least provenance considerations are made explicit, not picked up as side effect of the as keyword. (Banning use of exposed provenance APIs can be done with clippy's existing disallowed_methods lint.)

Second, provenance is now officially part of the language and prominently explained in the std::ptr documentation. The new lint refers to those docs and is more consistent with them.

Third, while strict provenance is encouraged whenever possible, exposed provenance is also part of the language and here to stay. Indeed, if we eventually enable the lint by default and make it cargo fix-able to keep the ecosystem migration manageable, we may want to suggest exposed provenance APIs to err on the side of not introducing UB. Thus, I made the diagnostics more neutral and descriptive. I've kept the suggestions that introduce strict provenance APIs, but we may want to reconsider this later.

Tracking issue: #130351

@rustbot rustbot added O-SGX Target: SGX O-unix Operating system: Unix-like O-windows Operating system: Windows S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 7, 2026
@rust-log-analyzer

This comment has been minimized.

@hanna-kruppe hanna-kruppe force-pushed the merge-strict-provenance-lints branch from d8b8b2f to 6e531ef Compare June 7, 2026 15:08
@hanna-kruppe hanna-kruppe marked this pull request as ready for review June 7, 2026 16:36
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 7, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 7, 2026
@rustbot

rustbot commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

r? @mu001999

rustbot has assigned @mu001999.
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 73 candidates
  • Random selection from 20 candidates

@hanna-kruppe

Copy link
Copy Markdown
Contributor Author

Not sure what the right process for renaming unstable lints. Does it need a lang FCP or is that only for new stable lints?

Also: cc @RalfJung

@RalfJung

RalfJung commented Jun 7, 2026

Copy link
Copy Markdown
Member

Since this is unstable it doesn't need heavy process.

However, we should ensure that the direction we are taking this is aligned with where t-lang wants this to go. Can you extend the PR description so it briefly recaps what the existing lints are and what they do? Then we can nominate that for t-lang to get their feedback.

Actually an easier approach might be for me to become the champion for these lints, so I can approve changes to them. I'll ask about that on Zulip.

@mu001999 mu001999 assigned RalfJung and unassigned mu001999 Jun 8, 2026
@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Jun 12, 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.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 12, 2026
There does not seem to be any good reason to have separate lints for
ptr2int and int2ptr casts. The main reason to enable this lint is to
help with replacing `as` casts with strict provenance APIs (if possible)
or explicit provenance APIs (when needed). That applies equally to both
directions.

Merging the lints requires coming up with new name and lint explanation.
This is a good chance to reconsider the purpose and messaging of the
lints which have been essentially unchanged since the early days of the
"strict provenance experiment". For reasons described below, I called
the merged lint `implicit_provenance_casts`, rewrote its explanation
from scratch, and made some changes to diagnostics.

First, the lint is not (only) about strict provenance any more. While
strict provenance is often the best fix, migrating `as` casts to exposed
provenance APIs also silences the lint. The exposed provenance APIs
aren't any better for Miri and CHERI, but at least provenance
considerations are made *explicit*, not picked up as side effect of the
`as` keyword. (Banning use of exposed provenance APIs can be done with
clippy's existing `disallowed_methods` lint.)

Second, provenance is now officially part of the language and
prominently explained in the `std::ptr` documentation. The new lint
refers to those docs and is more consistent with them.

Third, while strict provenance is encouraged whenever possible, exposed
provenance is also part of the language and here to stay. Indeed, if we
eventually enable the lint by default and make it `cargo fix`-able to
keep the ecosystem migration manageable, we may want to suggest exposed
provenance APIs to err on the side of not introducing UB. Thus, I made
the diagnostics more neutral and descriptive. I've kept the suggestions
that introduce strict provenance APIs, but we may want to reconsider
this later.
@hanna-kruppe hanna-kruppe force-pushed the merge-strict-provenance-lints branch from 0274a30 to fda7bff Compare June 12, 2026 19:22
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 12, 2026

@RalfJung RalfJung left a comment

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.

Thanks for your patience! I'm finally back from my trip and can start catching up with my backlog.

Mostly minor comments, but I am kind of concerned about the suggestions these lints emit. That's pre-existing though -- it does not block this PR, but I am not sure we want to stabilize this as-is.

@rustbot author

View changes since this review

Comment on lines +13 to +14
/// The `implicit_provenance_casts` lint detects integer-to-pointer and pointer-to-integer casts
/// that rely on [*Exposed Provenance*][exposed-provenance].

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.

This sounds like it only detects those int/ptr casts that rely on exposed provenance -- but really, they all do.

Suggested change
/// The `implicit_provenance_casts` lint detects integer-to-pointer and pointer-to-integer casts
/// that rely on [*Exposed Provenance*][exposed-provenance].
/// The `implicit_provenance_casts` lint detects integer-to-pointer and pointer-to-integer casts.

///
/// However, there are situations where exposed provenance is required or following the strict
/// provenance model requires major refactorings. In those cases, it's still useful to replace
/// the `as` casts with explicit use of exposed provenance APIs and a comment explaining why

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.

Suggested change
/// the `as` casts with explicit use of exposed provenance APIs and a comment explaining why
/// the `as` casts with equivalent explicit use of exposed provenance APIs and a comment explaining why

Comment on lines 16 to 17
LL - let addr: usize = &x as *const u8 as usize;
LL + let addr: usize = (&x as *const u8).addr();

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.

This suggestion seems kind of dangerous -- it is almost always wrong to just apply this, because the cast back to a pointer somewhere else in the code also needs to be adjusted. Do we really want such a suggestions?

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.

This doesn't seem worth a test

@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 25, 2026
@rustbot

rustbot commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

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

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

Labels

O-SGX Target: SGX O-unix Operating system: Unix-like O-windows Operating system: Windows S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants