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

[Bug] C bridging header "field has incomplete type" with recursive type definitions #1215

Closed
tmpfs opened this issue May 17, 2023 · 5 comments
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@tmpfs
Copy link
Contributor

tmpfs commented May 17, 2023

Describe the bug

Using FRB 1.75.3 codegen is successful but there are errors compiling the generated header files.

/Users/muji/git/sos/gui/macos/Runner/api_generated.h:119:22: error: field has incomplete type 'struct wire_Secret'

When defining recursive type definitions the order of the C struct declarations are incorrect which yields this error trying to compile on MacOS.

For now my only workaround is to re-order the definitions manually but it would be great to not have this overhead if the fix is not too difficult.

I have an enum where each variant has a user_data field and in the UserData struct is a collection of fields, so the type definitions look like this:

Secret -> UserData -> SecretRow -> Secret

Can anyone with knowledge of the FRB codebase indicate the difficulty to fix this problem?

Codegen logs with RUST_LOG=debug environment variable

N/A

To Reproduce

No response

Expected behavior

No response

Generated binding code

No response

OS

No response

Version of flutter_rust_bridge_codegen

No response

Flutter info

No response

Version of clang++

No response

Version of ffigen

No response

Additional context

No response

@tmpfs tmpfs added the bug Something isn't working label May 17, 2023
@tmpfs
Copy link
Contributor Author

tmpfs commented May 17, 2023

As a temporary crude hack I have added this to the end of build.rs:

fn reorder_recursive_types() {
    let files = &[
        "../ios/Runner/api_generated.h",
        "../macos/Runner/api_generated.h",
    ];

    let needle = r#"typedef struct wire_SecretRow {
  struct wire_uint_8_list *id;
  struct wire_SecretMeta meta;
  struct wire_Secret secret;
} wire_SecretRow;

typedef struct wire_list_secret_row {
  struct wire_SecretRow *ptr;
  int32_t len;
} wire_list_secret_row;

typedef struct wire_UserData {
  struct wire_list_secret_row *fields;
  struct wire_uint_8_list *comment;
  struct wire_uint_8_list *recovery_note;
} wire_UserData;
"#;

    let target = r#"typedef struct wire_Secret {
  int32_t tag;
  union SecretKind *kind;
} wire_Secret;
"#;

    for file in files {
        let contents =
            std::fs::read_to_string(file).expect("failed to read file");
        let contents = contents.replace(needle, "");
        let replacement = format!("{}\n{}", target, needle);
        let contents = contents.replace(target, &replacement);
        std::fs::write(file, contents).expect("failed to write file");
    }
}

@fzyzcjy
Copy link
Owner

fzyzcjy commented May 17, 2023

Hmm I suspect it is a bug of https://github.com/mozilla/cbindgen, which FRB uses to generate .h headers. For example, maybe show api_generated.rs to cbindgen repository and tell them that the generated .h is incorrect?

@tmpfs
Copy link
Contributor Author

tmpfs commented May 17, 2023

Thanks @fzyzcjy, I raised an issue here in the cbindgen repository.

@stale
Copy link

stale bot commented Jul 16, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Jul 16, 2023
@stale stale bot closed this as completed Jul 23, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 6, 2023

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants