reflect/protodesc: fix initialization bug

This fixes a bug introduced by CL/182360.

Overview of the problem:
* CL/182360 removes the internal/prototype package, such that
protodesc was re-implemented using internal/filedesc.
* As a result of that change, resolving internal dependencies became
the responsibility of protodesc.
* Dependency resolution used the following two-pass algorithm:
	1) first pass derives the full name of all declarations
	2) second pass fully initializes each descriptor declaration,
	now being able to resolve local dependencies from the previous step.
* When the second pass looks up a local dependency, it is guaranteed to
find it, but it is not guaranteed that the dependency has been initialized
(since it may appear later on). This is problematic for default enum values
since it implies that the enum dependency may not be sufficiently
initialized to be able to query its set of values, leading to panics.
* CL/182360 recognized the problem and attempted to enforce an initialization
ordering where nested enums were always initialized before the body of the
message declaration itself.
* However, that ordering fails to enforce that that enum declarations outside
the parent tree are initialized beforehand. For example, referring to an
enum value that is declared within a sibling of the parent message.
* This CL fixes the problem with a three-pass algorithm:
	1) first pass derives the full name *and* fully initialize the
	entire descriptor *except* for dependency references (i.e., type_name).
	2) second pass only resolves dependency references,
	where we do not need to worry about initialization ordering.
	3) third pass validates the descriptors are well-formed.
	This can now depend on all information being fully initialized.
* While a lot of code moves, this change is actually very mechanical.
Other than split things apart, no new logic is introduced nor removed.

Change-Id: Ia91d4aade8f6187c19d704d43ae96b3b9d276792
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/184297
Reviewed-by: Damien Neil <dneil@google.com>
6 files changed
tree: cb1f0ee8cdd9d714e4d3a605d0d837a75db8109d
  1. benchmarks/
  2. cmd/
  3. compiler/
  4. encoding/
  5. internal/
  6. proto/
  7. reflect/
  8. runtime/
  9. testing/
  10. types/
  11. .gitignore
  12. .travis.yml
  13. AUTHORS
  14. CONTRIBUTING.md
  15. CONTRIBUTORS
  16. download_benchdata.bash
  17. go.mod
  18. go.sum
  19. integration_test.go
  20. LICENSE
  21. PATENTS
  22. README.md
  23. regenerate.bash
  24. test.bash
README.md

Next Generation Go Protocol Buffers

WARNING: This repository is in active development. There are no guarantees about API stability. Breaking changes will occur until a stable release is made and announced.

This repository is for the development of the next major Go implementation of protocol buffers. This library makes breaking API changes relative to the existing Go protobuf library. Of particular note, this API aims to make protobuf reflection a first-class feature of the API and implements the protobuf ecosystem in terms of reflection.

Design Documents

List of relevant design documents:

Contributing

We appreciate community contributions. See CONTRIBUTING.md.

Reporting Issues

Issues regarding the new API can be filed at github.com/golang/protobuf. Please use a APIv2: prefix in the title to make it clear that the issue is regarding the new API work.