Skip to content

lint ImproperCTypes: refactor linting architecture (part 2)#146273

Open
niacdoial wants to merge 2 commits intorust-lang:mainfrom
niacdoial:improperctypes-refactor2
Open

lint ImproperCTypes: refactor linting architecture (part 2)#146273
niacdoial wants to merge 2 commits intorust-lang:mainfrom
niacdoial:improperctypes-refactor2

Conversation

@niacdoial
Copy link
Copy Markdown
Contributor

@niacdoial niacdoial commented Sep 6, 2025

View all comments

This is the second PR in an effort to split #134697 (refactor plus overhaul of the ImproperCTypes family of lints) into individually-mergeable parts.

Contains the changes of the first PR, and splits the core type checking function into several bits, each focused on a specific aspect of FFI-safety.
Some logic which was outside of said core function was also moved into the new functions.

Superset of: #146271

@rustbot rustbot added 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. labels Sep 6, 2025
Copy link
Copy Markdown
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

I think a lot of this makes sense, but aren't there behavior changes here? It looks like tuples and arrays may be treated slightly differently.

Which is probably fine, that would ideally just be split from the refactoring and come with test updates.

View changes since this review

Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch from 1dbb0e2 to f54061c Compare September 6, 2025 20:31
@niacdoial
Copy link
Copy Markdown
Contributor Author

I think a lot of this makes sense, but aren't there behavior changes here? It looks like tuples and arrays may be treated slightly differently.

Which is probably fine, that would ideally just be split from the refactoring and come with test updates.

I moved the actual change in behaviour in a later commit.
The rest of the changes here are just an exercise in moving more of the type-checking logic into the visit_* methods.

@rust-log-analyzer

This comment has been minimized.

@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch from f54061c to 66037fd Compare September 6, 2025 21:03
@rust-log-analyzer

This comment has been minimized.

@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch from 66037fd to 2781ebd Compare September 6, 2025 22:23
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
@tgross35
Copy link
Copy Markdown
Contributor

tgross35 commented Sep 7, 2025

I moved the actual change in behaviour in a later commit.
The rest of the changes here are just an exercise in moving more of the type-checking logic into the visit_* methods.

"split type visiting into subfunctions" still has some changes right? Array went from just checking the type to checking whether or not it is in a function. Which is probably a reasonable change to make, it should just be its own thing (and come with a test update).

