Skip to content

Default initialize properties in custom structs #52677

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

RSNara
Copy link
Contributor

@RSNara RSNara commented Jul 17, 2025

Summary:
If a native module spec declares a custom type:

https://www.internalfb.com/code/fbsource/[f6aeb413dbc3]/xplat/js/RKJSModules/Libraries/hsr/tm/code/editable_object/NativeEditableObjectModule.js?lines=151-154

The codegen will generate a c++ struct for that type, like so:

template <typename P0, typename P1>
struct NativeEditableObjectModuleChangedProperty {
  P0 propertyPath;
  P1 value;

  bool operator==(const NativeEditableObjectModuleChangedProperty &other) const {
    return propertyPath == other.propertyPath && value == other.value;
  }
};

Problem

People can sometimes forget to default initialize this struct:

In this scenario, all the members contain garbage data.

Changes

This diff ensures that all the members are always default initialized, like so:

template <typename P0, typename P1>
struct NativeEditableObjectModuleChangedProperty {
  P0 propertyPath{};
  P1 value{};

  bool operator==(const NativeEditableObjectModuleChangedProperty &other) const {
    return propertyPath == other.propertyPath && value == other.value;
  }
};

This way, even if you forget to default initailize the struct, the members will still be default initialized.

Concerns/Risks

This could cause compilation issues in some of our existing native modules. (if one of the member types doesn't have a default constructor).

Code could start behaving differently, now that all the members are default initialized. (my hunch is that the risk from this causing a problem is low).

Changelog: [General][Changed] c++ tm codegen: Default initialize properties in custom structs

Reviewed By: christophpurrer

Differential Revision: D77315742

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 17, 2025
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D77315742

Summary:
If a native module spec declares a custom type:

https://www.internalfb.com/code/fbsource/[f6aeb413dbc3]/xplat/js/RKJSModules/Libraries/hsr/tm/code/editable_object/NativeEditableObjectModule.js?lines=151-154

The codegen will generate a c++ struct for that type, like so:

```
template <typename P0, typename P1>
struct NativeEditableObjectModuleChangedProperty {
  P0 propertyPath;
  P1 value;

  bool operator==(const NativeEditableObjectModuleChangedProperty &other) const {
    return propertyPath == other.propertyPath && value == other.value;
  }
};
```

## Problem
People can sometimes forget to default initialize this struct:
- **Report:** [post](https://fb.workplace.com/groups/rn.support/permalink/28506993098922601/)
- **Diff facebook#1:** D74868762
- **Diff facebook#2:** D77182083

In this scenario, all the members contain garbage data.

## Changes
This diff ensures that all the members are always default initialized, like so:

```
template <typename P0, typename P1>
struct NativeEditableObjectModuleChangedProperty {
  P0 propertyPath{};
  P1 value{};

  bool operator==(const NativeEditableObjectModuleChangedProperty &other) const {
    return propertyPath == other.propertyPath && value == other.value;
  }
};
```

This way, even if you forget to default initailize the struct, the members will still be default initialized.

## Concerns/Risks
This could cause compilation issues in some of our existing native modules. (if one of the member types doesn't have a default constructor).

Code could start behaving differently, now that all the members are default initialized. (my hunch is that the risk from this causing a problem is low).

Changelog: [General][Changed] c++ tm codegen: Default initialize properties in custom structs

Reviewed By: christophpurrer

Differential Revision: D77315742
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D77315742

@RSNara RSNara force-pushed the export-D77315742 branch from 1852eff to 7c34720 Compare July 17, 2025 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported p: Facebook Partner: Facebook Partner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants