From b01794cb3bd6d1c74815d118a3c70b41fb2fc654 Mon Sep 17 00:00:00 2001 From: Hong Shin Date: Mon, 10 Jul 2023 10:08:43 -0700 Subject: [PATCH] Add test that ensures rust protos build with reserved keywords Also confirmed that this fails to build without the `r#` inside singular_scalar.cc PiperOrigin-RevId: 546906226 --- rust/test/BUILD | 32 +++++++++++++++++++++++++ rust/test/reserved.proto | 37 +++++++++++++++++++++++++++++ rust/test/shared/BUILD | 18 ++++++++++++++ rust/test/shared/reserved_test.rs | 39 +++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 rust/test/reserved.proto create mode 100644 rust/test/shared/reserved_test.rs diff --git a/rust/test/BUILD b/rust/test/BUILD index 9fca9bc2cc..0ec7aad5b9 100644 --- a/rust/test/BUILD +++ b/rust/test/BUILD @@ -175,3 +175,35 @@ rust_upb_proto_library( ], deps = [":no_package_proto"], ) + +proto_library( + name = "reserved_proto", + testonly = True, + srcs = ["reserved.proto"], +) + +cc_proto_library( + name = "reserved_cc_proto", + testonly = True, + deps = [":reserved_proto"], +) + +rust_cc_proto_library( + name = "reserved_cc_rust_proto", + testonly = True, + visibility = [ + "//rust/test/cpp:__subpackages__", + "//rust/test/shared:__subpackages__", + ], + deps = [":reserved_cc_proto"], +) + +rust_upb_proto_library( + name = "reserved_upb_rust_proto", + testonly = True, + visibility = [ + "//rust/test/shared:__subpackages__", + "//rust/test/upb:__subpackages__", + ], + deps = [":reserved_proto"], +) diff --git a/rust/test/reserved.proto b/rust/test/reserved.proto new file mode 100644 index 0000000000..87e94d28d2 --- /dev/null +++ b/rust/test/reserved.proto @@ -0,0 +1,37 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google LLC. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto2"; + +package naming; + +message Reserved { + optional int32 for = 1; // for is a reserved word in rust, let's make sure we can compile it +} diff --git a/rust/test/shared/BUILD b/rust/test/shared/BUILD index 40a03b1c21..140944cf8b 100644 --- a/rust/test/shared/BUILD +++ b/rust/test/shared/BUILD @@ -54,6 +54,24 @@ rust_test( ], ) +rust_test( + name = "reserved_cpp_test", + srcs = ["reserved_test.rs"], + deps = [ + "//rust/test:reserved_cc_rust_proto", + "//rust/test:unittest_cc_rust_proto", + ], +) + +rust_test( + name = "reserved_upb_test", + srcs = ["reserved_test.rs"], + deps = [ + "//rust/test:reserved_upb_rust_proto", + "//rust/test:unittest_upb_rust_proto", + ], +) + rust_test( name = "nested_messages_cpp_test", srcs = ["nested_messages_test.rs"], diff --git a/rust/test/shared/reserved_test.rs b/rust/test/shared/reserved_test.rs new file mode 100644 index 0000000000..0c8ed4eab2 --- /dev/null +++ b/rust/test/shared/reserved_test.rs @@ -0,0 +1,39 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google LLC. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +/// Test covering proto compilation with reserved words. +use reserved_proto::naming::Reserved; + +#[test] +fn test_reserved_keyword_in_accessors() { + let msg = Reserved::new(); + let res = msg.r#for(); + assert_eq!(res, 0); +}