(Possible I'm missing something here)

@niacdoial
Copy link
Copy Markdown
Contributor Author

Array went from just checking the type to checking whether or not it is in a function.

That's a bit of logic that was moved from check_type to visit_type

Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch 2 times, most recently from efe195a to 69b0807 Compare September 11, 2025 21:56
@rust-log-analyzer

This comment has been minimized.

@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch from 69b0807 to 64106e6 Compare September 11, 2025 22:10
@tgross35 tgross35 self-assigned this Sep 12, 2025
@tgross35
Copy link
Copy Markdown
Contributor

Btw if these are ready for a more final review, feel free to un-draft them (just gets them actually into my queue)

@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch from 64106e6 to 359cb79 Compare September 19, 2025 21:15
@niacdoial
Copy link
Copy Markdown
Contributor Author

just double-checked:
I'm pretty sure I covered all the things you made reviews on
(the one change in this force-push is renaming IndirectionType->IndirectionKind)

@tgross35
Copy link
Copy Markdown
Contributor

@niacdoial what exactly are these waiting on? I assume they're close to ready based on your above comment, but they are still marked as drafts.

@niacdoial niacdoial marked this pull request as ready for review September 22, 2025 18:38
@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 Sep 22, 2025
@niacdoial
Copy link
Copy Markdown
Contributor Author

niacdoial commented Sep 22, 2025

ah, I knew I was missing something (talking about the PR still being a draft)

Copy link
Copy Markdown
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Sorry for the delay here, I've been behind for al little while. Should be catching up now, though

View changes since this review

Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 21, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 9, 2026

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

@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch from c52aa53 to 2150411 Compare April 15, 2026 21:10
@niacdoial
Copy link
Copy Markdown
Contributor Author

ok! all is split, with the two commits "before" this one being #155358 and #155359
sorry it took some time, I ended up being far more busy than I thought I would

@petrochenkov
Copy link
Copy Markdown
Contributor

petrochenkov commented Apr 16, 2026

Blocked on #155359.
@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 16, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 21, 2026
….2, r=petrochenkov

Improperctypes refactor2.2

This is "part 2/3 of 2/3 of 1/2" of the original pull request rust-lang#134697 (refactor plus overhaul of the ImproperCTypes family of lints)
(all pulls of this series of pulls are supersets of the previous pulls.)
previous pull: rust-lang#155358
next pull: rust-lang#146273

This commit splits the lint's `visit_type` function into multiple functions that focus on specific things:
- visit_indirection (references, boxes, raw pointers)
- visit_variant_fields (the list of fields of a struct, enum variant, or union)
- visit_enum
- visit_struct_or_union
- visit_type (most "easy" decisions such as labeling `char` unsafe are here)

since, during these visits, we often move from an "outer type" to an "inner type" (structs, arrays, pointers, etc...),
two structs have been added to track the current state of a visit:
- VisitorState tracks the state related to the "original type" being checked (function argument/return, static variable)
- OuterTyData tracks the data related to the type "immediately outer to the current visited type"

r? petrochenkov (because you asked me to)
rust-timer added a commit that referenced this pull request Apr 21, 2026
Rollup merge of #155359 - niacdoial:improperctypes-refactor2.2, r=petrochenkov

Improperctypes refactor2.2

This is "part 2/3 of 2/3 of 1/2" of the original pull request #134697 (refactor plus overhaul of the ImproperCTypes family of lints)
(all pulls of this series of pulls are supersets of the previous pulls.)
previous pull: #155358
next pull: #146273

This commit splits the lint's `visit_type` function into multiple functions that focus on specific things:
- visit_indirection (references, boxes, raw pointers)
- visit_variant_fields (the list of fields of a struct, enum variant, or union)
- visit_enum
- visit_struct_or_union
- visit_type (most "easy" decisions such as labeling `char` unsafe are here)

since, during these visits, we often move from an "outer type" to an "inner type" (structs, arrays, pointers, etc...),
two structs have been added to track the current state of a visit:
- VisitorState tracks the state related to the "original type" being checked (function argument/return, static variable)
- OuterTyData tracks the data related to the type "immediately outer to the current visited type"

r? petrochenkov (because you asked me to)
@rust-bors

This comment has been minimized.

@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch from 2150411 to ac62a57 Compare April 21, 2026 21:30
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 21, 2026

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.

@niacdoial
Copy link
Copy Markdown
Contributor Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Apr 22, 2026
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
Comment thread compiler/rustc_lint/src/types/improper_ctypes.rs Outdated
@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 Apr 22, 2026
@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch from ac62a57 to 405a44a Compare April 24, 2026 06:29
niacdoial added 2 commits May 3, 2026 00:25
Another user-transparent change, unifying outer-type information and the
existing VisitorState flags.
In order to follow along with the efforts to properly distinguish
already-normalised and unnormalized types, we separate the internal
interfaces of this lint that rely on normalized types from those that
do not. We do that by adding the `Unnormalized` wrapper to some
interfaces.
@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch from 405a44a to 0300277 Compare May 2, 2026 22:25
@niacdoial
Copy link
Copy Markdown
Contributor Author

niacdoial commented May 2, 2026

added a new commit to better make use of the Unnormalized<> type wrapper that appeared during one of the rebases for this PR and its sub-PRs

@niacdoial
Copy link
Copy Markdown
Contributor Author

wait, have I not...?
@rustbot review

@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 May 3, 2026
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct VisitorState: u8 {
struct RootUseFlags: u16 {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov May 4, 2026

Choose a reason for hiding this comment

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

Suggested change
struct RootUseFlags: u16 {
struct RootUseFlags: u8 {

Extending to 16 bit is not (yet?) necessary.

View changes since the review

@@ -286,8 +295,10 @@ enum IndirectionKind {
}

bitflags! {
/// VisitorState flags that are linked with the root type's use.
/// (These are the permanent part of the state, kept when visiting new mir::Ty.)
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov May 4, 2026

Choose a reason for hiding this comment

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

Suggested change
/// (These are the permanent part of the state, kept when visiting new mir::Ty.)
/// (These are the permanent part of the state, kept when visiting new `Ty`.)

I've just noticed, what is mir::Ty?
In this PR it seems to refer to the usual Ty, which is not "mir".

View changes since the review

Copy link
Copy Markdown
Contributor Author

@niacdoial niacdoial May 4, 2026

Choose a reason for hiding this comment

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

ah, my bad.
It was to differentiate from hir::Ty (rustc_hir::ty::Ty) used elsewhere in the file, and I kinda assumed this meant the usual Ty (rustc_middle::ty::Ty) is MIR.

Should I still make it obvious this isn't the same as hir::Ty?

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.

If you need to disambiguate, then ty::Ty or hir::Ty can be used (whatever requires less annotations).

@petrochenkov
Copy link
Copy Markdown
Contributor

r=me after addressing the remaining nits.
@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 May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants