Skip to content

Fix tail calling intrinsics - #144815

Closed
WaffleLapkin wants to merge 2 commits into
rust-lang:masterfrom
WaffleLapkin:intrinsics-tail
Closed

Fix tail calling intrinsics#144815
WaffleLapkin wants to merge 2 commits into
rust-lang:masterfrom
WaffleLapkin:intrinsics-tail

Conversation

@WaffleLapkin

@WaffleLapkinWaffleLapkin commented Aug 2, 2025

Copy link
Copy Markdown
Member

Tail-calling intrinsics used to ICE/miscompile, this PR fixes that.

The reason I think we should explicitly support calling intrinsics, rather than banning it, is that I don't think we want to expose to users if a function is an intrinsic or not. This is especially relevant with something like std::mem::transmute which is a direct export of the intrinsic, rather than a wrapper function.

The fix has two parts:

  1. For intrinsics which are lowered in mir I've added code to lower them even if they are tail called
  2. For cg_ssa I added code which transforms a tail call to an intrinsic into intrinsic desugaring+ret. This feels like a hack, but I don't really see a better way. (cc @scottmcm, maybe you have an opinion?)

Fixes#144806

@rustbot

Copy link
Copy Markdown
Collaborator

r? @SparrowLii

rustbot has assigned @SparrowLii.
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

@rustbotrustbot added 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 Aug 2, 2025
@rustbot

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job tidy failed! Check out the build log: (web)(plain enhanced)(plain)

Click to see the possible cause of the failure (guessed by this bot)

added 291 packages in 34s
Running eslint on rustdoc JS files
info: ES-Check: there were no ES version matching errors! 🎉
some tidy checks failed
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:01:51
local time: Sat Aug 2 09:34:10 UTC 2025
network time: Sat, 02 Aug 2025 09:34:10 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

@theemathas

Copy link
Copy Markdown
Contributor

The fact that transmute is an intrinsic is actually already exposed to users. It cannot be coerced to a function pointer.

fnmain(){let _:unsafefn(()) = std::mem::transmute::<(),()>;}
error[E0308]: cannot coerce intrinsics to function pointers
--> src/main.rs:2:28
|
2 | let _: unsafe fn(()) = std::mem::transmute::<(), ()>;
| ------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot coerce intrinsics to function pointers
| |
| expected due to this
|
= note: expected fn pointer `unsafe fn(())`
found fn item `unsafe fn(()) {std::intrinsics::transmute::<(), ()>}`
For more information about this error, try `rustc --explain E0308`.

@scottmcm

Copy link
Copy Markdown
Member

@theemathas's comment reminded me about #106281 (comment), so I went and wrote rust-lang/rfcs#3844 to propose the alternative here: that we just make mem::transmute not be an intrinsic any more so we can not care :)

))),
));
terminator.kind = TerminatorKind::Goto { target };
terminator.kind = cont.unwrap();

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.

Hmm, I opened the code change thinking "well, if this is small then whatever we can just do it", but needing all these conts is kinda annoying, and I definitely don't think I'd (personally) be excited to write double the tests to make sure that the call and the become both work the next time I add something to this fine.


How many intrinsics exist in the wild that are allowed to be called from stable? Could we do something simpler just for those? Then we could just do an rust-lang/compiler-team#620 ICE for TailCall'ing any other intrinsic.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

How many intrinsics exist in the wild that are allowed to be called from stable?

From searching for #[stable in https://doc.rust-lang.org/stable/src/core/intrinsics/mod.rs.html#3830, it appears it's only transmute (there are a few other stable functions, but they are wrapper functions, not intrinsics directly)

Gives this and #144815 (comment) I'm inclined to say it's fine to forbid tail-calling intrinsics. I think I'll change this PR to make it an error to tail call an intrinsic.

@WaffleLapkinWaffleLapkin added the F-explicit_tail_calls `#![feature(explicit_tail_calls)]` label Aug 3, 2025
samueltardieu added a commit to samueltardieu/rust that referenced this pull request Aug 3, 2025
…mpiler-errors,scottmcm
Forbid tail calling intrinsics
There is only one intrinsic that can be called on stable, as far as I can find, (`transmute`). And in general tail calling intrinsics doesn't make much sense.
Alternative to rust-lang#144815 (and thus closesrust-lang#144815)
Fixesrust-lang#144806
r? `@scottmcm`
samueltardieu added a commit to samueltardieu/rust that referenced this pull request Aug 3, 2025
…mpiler-errors,scottmcm
Forbid tail calling intrinsics
There is only one intrinsic that can be called on stable, as far as I can find, (`transmute`). And in general tail calling intrinsics doesn't make much sense.
Alternative to rust-lang#144815 (and thus closesrust-lang#144815)
Fixesrust-lang#144806
r? ``@scottmcm``
rust-timer added a commit that referenced this pull request Aug 4, 2025
Rollup merge of #144851 - WaffleLapkin:instrinsic-deny, r=compiler-errors,scottmcm
Forbid tail calling intrinsics
There is only one intrinsic that can be called on stable, as far as I can find, (`transmute`). And in general tail calling intrinsics doesn't make much sense.
Alternative to #144815 (and thus closes#144815)
Fixes#144806
r? ``@scottmcm``
@rustbotrustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 4, 2025
github-actionsBot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Aug 4, 2025
…rors,scottmcm
Forbid tail calling intrinsics
There is only one intrinsic that can be called on stable, as far as I can find, (`transmute`). And in general tail calling intrinsics doesn't make much sense.
Alternative to rust-lang/rust#144815 (and thus closesrust-lang/rust#144815)
Fixesrust-lang/rust#144806
r? ``@scottmcm``
@WaffleLapkin
WaffleLapkin deleted the intrinsics-tail branch August 4, 2025 06:06
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-explicit_tail_calls`#![feature(explicit_tail_calls)]`T-compilerRelevant 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.

ICE from tail call to transmute

6 participants

@WaffleLapkin@rustbot@rust-log-analyzer@theemathas@scottmcm@SparrowLii