Skip to content

[pigeon] Improve C++ field handling #2097

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

Merged
merged 10 commits into from
Jun 6, 2022

Conversation

stuartmorgan-g
Copy link
Contributor

@stuartmorgan-g stuartmorgan-g commented May 28, 2022

This implements non-nullable field support in the C++ generator, and fixes some cases where the C++ generation for nullable fields was assuming non-nullable values.

As part of this:

  • Factors data class generation out into helper methods.
  • Adds isNullable to HostDatatype to make it easier to track nullability across translation from Dart to host datatypes.
  • Ports the existing nullable and non-nullable field native unit tests from ObjC to C++, giving native unit test coverage of most of the changes here.
    • Adds a hook in the C++ generator to generate a test friend for a special namespace to facilitate these tests.
  • Enables building the non-nullable field file.
  • Adjusts the ivar, setter parameter, and getter return types to match nullability expectations.
    • Adds a second convenience setter for nullable types that takes non-nullable values, to facilitate use of literals when constructing objects on the C++ side.

Fixes flutter/flutter#104278

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

///
/// See [_writeDataClassImplementation] for the corresponding declaration.
/// This is intended to be added to the header.
void _writeDataClassDeclaration(Indent indent, Class klass, Root root,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved these without changes as the very first commit, so excluding the first commit from the review will make it easier to see the substantive changes.

}

// TODO(stuartmorgan): Audit all uses of this and convert them to context-based
// methods like those above. Code still using this method may well have bugs.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suspect that there are at least some bugs similar to the ones I fixed for field types in other places, but I wanted to limit the scope of this PR. We should get more of the native tests online in follow-ups to flush out other bugs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. This PR removes fields from being unique_ptrs, so the use of this method should be greatly reduced here.

}

// TODO(stuartmorgan): Audit all uses of this and convert them to context-based
// methods like those above. Code still using this method may well have bugs.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. This PR removes fields from being unique_ptrs, so the use of this method should be greatly reduced here.

@azchohfi
Copy link
Contributor

azchohfi commented Jun 3, 2022

@stuartmorgan everything is working fine on my end.
Only one question:
The setters are no longer being generated with const args. Is that expected, or was it an oversight?

@stuartmorgan-g
Copy link
Contributor Author

The setters are no longer being generated with const args. Is that expected, or was it an oversight?

I assume you mean the non-null POD case? For the declaration it doesn't do anything (https://abseil.io/tips/109), and for the definition it seemed like pointless boilerplate in the generated code (not to mention extra work in the generator) given that it's a one-line implementation, so there's not really any room to accidentally modify the parameter.

@azchohfi
Copy link
Contributor

azchohfi commented Jun 6, 2022

I assume you mean the non-null POD case? For the declaration it doesn't do anything (https://abseil.io/tips/109), and for the definition it seemed like pointless boilerplate in the generated code (not to mention extra work in the generator) given that it's a one-line implementation, so there's not really any room to accidentally modify the parameter.

I was mostly thinking about compiler optimizations due to it being const, but I don't think it would change much, if at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[pigeon] Support non-null fields for Windows C++
3 participants