Skip to content

Commit 80f0b11

Browse files
jckingcopybara-github
authored andcommitted
Update RuntimeBuilder to accept std::shared_ptr<const proto2::DescriptorPool> to match type checker
PiperOrigin-RevId: 690674918
1 parent f10fd17 commit 80f0b11

15 files changed

Lines changed: 163 additions & 20 deletions

checker/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ cc_library(
9090
"//checker/internal:type_checker_impl",
9191
"//common:decl",
9292
"//common:type",
93+
"//internal:noop_delete",
9394
"//internal:status_macros",
9495
"//internal:well_known_types",
9596
"//parser:macro",

checker/type_checker_builder.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "common/decl.h"
3535
#include "common/type.h"
3636
#include "common/type_introspector.h"
37+
#include "internal/noop_delete.h"
3738
#include "internal/status_macros.h"
3839
#include "internal/well_known_types.h"
3940
#include "parser/macro.h"
@@ -89,7 +90,7 @@ absl::StatusOr<TypeCheckerBuilder> CreateTypeCheckerBuilder(
8990
return CreateTypeCheckerBuilder(
9091
std::shared_ptr<const google::protobuf::DescriptorPool>(
9192
descriptor_pool,
92-
[](absl::Nullable<const google::protobuf::DescriptorPool*>) {}),
93+
internal::NoopDeleteFor<const google::protobuf::DescriptorPool>()),
9394
options);
9495
}
9596

