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

Add UseResult annotation to rebuild #631

Merged
merged 5 commits into from
May 6, 2022
Merged

Add UseResult annotation to rebuild #631

merged 5 commits into from
May 6, 2022

Conversation

osa1
Copy link
Member

@osa1 osa1 commented Apr 25, 2022

This catches issues like #630 where we forgot to use return value of
rebuild. Since rebuild does not update in-place, not using the
return value is often a bug.

This adds meta as a dependency but it should not affect generated
code. We may also want to use meta in the future for things like:

  • Annotating GeneratedMessage methods that are supposed to be used by
    the generated code with @protected.

  • Annotating generated message classes with @sealed. This help with
    maintaining backwards compatibility when adding new members to
    generated messages or to GeneratedMessage.

This catches issues like google#630 where we forgot to use return value of
`rebuild`. Since `rebuild` does not update in-place, not using the
return value is often a bug.

This adds `meta` as a dependency but it should not affect generated
code. We may also want to use `meta` in the future for things like:

- Annotating `GeneratedMessage` methods that are supposed to be used by
  the generated code with `@protected`.

- Annotating generated message classes with `@sealed`. This help with
  maintaining backwards compatibility when adding new members to
  generated messages or to `GeneratedMessage`.
@osa1 osa1 requested a review from sigurdm April 25, 2022 12:00
Copy link
Collaborator

@sigurdm sigurdm left a comment

Choose a reason for hiding this comment

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

LGTM

Seems there is a test that needs to be fixed.

Maybe @rakudrama has something to say about adding the dependency.

I think package:meta is so ubiquitous that package:protobuf can afford to depend on it.

@osa1
Copy link
Member Author

osa1 commented Apr 25, 2022

Seems there is a test that needs to be fixed.

Hm.. I'm a bit surprised that this line generates a unused_result warning:

var copy1 = foo.deepCopy().freeze().rebuild((_) {}) as Foo;

The result is clearly used. It's casted to Foo and then assigned to copy1. Is this an SDK bug?

So this generates a warning:

    var copy1 = foo.deepCopy().freeze().rebuild((_) {}) as Foo;
    expectOneofNotSet(copy1);

But this does not:

    var copy1 = foo.deepCopy().freeze().rebuild((_) {});
    expectOneofNotSet(copy1 as Foo);

@osa1
Copy link
Member Author

osa1 commented Apr 25, 2022

Hm.. I'm a bit surprised that this line generates a unused_result warning:

Reported this as dart-lang/sdk#48879.

@osa1
Copy link
Member Author

osa1 commented Apr 28, 2022

The issue with the warning described above is fixed in Dart main branch but it will be a while until it's released. I'm not sure whether to wait for the release with the fix before merging this or work around the issue.

@sigurdm
Copy link
Collaborator

sigurdm commented Apr 28, 2022

The issue with the warning described above is fixed in Dart main branch but it will be a while until it's released. I'm not sure whether to wait for the release with the fix before merging this or work around the issue.

I think it is ok to add a // ignore: comment in the test.

@osa1
Copy link
Member Author

osa1 commented May 5, 2022

@rakudrama any thoughts on adding meta as a dependency to protobuf?

@rakudrama
Copy link
Contributor

@rakudrama any thoughts on adding meta as a dependency to protobuf?

I don't foresee any problems.

@rakudrama
Copy link
Contributor

    var copy1 = foo.deepCopy().freeze().rebuild((_) {}) as Foo;
    expectOneofNotSet(copy1);

Side comment - rebuild is generic, so why do we need the cast?

@osa1
Copy link
Member Author

osa1 commented May 5, 2022

Side comment - rebuild is generic, so why do we need the cast?

This is because GeneratedMessage.freeze's return type is GeneratedMessage:

GeneratedMessage freeze() {
_fieldSet._markReadOnly();
return this;
}
so the concrete type is lost after freeze.

It would be great if we could use a type like This to refer to the concrete type in a superclass. If we had that freeze could have This as return type, and calls to freeze would not upcast the receiver.

@osa1 osa1 merged commit 46df68a into google:master May 6, 2022
@osa1 osa1 deleted the use_rebuild branch May 6, 2022 07:19
osa1 added a commit to osa1/protobuf.dart that referenced this pull request May 6, 2022
@osa1
Copy link
Member Author

osa1 commented May 16, 2022

This revealed another SDK bug when updating a protobuf user to the current master branch: dart-lang/sdk#49023

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.

3 participants