Commit Graph

5096 Commits (main)

Author SHA1 Message Date
Protobuf Team Bot 5dfdd85d34 Auto-generate files after cl/634787159 2024-05-17 15:56:35 +00:00
Protobuf Team Bot 5b8e90f404 Refactor the way we turn on the optimization in StrongPointer.
Some versions of gcc seem to advertise __cpp_nontype_template_args but not
support the argument in some cases.
Only attempt the template parameter if we are using the optimized .reloc
approach.

Fixes https://github.com/protocolbuffers/protobuf/issues/16868

PiperOrigin-RevId: 634787159
2024-05-17 08:44:35 -07:00
Protobuf Team Bot 2f3242c576 Fix ClangTidy warnings
- Call std::string::insert to insert a character.
- Use assignment instead of CopyFrom.

PiperOrigin-RevId: 634610391
2024-05-16 20:06:16 -07:00
Anton Grbin 34c843d671 [7392] [cpp] Remove dead code path for map key of type enum in JSON parsing (#16567)
# Changes

Remove dead code path -- we don't allow enums to be map keys ([proto2 spec](https://protobuf.dev/programming-guides/proto2/#maps), [proto3 spec](https://protobuf.dev/programming-guides/proto3/#maps)). In other words the case block `case FieldDescriptor::TYPE_ENUM` is dead code. Potential enum type keys will be caught in `default: return lex.Invalid("unsupported map key type");` block below similar to other unsupported map key types like double.

# Motivation

While working on fixing `IgnoreUnknownEnumStringValueInMap` conformance tests for cpp ([related issue](https://github.com/protocolbuffers/protobuf/issues/7392)) I stumbled upon a bug where we pass the wrong `field` parameter to the enum parsing function.

In this scope:
* the variable `field` is a map field of the message that holds the map. This field is not of enum type, it's a repeated message of map entires.
* the variable `key_field` is the key field of the map message entry. This field is the enum type that we need to parse here.

The function is long, so I clarified it here:

```cpp
template <typename Traits>
absl::Status ParseMap(JsonLexer& lex, Field<Traits> field, Msg<Traits>& msg) {
  (..)
  return lex.VisitObject(
      [&](LocationWith<MaybeOwnedString>& key) -> absl::Status {
          (..)
          return Traits::NewMsg(
            field, msg,
            [&](const Desc<Traits>& type, Msg<Traits>& entry) -> absl::Status {
              auto key_field = Traits::KeyField(type);
              switch (Traits::FieldType(key_field)) {
                (..)
                case FieldDescriptor::TYPE_ENUM: {
                  MaybeOwnedString key_str = key.value;
                  auto e = ParseEnumFromStr<Traits>(lex, key_str, /** bug here **/ field);
```

The correct reference should be `key_field`.

Instead of fixing the bug and leaving the dead code, it's better to remove the dead block alltogether.

Closes #16567

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/16567 from noom:anton--7392--fix-map-key-nit d992b8a2a6
PiperOrigin-RevId: 634516984
2024-05-16 13:50:40 -07:00
Mike Kruskal 24b91a7fec Prohibit using features in the same file they're defined in.
This is an edge case we can't handle properly today.  Rather than allowing poorly defined behavior, we'll make this an error condition until we can actually support it.  In the future, it may be necessary to upgrade feature files to newer editions.

Closes #16756

PiperOrigin-RevId: 634512378
2024-05-16 13:37:35 -07:00
Sandy Zhang baeab50df0 Automated rollback of commit ec126a5069.
PiperOrigin-RevId: 634496540
2024-05-16 12:43:55 -07:00
Protobuf Team Bot c6cbaaeeee Devirtualize CheckTypeAndMergeFrom and use the existing function from
ClassData.
This removes a virtual function and reduces binary size.

PiperOrigin-RevId: 634413330
2024-05-16 08:57:31 -07:00
Protobuf Team Bot ca995da263 Reduce the cost of CheckTypeAndMergeFrom in LITE_RUNTIME objects when icf is
enabled.
DownCastToGenerated injects the strong reference to T, which makes all instances of CheckTypeAndMergeFrom unique and not candidates for identical code folding.
The strong reference is not needed here in the member function.

PiperOrigin-RevId: 634033477
2024-05-15 12:08:55 -07:00
Hong Shin 3574af2397 Internal Change
PiperOrigin-RevId: 634000296
2024-05-15 10:33:39 -07:00
Protobuf Team Bot cfea7596c1 Remove dead VTableName function.
PiperOrigin-RevId: 633999639
2024-05-15 10:30:55 -07:00
Protobuf Team Bot 5fdef5bfce Remove obsolete msgmut::or_default()
PiperOrigin-RevId: 633997832
2024-05-15 10:25:54 -07:00
Derek Benson 9f40411caf fix unneeded mut in enum gencode
PiperOrigin-RevId: 633955047
2024-05-15 08:03:37 -07:00
Mike Kruskal 6b9a81a042 Add extension declarations for known features.
This will prevent users from accidentally overriding these with different types (e.g. https://github.com/protocolbuffers/protobuf/issues/16757 and https://github.com/protocolbuffers/protobuf/issues/16756).

PiperOrigin-RevId: 633760581
2024-05-14 17:51:20 -07:00
Protobuf Team Bot 9b463ac048 Make the underlying type of the enum by 8-bits instead of using bitfields for
it.
It silences a warning in gcc 8/9.

PiperOrigin-RevId: 633719795
2024-05-14 15:12:09 -07:00
Protobuf Team Bot bb68eb22dd Implement new Debug API with redaction.
Implement emittingSingleLine TextFormat printer option.

PiperOrigin-RevId: 633672722
2024-05-14 12:40:20 -07:00
Protobuf Team Bot 448e326200 Use bool HasHasbits(const FieldDescriptor*) instead of manual checks.
It is not sufficient to check schema_.HasHasbits() followed by naively skipping
repeated and oneof fields as that can miss proto3 messages with message fields
and other scalar fields without "optional" keyword.

Use internal::cpp::HasHasbits(const FieldDescriptor*) instead.

PiperOrigin-RevId: 633633184
2024-05-14 10:40:02 -07:00
Protobuf Team Bot 2238cdc091 Add impl Clone for Rust proto owned messages.
PiperOrigin-RevId: 633265290
2024-05-13 10:57:10 -07:00
Protobuf Team Bot 7f23b700fe Introduce new API DownCastToMessage/DynamicCastToMessage as a replacement for down_cast/dynamic_cast from MessageLite to Message.
The new functions work even when RTTI is not present.

Also, add an assertion in DownCast to make sure we don't use it for message types. Message types have dedicated cast functions that should be used instead.

PiperOrigin-RevId: 633236522
2024-05-13 09:30:44 -07:00
Protobuf Team Bot fad4736785 Auto-generate files after cl/633224191 2024-05-13 16:00:07 +00:00
Sandy Zhang ce026abf92 Work around windows path length limitations by moving immutable -> full in open source (new generator paths) and shortening long file names.
Re-enable windows bazel test.

PiperOrigin-RevId: 633224191
2024-05-13 08:48:40 -07:00
Derek Benson f2d8c2bdd8 Add a reserve method on ProxiedInRepeated
PiperOrigin-RevId: 633179853
2024-05-13 05:50:14 -07:00
Protobuf Team Bot 39986c099b Auto-generate files after cl/632535021 2024-05-10 17:38:46 +00:00
Protobuf Team Bot 24d42eacec Add a BUILD file under rust/accessors and rename helpers.h to default_value.h
PiperOrigin-RevId: 632535021
2024-05-10 10:26:58 -07:00
Sandy Zhang c99cf4bbfa Add `java` to reserved names to escape extensions to `java_`. This may break existing references to gencode for extensions named `java`.
This prevents shadowing of `java.lang` package commonly used in protobuf gencode. Existing extensions named `java` may or may not previously fail to compile depending on if the contents of their .proto result in gencode using `java.lang`. This is needed to fix `java_features.proto` lite gencode since enum gencode uses `java.lang`. Fields named `java` should already be escaped.

*Warning: This may break user code for existing protos with extensions named `java`. References to the extension should be renamed to use `java_` e.g. registry.add(GeneratedClassName.java_)*

PiperOrigin-RevId: 632508249
2024-05-10 08:49:22 -07:00
Hong Shin 82e83ddc95 wire_format.cc: Refactor Message and Group handling into a helper func
PiperOrigin-RevId: 632191495
2024-05-09 10:21:36 -07:00
Éamonn McManus 510c1434ed Add `null` to the list of reserved words.
The previous change claimed to do this in addition to `true` and `false`, but it was not actually in the code.

To reiterate the text from the earlier change, now actually reflected entirely in the code:

> The identifiers `true`, `false`, and `null` are effectively reserved words in Java, although for some reason they are listed separately from the "keywords" in the Java Language Specification.
>
> This doesn't matter for regular fields, because a proto field called `true` will be accessed with `getTrue` and `setTrue`. But for extensions, the generated Java code will have a public static field whose name is the same as the name of the extension field, with `_` appended if the name is a reserved word. Previously there was no `_` for `true` etc, so the generated code would not compile.

This change should not affect any existing client code in Java. If someone had tried to use an extension called `true` (etc), they would have found that the generated proto code did not compile. Now it is possible to reference such an extension as `true_`.

PiperOrigin-RevId: 632174190
2024-05-09 09:30:45 -07:00
Protobuf Team Bot 1b06cefe33 Move casting functions to MessageLite and use ClassData as the uniqueness
instead of Reflection. This allows using these functions instead of
`dynamic_cast` for all generated types including LITE.

PiperOrigin-RevId: 632135009
2024-05-09 07:00:26 -07:00
Protobuf Team Bot b694cfa441 Clarify that the input to proto2::DynamicMessageFactory::GetPrototype should be non-null
PiperOrigin-RevId: 631938646
2024-05-08 15:16:02 -07:00
Protobuf Team Bot ec126a5069 Replace the single user of `PROTOBUF_ALIGNAS` with `alignas` and remove the now
dead macro.
This reduces the compile time cost of port_def.inc

PiperOrigin-RevId: 631886181
2024-05-08 12:27:42 -07:00
Protobuf Team Bot 64580b29c1 Expose JavaPackageDirectory and FileClassName in java/names.h
PiperOrigin-RevId: 631870244
2024-05-08 11:35:56 -07:00
Protobuf Team Bot d5b7d7a87f Remove dead feature macro.
The opt-out code was removed some time ago.

PiperOrigin-RevId: 631826723
2024-05-08 09:30:53 -07:00
Brent Shaffer 01744cccae chore(php): conformance testing for edition (#16712)
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/16712 from bshaffer:php-editions a1c41add7d
PiperOrigin-RevId: 631824623
2024-05-08 09:23:08 -07:00
Protobuf Team Bot d8329b68e4 Add .to_owned(&self)->Msg functions to MsgMut and MsgView
PiperOrigin-RevId: 631686314
2024-05-08 00:54:28 -07:00
Éamonn McManus 98d5bdd111 Add "reserved literals" to the list of reserved names for Java.
The identifiers `true`, `false`, and `null` are effectively reserved words in Java, although for some reason they are listed separately from the "keywords" in the Java Language Specification.

This doesn't matter for regular fields, because a proto field called `true` will be accessed with `getTrue` and `setTrue`. But for extensions, the generated Java code will have a public static field whose name is the same as the name of the extension field, with `_` appended if the name is a reserved word. Previously there was no `_` for `true` etc, so the generated code would not compile.

PiperOrigin-RevId: 631599695
2024-05-07 17:23:09 -07:00
Protobuf Team Bot 5632d8e616 Replace macro with constexpr function to reduce compile time cost of
port_def.inc usage.

PiperOrigin-RevId: 631545753
2024-05-07 14:17:33 -07:00
Protobuf Team Bot 12af9ade1d Add simple conformance test that builds the old gencode against the current runtime.
PiperOrigin-RevId: 631486123
2024-05-07 11:15:24 -07:00
Protobuf Team Bot f70d90b474 Put arena cleanup nodes on a separate chunked list instead of at the ends of arena blocks.
The motivation is (a) better data locality during SerialArena::CleanupList and (b) simplification of arena layout.

PiperOrigin-RevId: 631173641
2024-05-06 13:54:56 -07:00
Protobuf Team Bot e949bba22a Clean up port_def.inc by inlining BSWAP macros that are only used once.
PiperOrigin-RevId: 631130592
2024-05-06 11:35:23 -07:00
Protobuf Team Bot e03c1ce349 In SerialArenaChunk, (a) avoid recomputing offset of ids/arenas on each element access in constructor, (b) add a no-arg Layout() function for convenience, (c) fix an incorrect construction type in `new (&arena(i)) std::atomic<void*>`, where std::atomic<void*> should be std::atomic<SerialArena*>.
PiperOrigin-RevId: 631125248
2024-05-06 11:21:30 -07:00
Jakob Buchgraber b6e0a48b02 Implement `IntoProxied` for repeated field setters
We modify set_<repeated_field> to accept the IntoProxied type as the value and move the value (avoid copying) whenever possible.

For UPB:
 - We fuse the arena of Repeated<T> with the parent message arena.
 - We use upb_Message_SetBaseField to set the upb_Array contained in the Repeated<T>.

For C++:
 - We generate an additional setter thunk that moves the value.
 - The move assignment operator of RepeatedField/RepeatedPtrField is specialized. In order to adhere to the layering check we need to add '#include' statements for all .proto imports to the generated thunks.pb.cc.

PiperOrigin-RevId: 631010333
2024-05-06 05:31:08 -07:00
Protobuf Team Bot a91d76bb8a Internal changes to extension declarations.
PiperOrigin-RevId: 630448683
2024-05-03 11:33:11 -07:00
Protobuf Team Bot 9ae3d81052 Internal change.
PiperOrigin-RevId: 630431165
2024-05-03 10:41:21 -07:00
Adrian Sadłocha 733b9c54e9 Implement `is_known` method on the `Enum` trait
This is equivalent to C++'s `_IsValid` functions. Since proto3 enums are open,
`is_known` seems to be a better name than the misleading `is_valid`.

C++-specific Protocol Buffers documentation already uses "known fields" and
"unknown enum values" expressions.

PiperOrigin-RevId: 630344180
2024-05-03 03:52:51 -07:00
Protobuf Team Bot d9ff109888 internal change
PiperOrigin-RevId: 630273635
2024-05-02 21:48:07 -07:00
Mark Hansen cc79f776dc Proto java full runtime: avoid allocating Integers accessing enum lists
Previously, we would allocate a boxed Integer when accessing repeated enums, because we used ArrayList<Integer>, which must box the int.

Use IntArrayList and the primitive "int getInt()" method instead.

PiperOrigin-RevId: 630229119
2024-05-02 17:50:55 -07:00
Mike Kruskal d6c283321e Fix validation checks of implicit presence.
Instead of checking the resolved features, we should really be checking the has_presence helper.  Repeated fields, oneofs, and extensions can trigger these conditions when they inherit IMPLICIT, even though it's ignored.

Closes #16664

PiperOrigin-RevId: 630206208
2024-05-02 16:06:47 -07:00
Mike Kruskal 0dbd99a41d Clarify map behaviors in editions.
Map fields should remain length-prefixed for now, even if DELIMITED is inherited.  Field presence will remain unchanged, but unit-tests are added to make sure proto2/proto3 behaviors stay consistent.

Closes #16549

PiperOrigin-RevId: 630191163
2024-05-02 15:14:11 -07:00
Protobuf Team Bot 9340eec422 Auto-generate files after cl/630099889 2024-05-02 17:25:22 +00:00
Mike Kruskal 2257232676 Split bootstrapped java_features.proto to keep it from leaking out.
This can cause ODR violations in downstream users who link against both the bootstrapped proto and transitive C++ gencode of java_features.proto.  Once protoc is split up, we can turn the bootstrapped proto into a real cc_proto_library target and avoid this problem altogether.

PiperOrigin-RevId: 630099889
2024-05-02 10:13:02 -07:00
Éamonn McManus 39a1c6a46e In the Kotlin DSL, reference builder methods with property syntax.
Instead of _builder.getFoo() and _builder.setFoo(value), the generated code now uses _builder.foo and _builder.foo = value. When compiling against the Java Proto API, this makes no difference, since the Kotlin compiler treats Java methods getFoo/setFoo as defining a Kotlin property foo. But if a compatible proto API is implemented in Kotlin then there is no such equivalence. Such an implementation would have to define either both forms or just the one that the DSL uses. For a Kotlin API it is more natural to use properties.

Similarly, the generated Java methods getFoosList(), getFoosCount(), getFoosMap(), and getEnumValue() are accessed via property names (foosList, foosCount, foosMap, enumValue).

(This is the second application of this change. The first had to be rolled back because it didn't handle conflict-resolution names correctly.)

PiperOrigin-RevId: 629807104
2024-05-01 12:49:52 -07:00