Skip to content

Make *const (), *mut () okay for FFI - #84267

Merged
bors merged 2 commits into
rust-lang:masterfrom
dtolnay:ptrunit
Oct 3, 2021
Merged

Make *const (), *mut () okay for FFI#84267
bors merged 2 commits into
rust-lang:masterfrom
dtolnay:ptrunit

Conversation

@dtolnay

Copy link
Copy Markdown
Member

Pointer-to-() is used occasionally in the standard library to mean "pointer to none-of-your-business". Examples:

I believe it's useful for the same purpose in FFI signatures, even while () itself is not FFI safe. The following should be allowed:

extern"C"{fndemo(pc:*const(),pm:*mut());}

Prior to this PR, those pointers were not considered okay for an extern signature.

warning: `extern` block uses type `()`, which is not FFI-safe --> src/main.rs:2:17 |2 | fn demo(pc: *const (), pm: *mut ()); | ^^^^^^^^^ not FFI-safe | = note: `#[warn(improper_ctypes)]` on by default = help: consider using a struct instead = note: tuples have unspecified layoutwarning: `extern` block uses type `()`, which is not FFI-safe --> src/main.rs:2:32 |2 | fn demo(pc: *const (), pm: *mut ()); | ^^^^^^^ not FFI-safe | = help: consider using a struct instead = note: tuples have unspecified layout

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @matthewjasper

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 17, 2021
@tspiteri

Copy link
Copy Markdown
Contributor

Would there be any difference between *const () and *const c_void in FFI?

@crlf0710crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 8, 2021
@JohnCSimonJohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 23, 2021
@bors

bors commented Jun 5, 2021

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (presumably #86006) made this pull request unmergeable. Please resolve the merge conflicts.

@crlf0710crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 26, 2021
@JohnCSimonJohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 12, 2021
@camelidcamelid added A-FFI Area: Foreign function interface (FFI) A-raw-pointers Area: raw pointers, MaybeUninit, NonNull labels Jul 29, 2021
@JohnCSimonJohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 15, 2021
@inquisitivecrystalinquisitivecrystal added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 24, 2021
@wesleywiser

Copy link
Copy Markdown
Member

r? rust-lang/compiler-team

@nagisa

Copy link
Copy Markdown
Member

I believe this is something @rust-lang/lang should take a look at before this is merged. Implementation-wise this LGTM.

@nagisanagisa added T-lang Relevant to the language team I-nominated and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 5, 2021
@joshtriplett

Copy link
Copy Markdown
Member

While there's no fundamental reason this couldn't work, we already have c_void for this, and it's useful to unify around one such type rather than two. (Hence the "voidpocalypse" some time ago.)

For that reason, I don't think we should do this.

As an alternative, perhaps we could emit a structured suggestion if we see those types used in FFI, suggesting the use of c_void instead?

@dtolnay

dtolnay commented Sep 7, 2021

Copy link
Copy Markdown
MemberAuthor

@joshtriplett in pure Rust to Rust FFI (where C is not involved), c_void doesn't seem appropriate. Rust code idiomatically uses ptr to () as the unspecified pointer type, as seen in RawWakerVTable and to_raw_parts in the standard library. I think that c_void should only come up when talking to C.

@joshtriplett

Copy link
Copy Markdown
Member

We discussed this in today's @rust-lang/lang meeting.

I would still like to see a lint on extern "C" usage of pointers to () that points to c_void instead. And from a different perspective, several people (myself included) felt like it'd be fine to allow pointers to T in general.

But for now, this seems like a good change, and a conservative one:

@rfcbot merge

@rfcbot

rfcbot commented Sep 7, 2021

Copy link
Copy Markdown

Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbotrfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 7, 2021
@nikomatsakis

Copy link
Copy Markdown
Contributor

@bors reviewed

This seems to be setting a kind of precedent that the "C ABI" should not warn about things that are reasonable but which may not be idiomatic (and that the latter would be a distinct lint). Does that seem correct?

@joshtriplett

Copy link
Copy Markdown
Member

"reasonable but not idiomatic" seems like exactly the case for which we should accept the code but emit a lint.

@nikomatsakis

Copy link
Copy Markdown
Contributor

@joshtriplett well, this is ultimately a lint no matter what, right? So the question is more like: what is the domain of this lint?

@scottmcm

Copy link
Copy Markdown
Member

Hmm, *const () is arguably just temporary while waiting for extern types (#43467), right? Is that something we can push on -- at least as a canonical one in core -- now that Thin exists? Or make some kind of #[repr(transparent)] struct OpaquePtr(*const ()); to suggest in this case?

As for this specifically, I dunno. I could see both "just use c_void, since it's in core and easy" or "well, if there's not the best option available yet then it shouldn't be linting for () until we can have a better suggestion".

@nagisanagisa added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 13, 2021
@rfcbotrfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Sep 14, 2021
@rfcbot

Copy link
Copy Markdown

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbotrfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Sep 14, 2021
@rfcbotrfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Sep 24, 2021
@rfcbot

Copy link
Copy Markdown

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@rfcbotrfcbot added the to-announce Announce this issue on triage meeting label Sep 24, 2021
@apirainoapiraino removed the to-announce Announce this issue on triage meeting label Sep 30, 2021
@nagisa

Copy link
Copy Markdown
Member

@bors r+

@bors

bors commented Oct 2, 2021

Copy link
Copy Markdown
Collaborator

📌 Commit abfad74 has been approved by nagisa

@borsbors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Oct 2, 2021
@bors

bors commented Oct 3, 2021

Copy link
Copy Markdown
Collaborator

⌛ Testing commit abfad74 with merge c70b35e...

@bors

bors commented Oct 3, 2021

Copy link
Copy Markdown
Collaborator

☀️ Test successful - checks-actions
Approved by: nagisa
Pushing c70b35e to master...

@borsbors added the merged-by-bors This PR was explicitly merged by bors. label Oct 3, 2021
@bors
bors merged commit c70b35e into rust-lang:masterOct 3, 2021
@rustbotrustbot added this to the 1.57.0 milestone Oct 3, 2021
@dtolnay
dtolnay deleted the ptrunit branch October 3, 2021 03:29
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (c70b35e): comparison url.

Summary: This benchmark run did not return any relevant changes.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

@dtolnaydtolnay removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. labels Jan 14, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-FFIArea: Foreign function interface (FFI)A-raw-pointersArea: raw pointers, MaybeUninit, NonNulldisposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.merged-by-borsThis PR was explicitly merged by bors.T-langRelevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

18 participants

@dtolnay@rust-highfive@tspiteri@bors@wesleywiser@nagisa@joshtriplett@rfcbot@nikomatsakis@scottmcm@rust-timer@crlf0710@JohnCSimon@apiraino@matthewjasper@inquisitivecrystal@camelid@rustbot