Skip to content
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

Merged
merged 1 commit into from
Feb 8, 2017

Conversation

emilio
Copy link
Contributor

@emilio emilio commented Feb 2, 2017

Fixes #34798

@rust-highfive
Copy link
Collaborator

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)]
Copy link
Contributor

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)

Copy link
Member

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.

if all_phantom {
if check_kind.is_toplevel() {
return FfiUnsafe(UNSIZED_STRUCT_MSG);
}
Copy link
Contributor

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.

Copy link
Contributor Author

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>);
Copy link
Member

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?

Copy link
Contributor Author

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.

@nagisa
Copy link
Member

nagisa commented Feb 3, 2017

The PR seems overcomplicated to me. Wouldn’t something along the lines of returning FfiSafe for PhantomData and then checking that any of the fields is not a phantom data?

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), but I’d argue that rustc ought to trust it – primarily because I do not want rustc to report double warnings for something like this:

#[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).

@emilio emilio force-pushed the improper-ctypes branch 3 times, most recently from 3bbaaf1 to db824f8 Compare February 3, 2017 17:50
@emilio
Copy link
Contributor Author

emilio commented Feb 3, 2017

Ok, this approach should be quite simpler :)

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Feb 3, 2017

@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; };

@nagisa
Copy link
Member

nagisa commented Feb 3, 2017

@nikomatsakis

Of course it isn’t. Bar is not repr(C) and can be trivially declared to be non-FFI-safe just by looking at its repr attribute. What I’m not understanding is how recursive checking was at all necessary in the previous patch.

They removed the recursion now, so no point in discussing that anymore.

@nikomatsakis
Copy link
Contributor

@nagisa ok, I must be confused, but I would consider the fact that we must examine the types of the fields to decide if Foo is repr(C) to be "recursive". I'll note that the comment is still present and that check_type_for_ffi is still recursive -- but maybe you are referring to some other recursion?

self.cx.span_lint(IMPROPER_CTYPES,
sp,
&format!("found zero-sized type composed only \
of phantom-data in a foreign-function."));
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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).

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Feb 7, 2017

📌 Commit e866d07 has been approved by nikomatsakis

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 7, 2017
lint/ctypes: Don't warn on sized structs with PhantomData.

Fixes rust-lang#34798
bors added a commit that referenced this pull request Feb 7, 2017
Rollup of 15 pull requests

- Successful merges: #38764, #39361, #39426, #39462, #39482, #39557, #39561, #39582, #39583, #39587, #39598, #39599, #39601, #39602, #39604
- Failed merges:
frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 8, 2017
lint/ctypes: Don't warn on sized structs with PhantomData.

Fixes rust-lang#34798
frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 8, 2017
lint/ctypes: Don't warn on sized structs with PhantomData.

Fixes rust-lang#34798
frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 8, 2017
lint/ctypes: Don't warn on sized structs with PhantomData.

Fixes rust-lang#34798
bors added a commit that referenced this pull request Feb 8, 2017
Rollup of 11 pull requests

- Successful merges: #39462, #39512, #39529, #39557, #39561, #39582, #39583, #39597, #39622, #39624, #39630
- Failed merges:
@bors bors merged commit e866d07 into rust-lang:master Feb 8, 2017
@emilio emilio deleted the improper-ctypes branch February 11, 2017 17:37
jschwe added a commit to jschwe/rust-freetype that referenced this pull request Apr 21, 2024
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
github-merge-queue bot pushed a commit to servo/rust-freetype that referenced this pull request Apr 23, 2024
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants