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

Fix non-deterministic generation order #230

Merged
merged 2 commits into from
Jun 20, 2023

Conversation

NiwakaDev
Copy link
Collaborator

@NiwakaDev NiwakaDev commented Jun 17, 2023

Related to #225.

Before this commit, the integration tests sometimes fail, like so:

field has incomplete type 'struct swift_bridge$tuple$I32ResultTestOpaqueRustTypeString'

This PR fixes the issue.

@NiwakaDev
Copy link
Collaborator Author

NiwakaDev commented Jun 17, 2023

It seems like another issue happens:

Test session results, code coverage, and logs:
	Command CompileSwift failed with a nonzero exit code
	/Users/runner/Library/Developer/Xcode/DerivedData/SwiftRustIntegrationTestRunner-eunjzlpbulmkoedlbxmntxkdbksm/Logs/Test/Run-SwiftRustIntegrationTestRunner-2023.06.17_06-29-48-+0000.xcresult
	Cannot find 'swift_reflect_already_declared_struct' in scope
	Testing cancelled because the build failed.

@NiwakaDev
Copy link
Collaborator Author

I rebased #226 into this branch, and then I fixed the immediately above issue.

@NiwakaDev NiwakaDev requested a review from chinedufn June 17, 2023 07:13
func swift_reflect_already_declared_struct(arg: AlreadyDeclaredStructTest) -> AlreadyDeclaredStructTest {
arg
}

Copy link
Collaborator Author

@NiwakaDev NiwakaDev Jun 17, 2023

Choose a reason for hiding this comment

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

To fix the immediately above issue, I appended this function.

@NiwakaDev NiwakaDev changed the title Fix the non-deterministic generation order Fix non-deterministic generation order Jun 19, 2023
Copy link
Owner

@chinedufn chinedufn left a comment

Choose a reason for hiding this comment

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

AWESOME! Left some minor feedback but after that LGTM. Thanks so much for solving this problem. Solution looks good.

use quote::quote;

/// Verify that fields of a structure in a C header file are generated before the structure.
mod structure_fields_generated_before_structure {
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe

/// Verify that the type that if there is a `Result<Tuple, _>` the generated C header contains
/// the tuple's fields followed by the tuple's FFI representation followed by the Result FFI repr.
mod tuple_fields_generated_before_tuple_generated_before_result

extern "Rust" {
fn rust_func_return_result_tuple_transparent_enum(
succeed: bool,
) -> Result<(i32, ResultTestOpaqueRustType, String), i32>;
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe Result<(RustOpaqueType1, RustOpaqueType1), (RustOpaqueType2, RustOpaqueType2)> ?
(I can't remember if we allow tuples in the E of Result<T, E>)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't implement it, but it might work.

Here's a PR which adds support Result<tuple, _>
#213

@@ -16,6 +16,11 @@ struct Bookkeeping {
slice_types: HashSet<String>,
}

struct CFFiStructDeclarationBookkeeping {
Copy link
Owner

Choose a reason for hiding this comment

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

I think that most of the documentation that you put on CFFiStruct belongs here instead.

As in, we should document why we do this bookkeeping.

Comment on lines 36 to 40
/// For example, struct FooStruct {struct BarStruct bar_struct;} is generated in the order of
/// struct BarStruct {int32_t value;};
/// struct struct FooStruct {struct BarStruct bar_struct;};
///
/// Please see https://github.com/chinedufn/swift-bridge/pull/225.
Copy link
Owner

Choose a reason for hiding this comment

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

I think that we can move this example to CFFiStructDeclarationBookkeeping

The docs on CFFiStructDeclarationBookkeeping should

  1. Explain the problem
  2. Explain how we use the struct to bookkeeping struct solve the problem

I think we should delete the link to the pull request. Anything relevant should be explained in the code doc.

/// struct struct FooStruct {struct BarStruct bar_struct;};
///
/// Please see https://github.com/chinedufn/swift-bridge/pull/225.
pub(crate) struct CFFiStruct {
Copy link
Owner

Choose a reason for hiding this comment

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

CFfiStruct

@@ -0,0 +1,55 @@
use super::{CodegenTest, ExpectedCHeader, ExpectedRustTokens, ExpectedSwiftCode};
Copy link
Owner

Choose a reason for hiding this comment

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

Let's add the work header to this file. Maybe c_header_declaration_order_codegen_tests

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You mean:

Replace c_ffi_declaration_order_generation_tests with c_header_declaration_order_codegen_tests.rs.

?

Copy link
Owner

Choose a reason for hiding this comment

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

yes sorry i meant rename. was a typo

@@ -16,6 +16,11 @@ struct Bookkeeping {
slice_types: HashSet<String>,
}

struct CFFiStructDeclarationBookkeeping {
Copy link
Owner

Choose a reason for hiding this comment

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

CFfiStructDeclarationBookkeeping

@NiwakaDev
Copy link
Collaborator Author

NiwakaDev commented Jun 20, 2023

@chinedufn
I guess that I addressed your review. But, If we need to support Result<tuple, tuple>, we couldn't yet merge this PR into master.

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.

2 participants