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

Guarantee result nonzero for FFI #110968

Closed

Conversation

zacklukem
Copy link
Contributor

Implements the required comipler changes for result_ffi_guarantees (rust-lang/rfcs#3391)

Allows Result<T, E> to be used in extern "C" functions where one of (T, E) is a non-zero type and one of (T, E) is a zero sized type. e.g. Result<NonZeroI32, ()>

Adds test cases to ensure that the abi for Result and Option types with nonzero representation remains consistent

tracking issue: #110503

Implements the required comipler changes for result_ffi_guarantees
(rust-lang#3391)

Allows `Result<T, E>` to be used in extern "C" functions where one of
(T, E) is a non-zero type and one of (T, E) is a zero sized type.
e.g. `Result<NonZeroI32, ()>`

Adds test cases to ensure that the abi for Result and Option types
with nonzero representation remains consistent
@rustbot
Copy link
Collaborator

rustbot commented Apr 28, 2023

r? @jackh726

(rustbot has picked a reviewer for you, use r? to override)

@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 Apr 28, 2023
@apiraino
Copy link
Contributor

r? compiler

@rustbot rustbot assigned WaffleLapkin and unassigned jackh726 May 17, 2023
Comment on lines +781 to +787
let is_zst = |field: &FieldDef, variant: &VariantDef| {
let param_env = cx.tcx.param_env(variant.def_id);
let field_ty = field.ty(cx.tcx, substs);
cx.tcx
.layout_of(param_env.and(field_ty))
.map_or(false, |layout| layout.is_zst())
};
Copy link
Member

@WaffleLapkin WaffleLapkin May 18, 2023

Choose a reason for hiding this comment

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

This is not sufficient, see the RFC:

Result<T, E> where either T or E meet all of the following conditions:

  • Is a zero-sized type with alignment 1 (a "1-ZST").
  • Has no fields.
  • Does not have the #[non_exhaustive] attribute.

(emph. mine)

@@ -770,8 +773,26 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
debug!("is_repr_nullable_ptr(cx, ty = {:?})", ty);
Copy link
Member

Choose a reason for hiding this comment

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

Please also update the documentation comment on this function.

) -> FfiResult<'tcx> {
let field_ty = field.ty(self.cx.tcx, substs);
if field_ty.has_opaque_types() {
if field_ty.is_unit() && allow_unit_type {
Copy link
Member

Choose a reason for hiding this comment

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

This only checks for unit, shouldn't it be the same check as above? (ZST + no fields + exhaustive)

@@ -770,8 +773,26 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
debug!("is_repr_nullable_ptr(cx, ty = {:?})", ty);
Copy link
Member

Choose a reason for hiding this comment

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

Also, the doc comment seems to suggest we have similar code in codegen, but I don't see it changed in this PR.

@WaffleLapkin WaffleLapkin added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 18, 2023
@WaffleLapkin
Copy link
Member

cc @Lokathor

@bors
Copy link
Contributor

bors commented Jul 14, 2023

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

@JohnCSimon
Copy link
Member

@zacklukem
ping from triage - can you post your status on this PR? There hasn't been an update in a few months. Thanks!

FYI: when a PR is ready for review, send a message containing
@rustbot ready to switch to S-waiting-on-review so the PR is in the reviewer's backlog.

([], [field]) | ([field], []) => field.ty(cx.tcx, substs),
// If one variant has a ZST and the other has a single field (e.g. Result<T, ()> or Result<(), T>)
Copy link
Member

Choose a reason for hiding this comment

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

All of these uses of "ZST" here should become "1-ZST" -- it is quite important that we exclude ZST with alignment requirements. There now is a layout.is_1zst method that can and should be used here.

@Dylan-DPC
Copy link
Member

Closing this as inactive. Feel free to reöpen this pr or create a new pr if you get the time to work on this. Thanks

@Dylan-DPC Dylan-DPC closed this Oct 24, 2023
@Dylan-DPC Dylan-DPC added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. 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.

9 participants