internal/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ cc_library(
580580
hdrs = ["testing_descriptor_pool.h"],
581581
textual_hdrs = [":testing_descriptor_set_embed"],
582582
deps = [
583+
":noop_delete",
583584
"@com_google_absl//absl/base:core_headers",
584585
"@com_google_absl//absl/base:no_destructor",
585586
"@com_google_absl//absl/base:nullability",
@@ -829,3 +830,9 @@ cc_library(
829830
hdrs = ["protobuf_runtime_version.h"],
830831
deps = ["@com_google_protobuf//:protobuf"],
831832
)
833+
834+
cc_library(
835+
name = "noop_delete",
836+
hdrs = ["noop_delete.h"],
837+
deps = ["@com_google_absl//absl/base:nullability"],
838+
)

internal/noop_delete.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef THIRD_PARTY_CEL_CPP_INTERNAL_NOOP_DELETE_H_
16+
#define THIRD_PARTY_CEL_CPP_INTERNAL_NOOP_DELETE_H_
17+
18+
#include <type_traits>
19+
20+
#include "absl/base/nullability.h"
21+
22+
namespace cel::internal {
23+
24+
// Like `std::default_delete`, except it does nothing.
25+
template <typename T>
26+
struct NoopDelete {
27+
static_assert(!std::is_function<T>::value,
28+
"NoopDelete cannot be instantiated for function types");
29+
30+
constexpr NoopDelete() noexcept = default;
31+
constexpr NoopDelete(const NoopDelete<T>&) noexcept = default;
32+
33+
template <
34+
typename U,
35+
typename = std::enable_if_t<std::conjunction_v<
36+
std::negation<std::is_same<T, U>>, std::is_convertible<U*, T*>>>>
37+
// NOLINTNEXTLINE(google-explicit-constructor)
38+
constexpr NoopDelete(const NoopDelete<U>&) noexcept {}
39+
40+
constexpr void operator()(absl::Nullable<T*>) const noexcept {
41+
static_assert(sizeof(T) >= 0, "cannot delete an incomplete type");
42+
static_assert(!std::is_void<T>::value, "cannot delete an incomplete type");
43+
}
44+
};
45+
46+
template <typename T>
47+
inline constexpr NoopDelete<T> NoopDeleteFor() noexcept {
48+
return NoopDelete<T>{};
49+
}
50+
51+
} // namespace cel::internal
52+
53+
#endif // THIRD_PARTY_CEL_CPP_INTERNAL_NOOP_DELETE_H_

internal/testing_descriptor_pool.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "absl/base/no_destructor.h"
2424
#include "absl/base/nullability.h"
2525
#include "absl/log/absl_check.h"
26+
#include "internal/noop_delete.h"
2627
#include "google/protobuf/descriptor.h"
2728

2829
namespace cel::internal {
@@ -54,7 +55,7 @@ GetSharedTestingDescriptorPool() {
5455
static const absl::NoDestructor<
5556
absl::Nonnull<std::shared_ptr<const google::protobuf::DescriptorPool>>>
5657
instance(GetTestingDescriptorPool(),
57-
[](absl::Nullable<const google::protobuf::DescriptorPool*>) {});
58+
internal::NoopDeleteFor<const google::protobuf::DescriptorPool>());
5859
return *instance;
5960
}
6061

internal/well_known_types.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,18 @@ absl::Status Reflection::Initialize(absl::Nonnull<const DescriptorPool*> pool) {
17621762
return absl::OkStatus();
17631763
}
17641764

1765+
bool Reflection::IsInitialized() const {
1766+
// Check that everything is initialized except field mask, which is optional.
1767+
return NullValue().IsInitialized() && BoolValue().IsInitialized() &&
1768+
Int32Value().IsInitialized() && Int64Value().IsInitialized() &&
1769+
UInt32Value().IsInitialized() && UInt64Value().IsInitialized() &&
1770+
FloatValue().IsInitialized() && DoubleValue().IsInitialized() &&
1771+
BytesValue().IsInitialized() && StringValue().IsInitialized() &&
1772+
Any().IsInitialized() && Duration().IsInitialized() &&
1773+
Timestamp().IsInitialized() && Value().IsInitialized() &&
1774+
ListValue().IsInitialized() && Struct().IsInitialized();
1775+
}
1776+
17651777
namespace {
17661778

17671779
// AdaptListValue verifies the message is the well known type

internal/well_known_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,8 @@ class Reflection final {
13911391

13921392
absl::Status Initialize(absl::Nonnull<const google::protobuf::DescriptorPool*> pool);
13931393

1394+
bool IsInitialized() const;
1395+
13941396
// At the moment we only use this class for verifying well known types in
13951397
// descriptor pools. We could eagerly initialize it and cache it somewhere to
13961398
// make things faster.

runtime/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,12 @@ cc_library(
213213
deps = [
214214
":runtime_builder",
215215
":runtime_options",
216+
"//internal:noop_delete",
216217
"//internal:status_macros",
217218
"//runtime/internal:runtime_impl",
218219
"@com_google_absl//absl/base:core_headers",
219220
"@com_google_absl//absl/base:nullability",
221+
"@com_google_absl//absl/log:absl_check",
220222
"@com_google_absl//absl/status:statusor",
221223
"@com_google_protobuf//:protobuf",
222224
],
@@ -231,9 +233,11 @@ cc_library(
231233
":runtime_builder_factory",
232234
":runtime_options",
233235
":standard_functions",
236+
"//internal:noop_delete",
234237
"//internal:status_macros",
235238
"@com_google_absl//absl/base:core_headers",
236239
"@com_google_absl//absl/base:nullability",
240+
"@com_google_absl//absl/log:absl_check",
237241
"@com_google_absl//absl/status:statusor",
238242
"@com_google_protobuf//:protobuf",
239243
],

runtime/internal/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ cc_library(
6969
"//runtime:function_registry",
7070
"//runtime:runtime_options",
7171
"//runtime:type_registry",
72+
"@com_google_absl//absl/base:core_headers",
7273
"@com_google_absl//absl/base:nullability",
74+
"@com_google_absl//absl/log:absl_check",
7375
"@com_google_absl//absl/status:statusor",
76+
"@com_google_protobuf//:protobuf",
7477
],
7578
)
7679

runtime/internal/runtime_impl.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
#define THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_RUNTIME_IMPL_H_
1717

1818
#include <memory>
19+
#include <utility>
1920

21+
#include "absl/base/attributes.h"
22+
#include "absl/base/nullability.h"
23+
#include "absl/log/absl_check.h"
2024
#include "absl/status/statusor.h"
2125
#include "base/ast.h"
2226
#include "base/type_provider.h"
@@ -27,21 +31,28 @@
2731
#include "runtime/runtime.h"
2832
#include "runtime/runtime_options.h"
2933
#include "runtime/type_registry.h"
34+
#include "google/protobuf/descriptor.h"
3035

3136
namespace cel::runtime_internal {
3237

3338
class RuntimeImpl : public Runtime {
3439
public:
3540
struct Environment {
41+
ABSL_ATTRIBUTE_UNUSED
42+
absl::Nonnull<std::shared_ptr<const google::protobuf::DescriptorPool>>
43+
descriptor_pool;
3644
TypeRegistry type_registry;
3745
FunctionRegistry function_registry;
3846
well_known_types::Reflection well_known_types;
3947
};
4048

41-
explicit RuntimeImpl(const RuntimeOptions& options)
42-
: environment_(std::make_shared<Environment>()),
49+
RuntimeImpl(absl::Nonnull<std::shared_ptr<Environment>> environment,
50+
const RuntimeOptions& options)
51+
: environment_(std::move(environment)),
4352
expr_builder_(environment_->function_registry,
44-
environment_->type_registry, options) {}
53+
environment_->type_registry, options) {
54+
ABSL_DCHECK(environment_->well_known_types.IsInitialized());
55+
}
4556

4657
TypeRegistry& type_registry() { return environment_->type_registry; }
4758
const TypeRegistry& type_registry() const {
@@ -55,9 +66,6 @@ class RuntimeImpl : public Runtime {
5566
return environment_->function_registry;
5667
}
5768

58-
well_known_types::Reflection& well_known_types() {
59-
return environment_->well_known_types;
60-
}
6169
const well_known_types::Reflection& well_known_types() const {
6270
return environment_->well_known_types;
6371
}

0 commit comments

Comments
 (0)