-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lint/ctypes: Don't warn on sized structs with PhantomData. #39462
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![forbid(improper_ctypes)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a brief comment here indicating what this test is testing for? (with a link to the issue, ideally)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or just call the test issue-xxyyzz
, as it makes it easy to look up how the test came to be just from the filename.
src/librustc_lint/types.rs
Outdated
if all_phantom { | ||
if check_kind.is_toplevel() { | ||
return FfiUnsafe(UNSIZED_STRUCT_MSG); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove this check_kind
parameter and instead pass back FfiPhantom
(in which case a to-level caller will error out?). Or, alternatively, return FfiPhantomWrapper
or something so that the top-level can distinguish the two cases? Threading the check_kind
parameter doesn't seem necessary and is kind of annoying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, all this mess was mainly to distinguish the top-level phantom-data case from the zero-size struct. But @nagisa is right that not doing it simplifies a bunch this code, so we can also just use the same error message.
} | ||
|
||
#[repr(C)] | ||
pub struct ZeroSizeWithPhantomData<T>(::std::marker::PhantomData<T>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this in a run-pass test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it's a simple wrapper over PhantomData, and itsn't used directly, but inside a sized struct.
So the type used directly in an FFI interface would raise the warning (the compile-fail test checks for that), but I've allowed in a sized struct for the same reason I've allowed PhantomData. Let me know if it's not sound for some reason.
The PR seems overcomplicated to me. Wouldn’t something along the lines of returning let is_phantom_safe = adt_def.struct_variant().fields().iter().any(|field| {
!(field_type).sty.is_phantom_data()
}); or some such? The comments point to an observation that rustc cannot trust #[repr(C)] struct A(PhantomData<i32>); // reports warning here
#[repr(C)] struct B(A); // and also here if checks are recursive which is sure to happen if some sort of recursive checks are done (and if we do not want rustc to report double warnings, recursive checks are not necessary). |
3bbaaf1
to
db824f8
Compare
Ok, this approach should be quite simpler :) |
@nagisa I believe the point of the comment is that if one has the following: #[repr(C)]
struct Foo { bar: Bar }
struct Bar { x: u32 } This is not sufficient to allow C code like the following to use fields reliably: // woah, it hurts my head to write these declarations now:
struct Foo { Bar bar; };
struct Bar { u32 x; }; |
Of course it isn’t. They removed the recursion now, so no point in discussing that anymore. |
@nagisa ok, I must be confused, but I would consider the fact that we must examine the types of the fields to decide if |
self.cx.span_lint(IMPROPER_CTYPES, | ||
sp, | ||
&format!("found zero-sized type composed only \ | ||
of phantom-data in a foreign-function.")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@emilio why is it important to highlight the fact that the zero-sized type consists only of phantom data? Isn't it true that any zero-sized type is suspicious, and that is the real problem here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not necessarily important. It's just that we happen to have that info. I can omit it if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Note that other kind of zero-sized types get reported with different messages by the struct or union checks).
db824f8
to
e866d07
Compare
@bors r+ |
📌 Commit e866d07 has been approved by |
lint/ctypes: Don't warn on sized structs with PhantomData. Fixes rust-lang#34798
lint/ctypes: Don't warn on sized structs with PhantomData. Fixes rust-lang#34798
lint/ctypes: Don't warn on sized structs with PhantomData. Fixes rust-lang#34798
lint/ctypes: Don't warn on sized structs with PhantomData. Fixes rust-lang#34798
The linked issue rust-lang/rust#34798 has since been fixed, and rust will not warn if PhantomData is used in FFI structs. See also: rust-lang/rust#39462
* Re-enable improper_ctypes lint on bindings The linked issue rust-lang/rust#34798 has since been fixed, and rust will not warn if PhantomData is used in FFI structs. See also: rust-lang/rust#39462 * Update ci.yml Fixes warnings on github actions about updating to Node v20 by updating the used actions. actions-rs/toolchain is not maintained anymore, switch to dtolnay/rust-toolchain instead.
Fixes #34798