Skip to content

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

Open
niacdoial wants to merge 1 commit intorust-lang:mainfrom
niacdoial:improperctypes-refactor2
Open

lint ImproperCTypes: refactor linting architecture (part 2)#146273
niacdoial wants to merge 1 commit 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 rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 28, 2026
@JohnTitor
Copy link
Copy Markdown
Member

@rustbot reroll

@rustbot rustbot assigned nnethercote and unassigned JohnTitor Apr 9, 2026
@nnethercote
Copy link
Copy Markdown
Contributor

Sorry to play hot potato, but this seems like the most realistic outcome:

r? @petrochenkov

@rustbot rustbot assigned petrochenkov and unassigned nnethercote Apr 9, 2026
@petrochenkov
Copy link
Copy Markdown
Contributor

Ok, then we return to

I can gradually review and merge this piece-by-piece is you submit the changes in smaller single-purpose portions, each accompanied with motivation
feel free to start splitting and assigning to me

@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 Apr 9, 2026
@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
| ty::Tuple(..)
| ty::Array(..)
| ty::Slice(_) => Self::UnusedVariant,
k @ _ => bug!("Unexpected outer type {:?} of kind {:?}", ty, k),
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 22, 2026

Choose a reason for hiding this comment

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

Suggested change
k @ _ => bug!("Unexpected outer type {:?} of kind {:?}", ty, k),
_ => bug!("unexpected outer type {ty:?}),

Debug printing for Ty already redirects to TyKind printing.

View changes since the review


impl OuterTyKind {
/// Computes the relationship by providing the containing mir::Ty itself
fn from_outer_ty<'tcx>(ty: Ty<'tcx>) -> Self {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 22, 2026

Choose a reason for hiding this comment

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

Suggested change
fn from_outer_ty<'tcx>(ty: Ty<'tcx>) -> Self {
fn from_ty<'tcx>(ty: Ty<'tcx>) -> Self {

Nit: already have "outer" in OuterTyKind.

View changes since the review

/// but is needed because we don't change the lint's behavior yet
NoneThroughFnPtr,
/// Placeholder for properties that will be used eventually
UnusedVariant,
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 22, 2026

Choose a reason for hiding this comment

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

Suggested change
UnusedVariant,
Other,

It's sort of used, because it indicates that the parent type is one of the allowed ones (and we panic on non-allowed). The comment can be also updated to "other allowed outer type".

View changes since the review

impl VisitorState {
/// From an existing state, compute the state of any subtype of the current type.
/// (General case. For the case where the current type is a function pointer, see `get_next_in_fnptr`.)
fn get_next<'tcx>(&self, current_ty: Ty<'tcx>) -> Self {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 22, 2026

Choose a reason for hiding this comment

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

Suggested change
fn get_next<'tcx>(&self, current_ty: Ty<'tcx>) -> Self {
fn next(&self, current_ty: Ty<'_>) -> Self {
  • get_ is usually omitted in typical naming conventions because it's kind of useless.
  • some of the lifetimes here and below are probably unnecessary.

View changes since the review

/// (General case. For the case where the current type is a function pointer, see `get_next_in_fnptr`.)
fn get_next<'tcx>(&self, current_ty: Ty<'tcx>) -> Self {
assert!(!matches!(current_ty.kind(), ty::FnPtr(..)));
Self {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 22, 2026

Choose a reason for hiding this comment

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

Suggested change
Self {
VisitorState {

Could you avoid Self in non-generic code? At least in constructors like this.

View changes since the review

Copy link
Copy Markdown
Contributor Author

@niacdoial niacdoial Apr 23, 2026

Choose a reason for hiding this comment

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

OK! I'm also changing the type annotation for OuterTyKind::from_ty's return and the RootUseFlags consts. Is this something you want?
edit: x fmt undoes that so...

root_use_flags: self.root_use_flags,
outer_ty_kind: OuterTyKind::from_outer_ty(current_ty),
}
}
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 22, 2026

Choose a reason for hiding this comment

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

Mega-nit: space between functions.

View changes since the review

/// Generate the state for an "outermost" type that needs to be checked
fn entry_point(root_use_flags: RootUseFlags) -> Self {
Self { root_use_flags, outer_ty_kind: OuterTyKind::None }
}
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 22, 2026

Choose a reason for hiding this comment

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

I'd inline and remove this function, it's trivial and is only used twice.

View changes since the review

}

/// Get the proper visitor state for a static variable's type
fn static_var() -> Self {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 22, 2026

Choose a reason for hiding this comment

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

Suggested change
fn static_var() -> Self {
fn static_entry_point() -> Self {

Nit: I don't think these are called "variables" often in Rust.
Also for consistency with fn_entry_point.

View changes since the review

}

/// Whether the type itself is the type of a function argument or return type.
fn is_direct_in_function(&self) -> bool {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 22, 2026

Choose a reason for hiding this comment

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

I'd rather not introduce simple functions that are used once, this one and is_direct_function_return can be inlined and removed.

View changes since the review

}

/// Get the proper visitor state for a given function's arguments or return type.
fn entry_point_from_fnmode(fn_mode: CItemKind, fn_pos: FnPos) -> Self {
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 22, 2026

Choose a reason for hiding this comment

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

Suggested change
fn entry_point_from_fnmode(fn_mode: CItemKind, fn_pos: FnPos) -> Self {
fn fn_entry_point(fn_mode: CItemKind, fn_pos: FnPos) -> Self {

For consistency with static_entry_point.
The _from_fnmode suffix would make sense if there were other simpler fn_entry, not from fn mode.

View changes since the review

@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
Another user-transparent change, unifying outer-type information and the
existing VisitorState flags.
@niacdoial niacdoial force-pushed the improperctypes-refactor2 branch from ac62a57 to 405a44a Compare April 24, 2026 06:29
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