protobuf-go/reflect/protodesc/desc_init.go

248 lines
9.4 KiB
Go
Raw Normal View History

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>
2019-06-29 07:05:22 +00:00
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package protodesc
import (
"google.golang.org/protobuf/internal/errors"
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>
2019-06-29 07:05:22 +00:00
"google.golang.org/protobuf/internal/filedesc"
"google.golang.org/protobuf/internal/strs"
"google.golang.org/protobuf/proto"
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>
2019-06-29 07:05:22 +00:00
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
type descsByName map[protoreflect.FullName]protoreflect.Descriptor
func (r descsByName) initEnumDeclarations(eds []*descriptorpb.EnumDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (es []filedesc.Enum, err error) {
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>
2019-06-29 07:05:22 +00:00
es = make([]filedesc.Enum, len(eds)) // allocate up-front to ensure stable pointers
for i, ed := range eds {
e := &es[i]
e.L2 = new(filedesc.EnumL2)
if e.L0, err = r.makeBase(e, parent, ed.GetName(), i, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if opts := ed.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.EnumOptions)
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>
2019-06-29 07:05:22 +00:00
e.L2.Options = func() protoreflect.ProtoMessage { return opts }
}
for _, s := range ed.GetReservedName() {
e.L2.ReservedNames.List = append(e.L2.ReservedNames.List, protoreflect.Name(s))
}
for _, rr := range ed.GetReservedRange() {
e.L2.ReservedRanges.List = append(e.L2.ReservedRanges.List, [2]protoreflect.EnumNumber{
protoreflect.EnumNumber(rr.GetStart()),
protoreflect.EnumNumber(rr.GetEnd()),
})
}
if e.L2.Values.List, err = r.initEnumValuesFromDescriptorProto(ed.GetValue(), e, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
}
return es, nil
}
func (r descsByName) initEnumValuesFromDescriptorProto(vds []*descriptorpb.EnumValueDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (vs []filedesc.EnumValue, err error) {
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>
2019-06-29 07:05:22 +00:00
vs = make([]filedesc.EnumValue, len(vds)) // allocate up-front to ensure stable pointers
for i, vd := range vds {
v := &vs[i]
if v.L0, err = r.makeBase(v, parent, vd.GetName(), i, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if opts := vd.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.EnumValueOptions)
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>
2019-06-29 07:05:22 +00:00
v.L1.Options = func() protoreflect.ProtoMessage { return opts }
}
v.L1.Number = protoreflect.EnumNumber(vd.GetNumber())
}
return vs, nil
}
func (r descsByName) initMessagesDeclarations(mds []*descriptorpb.DescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ms []filedesc.Message, err error) {
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>
2019-06-29 07:05:22 +00:00
ms = make([]filedesc.Message, len(mds)) // allocate up-front to ensure stable pointers
for i, md := range mds {
m := &ms[i]
m.L2 = new(filedesc.MessageL2)
if m.L0, err = r.makeBase(m, parent, md.GetName(), i, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if opts := md.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.MessageOptions)
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>
2019-06-29 07:05:22 +00:00
m.L2.Options = func() protoreflect.ProtoMessage { return opts }
m.L1.IsMapEntry = opts.GetMapEntry()
m.L1.IsMessageSet = opts.GetMessageSetWireFormat()
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>
2019-06-29 07:05:22 +00:00
}
for _, s := range md.GetReservedName() {
m.L2.ReservedNames.List = append(m.L2.ReservedNames.List, protoreflect.Name(s))
}
for _, rr := range md.GetReservedRange() {
m.L2.ReservedRanges.List = append(m.L2.ReservedRanges.List, [2]protoreflect.FieldNumber{
protoreflect.FieldNumber(rr.GetStart()),
protoreflect.FieldNumber(rr.GetEnd()),
})
}
for _, xr := range md.GetExtensionRange() {
m.L2.ExtensionRanges.List = append(m.L2.ExtensionRanges.List, [2]protoreflect.FieldNumber{
protoreflect.FieldNumber(xr.GetStart()),
protoreflect.FieldNumber(xr.GetEnd()),
})
var optsFunc func() protoreflect.ProtoMessage
if opts := xr.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.ExtensionRangeOptions)
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>
2019-06-29 07:05:22 +00:00
optsFunc = func() protoreflect.ProtoMessage { return opts }
}
m.L2.ExtensionRangeOptions = append(m.L2.ExtensionRangeOptions, optsFunc)
}
if m.L2.Fields.List, err = r.initFieldsFromDescriptorProto(md.GetField(), m, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if m.L2.Oneofs.List, err = r.initOneofsFromDescriptorProto(md.GetOneofDecl(), m, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if m.L1.Enums.List, err = r.initEnumDeclarations(md.GetEnumType(), m, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if m.L1.Messages.List, err = r.initMessagesDeclarations(md.GetNestedType(), m, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if m.L1.Extensions.List, err = r.initExtensionDeclarations(md.GetExtension(), m, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
}
return ms, nil
}
func (r descsByName) initFieldsFromDescriptorProto(fds []*descriptorpb.FieldDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (fs []filedesc.Field, err error) {
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>
2019-06-29 07:05:22 +00:00
fs = make([]filedesc.Field, len(fds)) // allocate up-front to ensure stable pointers
for i, fd := range fds {
f := &fs[i]
if f.L0, err = r.makeBase(f, parent, fd.GetName(), i, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if opts := fd.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.FieldOptions)
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>
2019-06-29 07:05:22 +00:00
f.L1.Options = func() protoreflect.ProtoMessage { return opts }
f.L1.IsWeak = opts.GetWeak()
f.L1.HasPacked = opts.Packed != nil
f.L1.IsPacked = opts.GetPacked()
}
f.L1.Number = protoreflect.FieldNumber(fd.GetNumber())
f.L1.Cardinality = protoreflect.Cardinality(fd.GetLabel())
if fd.Type != nil {
f.L1.Kind = protoreflect.Kind(fd.GetType())
}
if fd.JsonName != nil {
f.L1.JSONName.Init(fd.GetJsonName())
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>
2019-06-29 07:05:22 +00:00
}
}
return fs, nil
}
func (r descsByName) initOneofsFromDescriptorProto(ods []*descriptorpb.OneofDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (os []filedesc.Oneof, err error) {
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>
2019-06-29 07:05:22 +00:00
os = make([]filedesc.Oneof, len(ods)) // allocate up-front to ensure stable pointers
for i, od := range ods {
o := &os[i]
if o.L0, err = r.makeBase(o, parent, od.GetName(), i, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if opts := od.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.OneofOptions)
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>
2019-06-29 07:05:22 +00:00
o.L1.Options = func() protoreflect.ProtoMessage { return opts }
}
}
return os, nil
}
func (r descsByName) initExtensionDeclarations(xds []*descriptorpb.FieldDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (xs []filedesc.Extension, err error) {
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>
2019-06-29 07:05:22 +00:00
xs = make([]filedesc.Extension, len(xds)) // allocate up-front to ensure stable pointers
for i, xd := range xds {
x := &xs[i]
x.L2 = new(filedesc.ExtensionL2)
if x.L0, err = r.makeBase(x, parent, xd.GetName(), i, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if opts := xd.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.FieldOptions)
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>
2019-06-29 07:05:22 +00:00
x.L2.Options = func() protoreflect.ProtoMessage { return opts }
x.L2.IsPacked = opts.GetPacked()
}
x.L1.Number = protoreflect.FieldNumber(xd.GetNumber())
x.L1.Cardinality = protoreflect.Cardinality(xd.GetLabel())
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>
2019-06-29 07:05:22 +00:00
if xd.Type != nil {
x.L1.Kind = protoreflect.Kind(xd.GetType())
}
if xd.JsonName != nil {
x.L2.JSONName.Init(xd.GetJsonName())
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>
2019-06-29 07:05:22 +00:00
}
}
return xs, nil
}
func (r descsByName) initServiceDeclarations(sds []*descriptorpb.ServiceDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ss []filedesc.Service, err error) {
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>
2019-06-29 07:05:22 +00:00
ss = make([]filedesc.Service, len(sds)) // allocate up-front to ensure stable pointers
for i, sd := range sds {
s := &ss[i]
s.L2 = new(filedesc.ServiceL2)
if s.L0, err = r.makeBase(s, parent, sd.GetName(), i, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if opts := sd.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.ServiceOptions)
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>
2019-06-29 07:05:22 +00:00
s.L2.Options = func() protoreflect.ProtoMessage { return opts }
}
if s.L2.Methods.List, err = r.initMethodsFromDescriptorProto(sd.GetMethod(), s, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
}
return ss, nil
}
func (r descsByName) initMethodsFromDescriptorProto(mds []*descriptorpb.MethodDescriptorProto, parent protoreflect.Descriptor, sb *strs.Builder) (ms []filedesc.Method, err error) {
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>
2019-06-29 07:05:22 +00:00
ms = make([]filedesc.Method, len(mds)) // allocate up-front to ensure stable pointers
for i, md := range mds {
m := &ms[i]
if m.L0, err = r.makeBase(m, parent, md.GetName(), i, sb); err != nil {
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>
2019-06-29 07:05:22 +00:00
return nil, err
}
if opts := md.GetOptions(); opts != nil {
opts = proto.Clone(opts).(*descriptorpb.MethodOptions)
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>
2019-06-29 07:05:22 +00:00
m.L1.Options = func() protoreflect.ProtoMessage { return opts }
}
m.L1.IsStreamingClient = md.GetClientStreaming()
m.L1.IsStreamingServer = md.GetServerStreaming()
}
return ms, nil
}
func (r descsByName) makeBase(child, parent protoreflect.Descriptor, name string, idx int, sb *strs.Builder) (filedesc.BaseL0, error) {
if !protoreflect.Name(name).IsValid() {
return filedesc.BaseL0{}, errors.New("descriptor %q has an invalid nested name: %q", parent.FullName(), name)
}
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>
2019-06-29 07:05:22 +00:00
// Derive the full name of the child.
// Note that enum values are a sibling to the enum parent in the namespace.
var fullName protoreflect.FullName
if _, ok := parent.(protoreflect.EnumDescriptor); ok {
fullName = sb.AppendFullName(parent.FullName().Parent(), protoreflect.Name(name))
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>
2019-06-29 07:05:22 +00:00
} else {
fullName = sb.AppendFullName(parent.FullName(), protoreflect.Name(name))
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>
2019-06-29 07:05:22 +00:00
}
if _, ok := r[fullName]; ok {
return filedesc.BaseL0{}, errors.New("descriptor %q already declared", fullName)
}
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>
2019-06-29 07:05:22 +00:00
r[fullName] = child
// TODO: Verify that the full name does not already exist in the resolver?
// This is not as critical since most usages of NewFile will register
// the created file back into the registry, which will perform this check.
return filedesc.BaseL0{
FullName: fullName,
ParentFile: parent.ParentFile().(*filedesc.File),
Parent: parent,
Index: idx,
}, nil
}