fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
3.61k stars 254 forks source link

frb v2 - C++ keywords #1890

Closed erdemyerebasmaz closed 2 days ago

erdemyerebasmaz commented 1 month ago

Describe the bug

Rust structs field that are the same name with C++ Reserved Words, which are not shared with Rust or Dart, are now generated with an underscore suffix on the wire object with frb v2.

Generator completes without issues and this only becomes apparent upon using the bindings.

Related discussion:

Steps to reproduce

Create a struct with a field that uses C++ keywords that are not shared with Rust or Dart, friend, goto, template etc., they can be of any type and generate bindings for it.

Logs

logs.txt (8K lines, a bit laggy)

Expected behavior

Keywords that are not reserved by Rust or Dart should generate as is.

Generated binding code

For a struct that has a field named template

final class wire_cst_symbol extends ffi.Struct {
  external ffi.Pointer<wire_cst_list_prim_u_8_strict> grapheme;

  external ffi.Pointer<wire_cst_list_prim_u_8_strict> template_;

  external ffi.Pointer<ffi.Bool> rtl;

  external ffi.Pointer<ffi.Uint32> position;
}
  @protected
  void cst_api_fill_to_wire_symbol(Symbol apiObj, wire_cst_symbol wireObj) {
    wireObj.grapheme = cst_encode_opt_String(apiObj.grapheme);
    wireObj.template = cst_encode_opt_String(apiObj.template);
    wireObj.rtl = cst_encode_opt_box_autoadd_bool(apiObj.rtl);
    wireObj.position = cst_encode_opt_box_autoadd_u_32(apiObj.position);
  }

OS

macOS Sonoma 14.4.1

Version of flutter_rust_bridge_codegen

2.0.0-dev.32

Flutter info

[✓] Flutter (Channel stable, 3.19.5, on macOS 14.4.1 23E224 darwin-arm64, locale en-TR)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.88.1)

Version of clang++

Apple clang version 15.0.0 (clang-1500.3.9.4)
Target: arm64-apple-darwin23.4.0
Thread model: posix

Additional context

Error received on building the app that uses bindings:

../breez-sdk/libs/sdk-flutter/lib/generated/frb_generated.io.dart:3373:13: Error: The setter 'template' isn't defined for the class 'wire_cst_symbol'.
 - 'wire_cst_symbol' is from 'package:breez_sdk/generated/frb_generated.io.dart' ('../breez-sdk/libs/sdk-flutter/lib/generated/frb_generated.io.dart').
Try correcting the name to the name of an existing setter, or defining a setter or field named 'template'.
    wireObj.template = cst_encode_opt_String(apiObj.template);
            ^^^^^^^^
Target kernel_snapshot failed: Exception
welcome[bot] commented 1 month ago

Hi! Thanks for opening your first issue here! :smile:

fzyzcjy commented 1 month ago

As mentioned in the discussion, feel free to fix it, alternatively I will work on it in the next batch (hopefully within a week) :)

EDIT: Sorry for the latency, but I am really busy recently, and this is not a critical bug and has workarounds, thus I hope to work on it a bit later.

fzyzcjy commented 1 week ago

I see: It seems that cbindgen considers c++ keywords and add postfix when detects one. https://github.com/mozilla/cbindgen/blob/ca78140c01518a655355f84da1f3872939123b66/src/bindgen/reserved.rs#L8

A fix may be, let frb understand this (e.g. when reading/writing the field, use the name template_ instead of template).

Another workaround in order to use this name is that, remove full_dep: true in your frb configuration, then the codec will not use cbindgen and thus no this issue.