Skip to content

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented May 23, 2025

The function_casts_as_integer lint detects cases where users cast a function pointer into an integer.

warn-by-default

Example

fn foo() {}
let x = foo as usize;
warning: casting a function into an integer implicitly
  --> $DIR/function_casts_as_integer.rs:9:17
   |
LL |     let x = foo as usize;
   |                 ^^^^^^^^
   |
help: add `fn() as usize`
   |
LL |     let x = foo as fn() as usize;
   |                 +++++++

Explanation

You should never cast a function directly into an integer but go through a cast as fn first to make it obvious what's going on. It also allows to prevent confusion with (associated) constants.

Related to #81686 and https://stackoverflow.com/questions/68701177/whats-the-meaning-of-casting-a-rust-enum-variant-to-a-numeric-data-type

r? @Urgau

@rustbot rustbot added O-unix Operating system: Unix-like 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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 23, 2025
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch 2 times, most recently from 07f2c3c to 4978962 Compare May 23, 2025 20:54
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch 2 times, most recently from 3db3153 to d8b1955 Compare May 24, 2025 10:10
@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau added T-lang Relevant to the language team and removed 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 May 24, 2025
@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch from d8b1955 to 45984df Compare May 24, 2025 18:45
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch from 45984df to a6107b4 Compare May 24, 2025 19:09
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch from a6107b4 to 24d757e Compare May 24, 2025 22:47
@rustbot
Copy link
Collaborator

rustbot commented May 24, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer

This comment has been minimized.

@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 May 26, 2025
@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch from 24d757e to 3529162 Compare May 27, 2025 14:12
@rustbot
Copy link
Collaborator

rustbot commented May 27, 2025

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

/// a cast as `fn` first to make it obvious what's going on. It also allows
/// to prevent confusion with (associated) constants.
pub FUNCTION_CASTS_AS_INTEGER,
Warn,
Copy link
Member

Choose a reason for hiding this comment

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

Clippy has a few lints for fn to integer casts. But they are all restriction or style lints in Clippy. Adding a warn-by-default lint about this to rustc might be a bit aggressive 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I know, I implemented one myself. 😉 I think it highlights the fact that this is a big issue and that the compiler should warn about it and eventually even forbid this fn to integer cast (you need to cast to an fn pointer first).

But in any case, it's up to the lang team.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed 👍 Just want to add this information as "prior art" for the lang team to make this decision. Even though it might've sounded like it, I'm not against adding this lint to rustc.

Clippy question: Do you think if this lint gets added to rustc, we can (partially) deprecate Clippy lints?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hard to say. For example confusing_method_to_numeric_cast provides extra information about what (likely) went wrong. But with the current lint, they likely would already have seen the problem and fixed it. So by default I'd say yes. But we could eventually uplift part of them to add the extra context clippy has that this lint doesn't provide. Would make it much more interesting and even more useful.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, a partial uplift might be good then, should this be accepted.

@GuillaumeGomez
Copy link
Member Author

That seems related but separated from the current goal of this PR, no? More like a second step. First we warn for this as cast, second we can provide a suggestion saying that if they actually want the address, they can use this newly added method on functions to cast the function address into a usize. What do you think?

@joshtriplett
Copy link
Member

joshtriplett commented Jul 9, 2025

@GuillaumeGomez I was proposing it in part because it might make it easier for us to agree that we have a better alternative to the as-cast. And if we know that's where we want to get to, then we might not want the churn of driving people towards as something as usize and then towards .fn_addr().

Definitely not looking to make the perfect the enemy of the good, here. Rather, trying to make sure we have a sufficient good that people feel motivated to warn about as usize.

@GuillaumeGomez
Copy link
Member Author

Yeah, thinking some more about it today, I agree with you. If the libs team is ok with the addition of this new method on fn types, then I can send a PR. My only issue is that we'll need for this new method to be stabilized, and in the meantime, the current issue will remain. I suppose we suggest the new method on nightly and the longer version until then to reduce this delay?

@joshtriplett
Copy link
Member

joshtriplett commented Jul 9, 2025

We have an accepted ACP for an API that would work for this: rust-lang/libs-team#589 (comment)

