Skip to content

Conversation

@SATVIKsynopsis
Copy link

@SATVIKsynopsis SATVIKsynopsis commented Dec 7, 2025

Fixes #149719

This PR replaces the todo!("FIXME(unsafe_binder)") branch in the improper_ctypes lint with a proper diagnostic.

Previously, using an unsafe binder inside an extern "C" block caused an internal compiler error.
This fix now ensures that the compiler now emits a clear and stable error message explaining that types containing unsafe binders are not yet supported in FFI.

A new UI test (unsafe-binder-basic.rs) is included to ensure this behavior remains stable and prevent regressions.

@rustbot rustbot 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 Dec 7, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 7, 2025

r? @mati865

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

ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
ty::UnsafeBinder(binder) => {
let ty = binder.skip_binder(); // extract the inner type
let _vars = binder.bound_vars(); // get bound vars if needed
Copy link
Member

Choose a reason for hiding this comment

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

When using a chatbot to generate the code, you have to at least review it afterwards. _var is unused and should be removed.

@mati865
Copy link
Member

mati865 commented Dec 7, 2025

Rerolling, as I have no expertise in this area.

@rustbot reroll

@rustbot rustbot assigned eholk and unassigned mati865 Dec 7, 2025
Comment on lines 1 to 4
// build-fail
#![feature(unsafe_binders)]
//~^ WARN the feature `unsafe_binders` is incomplete
#![deny(improper_ctypes)]
Copy link
Contributor

Choose a reason for hiding this comment

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

// build-fail comment is not necessary. And if you use #![expect(incomple_feature)], the WARN will not occur.

Copy link
Member

@Kivooeo Kivooeo left a comment

Choose a reason for hiding this comment

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

Hi, I see this is your first pull request - thank you for taking the time to submit it.

I’ve noticed the reasonable concerns raised by Mateusz above that this may have been AI-generated. If you'd like, you’re welcome to join the discussion below and help explain the reasoning behind the code decisions you made.


Also, I’d like to mention that we recently adopted a policy aimed at keeping reviews sustainable.

In short, it allows reviewers to redirect or close contributions that are overly burdensome to review - for example, when code or explanations appear inconsistent, contradictory, or don’t demonstrate an understanding of the existing codebase. You can read the full details here:

rust-lang/compiler-team#893

View changes since this review

@SATVIKsynopsis
Copy link
Author

I have updated the PR with the following fixes:

removed the unused _vars binding as suggested.

adjusted the UI test as per the review (removed the unnecessary // build-fail comment ).

reran the full test suite to confirm the diagnostic and stderr output are stable.

I did use an AI tool initially to draft the change, but I fully reviewed, corrected, and refined the code myself.

all changes now reflect my understanding of the lint and the unsafebinder handling.

Please let me know if anything else should be improved.

Copy link
Member

@Kivooeo Kivooeo left a comment

Choose a reason for hiding this comment

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

And some nits I'm less concerned about, but still think are worth mentioning

View changes since this review

@SATVIKsynopsis
Copy link
Author

thanks for asking and happy to explain and answer your questions

i used skip_binder() because i only wanted to check the inner type that actually reaches the FFI boundary. The binder itself doesn’t matter for the FFI-safety check, since C doesn’t understand those binders. if I used the whole ty without skipping the binder, the lint wouldn’t look at the real inner type.

i understand your point about the diagnostic looking like just &().
if you prefer that the message keeps the unsafe part visible, i can update the code to print it that way.

@SATVIKsynopsis
Copy link
Author

I see your point about using the fluent error messages instead of a raw string. I will take a look at the other arms and update this one to follow the same pattern so the diagnostic style stays consistent.

I will push the fixes shortly.

@rust-log-analyzer

This comment has been minimized.

@SATVIKsynopsis
Copy link
Author

that makes sense.

the current output only shows &(), while the span highlights the full unsafe &a (). I agree that the diagnostic should refer to the same type for consistency.

I will update the diagnostic to report unsafe &a () instead of just &(), and then adjust the .stder file to match the updated message.

Thanks for your clear explanation and feedback. it is really helpful as i am learning my way around the compiler codebase.

@SATVIKsynopsis
Copy link
Author

Thanks for the detailed feedback I have updated the PR to address both concerns:

  1. i changed the diagnostic to show the full type unsafe &a() instead of just the inner type &(). this makes it clearer that the entire unsafe binder construct is not supported in FFI.

  2. switched from using a raw string to the fluent message system (added lint_improper_ctypes_unsafe_binder in messages.ftl) to keep it consistent with the other improper_ctypes handling thing.

i removed the skip_binder() call since we are not actually checking the inner type and we are just reporting that unsafe binders aren't supported in FFI contexts yet. the test output now correctly shows the full type in the error message.

let me know if there is anything else that needs adjusting. happy to learn and contribute.

@SATVIKsynopsis SATVIKsynopsis force-pushed the main branch 2 times, most recently from 1adc7b7 to 8423d12 Compare December 8, 2025 11:02
@SATVIKsynopsis
Copy link
Author

hi i have squashed the commits and cleaned up the history as requested.

these are the updates included in the final commit:

  1. unified the diagnostic wording and switched to the fluent message system

  2. updated the error to display the full unsafe &a() type for consistency

  3. removed the unnecessary skip_binder() usage

  4. improved the message wording as suggested

  5. updated the ui test .stder fle to match the corrected diagnostic

thank you for the guidance and feedback it really helped me understand the lint behavior much better.
please let me know if anything else should be refined.

Copy link
Member

@Kivooeo Kivooeo left a comment

Choose a reason for hiding this comment

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

It would be great if you could restore the two lines that were accidentally removed

View changes since this review

@SATVIKsynopsis
Copy link
Author

I ran ./x.py test tidy for clean format file checking because this automatically runs after running git push. So, to double check formatting i ran ./x.py test tidy and it gave errors at these two locations which you mentioned so i had to give an extra newline to pass the tidy test.

@Kivooeo
Copy link
Member

Kivooeo commented Dec 8, 2025

I just copied your PR and tried add them locally and tidy have nothing against this lines, can you show exact output?

@SATVIKsynopsis
Copy link
Author

image

compiler/rustc_lint/src/types/improper_ctypes.rs. For this file when i remove the last newline it shows me this while running tidy. so for this only i gave the the newline so that tidy test passes.

image

and for the messages.ftl file i removed the newline in the previous commit only.

@Kivooeo
Copy link
Member

Kivooeo commented Dec 8, 2025

Why the error in improper_ctypes.rs points at line 1055, when you need add new line after line 1020 (that was here before)

and for the messages.ftl file i removed the newline in the previous commit only

Yes, return it back please :) As you can see here all messages are spaced by a new line, you can't just remove it like this

@Kivooeo
Copy link
Member

Kivooeo commented Dec 8, 2025

Squash please

@Kivooeo
Copy link
Member

Kivooeo commented Dec 8, 2025

Tidy is failing because you change unrelated line, as I pointed it before

Why the error in improper_ctypes.rs points at line 1055, when you need add new line after line 1020

@Kivooeo
Copy link
Member

Kivooeo commented Dec 8, 2025

Pay attention to line number here please #149730 (comment)

@rust-log-analyzer

This comment has been minimized.

@Kivooeo
Copy link
Member

Kivooeo commented Dec 8, 2025

You should preserve this last trailing line in the file

image

@SATVIKsynopsis
Copy link
Author

I have checked the tidy issues and fixed them. Also fixed the newline and whitespaces issues. Please verify now.

@Kivooeo
Copy link
Member

Kivooeo commented Dec 8, 2025

Nice, thanks!

Let's wait on CI and I will merge it afterwards

@Kivooeo Kivooeo assigned Kivooeo and unassigned eholk Dec 8, 2025
@Kivooeo
Copy link
Member

Kivooeo commented Dec 8, 2025

Note: while I was patient and walked you through this process, other reviewers might handle things differently. In the future, similar PRs could be closed early if they require excessive back and forth for basic issues.

I'm not trying to scare you, just want you to be aware so you can better prepare for next time. If you plan to keep contributing, I'd recommend spending more time studying the surrounding code and understanding the problem fully before submitting.

@SATVIKsynopsis
Copy link
Author

Thanks for the guidance and for taking the time to walk me through the review.
I understand the expectations better now, and I will make sure to study the surrounding code and prepare improvements more carefully in my future contributions.
I appreciate your patience. this was very helpful for me as a new contributor.

@Kivooeo
Copy link
Member

Kivooeo commented Dec 8, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 8, 2025

📌 Commit 0f4ec28 has been approved by Kivooeo

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

ICE: improper ctypes: extern unsafe binders

8 participants