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

Remove mismatch between default value of struct field in C++ and other languages #1628

Conversation

pwrobeldev
Copy link
Contributor

---------- Motivation ----------

This change fixes a bug related to invalid default value of nullable
field, which uses struct type. When field was nullable and its default
value was set to default-constructed structure, then the generated
code in C++ was invalid, because the field was initialized to std::nullopt
instead of default-constructed struct.

---------- Contents of the change ----------

This change:

  • implements new functional tests to confirm the invalid behavior
  • adds new LIME input to smoke tests to point out which lines are
    invalid
  • implements a fix in CppNameResolver.kt to remove superflous
    std::optional prefix when resolving default value

This change is used to showcase the invalid behavior
of the code generator.

It implements new functional tests that verify the
default values that are set in LIME file for nullable
fields.

The default value is invalid in the case of default
constructed object in C++. There is a mismatch between
C++ and Java/Swift/Dart.

Signed-off-by: Patryk Wrobel <[email protected]>
This change introudces the same LIME structures
that the previous commit, but in smoke tests.

The following LIME syntax is used:
```
  someField1: SomeImmutableStructWithDefaults? = {}
  someField2: ImmutableStructWithCollections? = {}
```

It results in the following invalid C++ code (fields are nullopt):
```
  some_field1
    = std::optional<SomeImmutableStructWithDefaults>{};

  some_field2
    = std::optional<ImmutableStructWithCollections>{};
```

The generated C++ code for default values initializes fields
to nullopt instead of default-constructed structure. This
behavior is invalid and does not occur in other languages.

Signed-off-by: Patryk Wrobel <[email protected]>
This change ensures that fields, which use
std::optional are initialized correctly, when
default value is default-constructed struct.

Usage of typename wrapped in std::optional was
redundant and caused problems with initialized
value and compilation.

In this commit usage of std::optional prefix is
removed when resolving default value.

Signed-off-by: Patryk Wrobel <[email protected]>
@pwrobeldev pwrobeldev requested a review from Hsilgos November 28, 2024 08:55
@pwrobeldev pwrobeldev merged commit c583171 into master Nov 28, 2024
17 checks passed
@pwrobeldev pwrobeldev deleted the pwrobeldev/mismatch-between-field-default-value-in-cpp-and-other-languages branch November 28, 2024 11:12
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