We'd like to see a lint based on this, and attempt to ship and stabilize that API in a timely fashion.

If that API ends up taking longer than expected, we'd also approve an interim lint catching specific cases like the integer max/min functions.

@traviscross traviscross removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-2 Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang. labels Jul 9, 2025
@GuillaumeGomez
Copy link
Member Author

Then I can send a PR to implement this new API as a first step if it's ok with you and we'll see the next step once merged?

@apiraino
Copy link
Contributor

hello, trying to get current status here: is here currently waiting on @GuillaumeGomez to implement the lint described in this comment?

What about the needs-fcp label? Is an FCP needed to land this at a later point?

Thanks for clarifying a bit the next steps 🙂

@traviscross
Copy link
Contributor

What about the needs-fcp label? Is an FCP needed to land this at a later point?

New lints and significant lint expansions must be approved by the lang team, so in whatever form this ends up coming back, this will need a lang FCP.

@Urgau Urgau added S-waiting-on-t-lang Status: Awaiting decision from T-lang and removed S-waiting-on-team DEPRECATED: Use the team-based variants `S-waiting-on-t-lang`, `S-waiting-on-t-compiler`, ... labels Oct 6, 2025
@Amanieu Amanieu added the I-lang-nominated Nominated for discussion during a lang team meeting. label Oct 11, 2025
@Amanieu
Copy link
Member

Amanieu commented Oct 11, 2025

I had a long discussion with @GuillaumeGomez about this in person. The current situation is that the lack of this lint results in real code in the wild being subtly incorrect, potentially resulting in soundness issues (#81686, #146364). While it would be nice to wait for as_ptr or addr methods to be added to function types and stabilized, I don't think that this should block adding this lint: the suggestion can always be adjusted in the future once (and if) the new methods are stabilized. As such, I would like @rust-lang/lang to re-considering adding this lint in its current state.

@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch from f123b28 to 3777126 Compare October 11, 2025 12:02
@rustbot rustbot added O-unix Operating system: Unix-like O-windows Operating system: Windows T-clippy Relevant to the Clippy team. labels Oct 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 11, 2025

This PR was rebased onto a different master 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.

@GuillaumeGomez
Copy link
Member Author

Fixed merge conflicts.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-miri failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[RUSTC-TIMING] cargo_metadata test:false 11.085
error: direct cast of function item into an integer
   --> src/tools/miri/src/shims/native_lib/trace/parent.rs:504:31
    |
504 |     new_regs.set_ip(mempr_off as usize);
    |                               ^^^^^^^^
    |
    = note: `-D function-casts-as-integer` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(function_casts_as_integer)]`
help: first cast to a function pointer `unsafe extern "C" fn()`
    |
504 |     new_regs.set_ip(mempr_off as unsafe extern "C" fn() as usize);
    |                               +++++++++++++++++++++++++

error: direct cast of function item into an integer
   --> src/tools/miri/src/shims/native_lib/trace/parent.rs:545:30
    |
545 |     new_regs.set_ip(mempr_on as usize);
    |                              ^^^^^^^^
    |
help: first cast to a function pointer `unsafe extern "C" fn()`
    |
545 |     new_regs.set_ip(mempr_on as unsafe extern "C" fn() as usize);
    |                              +++++++++++++++++++++++++

[RUSTC-TIMING] miri test:false 18.035
error: could not compile `miri` (lib) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...

@traviscross
Copy link
Contributor

The last time we discussed this on lang, one part of the outcome is what @joshtriplett said in #141470 (comment):

If that API ends up taking longer than expected, we'd also approve an interim lint catching specific cases like the integer max/min functions.

To what degree might that be helpful?

In prior discussion, one place where the PR in its current form raised questions is that it seems that we'd be asking people to write out full function signatures in order to make these casts (including, then, having to import or otherwise name function argument and return types that may otherwise not be needed there), and there was a feeling that this may seem too onerous.

What options might we have to ameliorate that?

@traviscross traviscross added the P-lang-drag-2 Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang. label Oct 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. O-unix Operating system: Unix-like O-windows Operating system: Windows P-lang-drag-2 Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang. S-waiting-on-t-lang Status: Awaiting decision from T-lang T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.