Skip to content

Add ast_internal::NullType for internal AST type representation. #1638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checker/internal/type_checker_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ absl::StatusOr<AstType> FlattenType(const Type& type) {
case TypeKind::kError:
return AstType(ast_internal::ErrorType());
case TypeKind::kNull:
return AstType(nullptr);
return AstType(ast_internal::NullType());
case TypeKind::kBool:
return AstType(ast_internal::PrimitiveType::kBool);
case TypeKind::kInt:
Expand Down
2 changes: 1 addition & 1 deletion checker/internal/type_checker_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ INSTANTIATE_TEST_SUITE_P(
::testing::Values(
AstTypeConversionTestCase{
.decl_type = NullType(),
.expected_type = AstType(nullptr),
.expected_type = AstType(ast_internal::NullType()),
},
AstTypeConversionTestCase{
.decl_type = DynType(),
Expand Down
4 changes: 4 additions & 0 deletions codelab/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ cc_library(
name = "exercise1",
srcs = ["exercise1.cc"],
hdrs = ["exercise1.h"],
tags = [
"manual",
"nobuilder",
],
deps = [
"//eval/public:activation",
"//eval/public:builtin_func_registrar",
Expand Down
33 changes: 13 additions & 20 deletions common/ast/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#ifndef THIRD_PARTY_CEL_CPP_BASE_AST_INTERNAL_EXPR_H_
#define THIRD_PARTY_CEL_CPP_BASE_AST_INTERNAL_EXPR_H_

#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
Expand All @@ -33,19 +32,6 @@

namespace cel::ast_internal {

// Temporary aliases that will be deleted in future.
using NullValue = std::nullptr_t;
using Bytes = cel::BytesConstant;
using Constant = cel::Constant;
using ConstantKind = cel::ConstantKind;
using Ident = cel::IdentExpr;
using Expr = cel::Expr;
using ExprKind = cel::ExprKind;
using Select = cel::SelectExpr;
using Call = cel::CallExpr;
using CreateList = cel::ListExpr;
using CreateStruct = cel::StructExpr;
using Comprehension = cel::ComprehensionExpr;

// An extension that was requested for the source expression.
class Extension {
Expand Down Expand Up @@ -566,10 +552,17 @@ enum class ErrorType { kErrorTypeValue = 0 };

struct UnspecifiedType : public absl::monostate {};

struct DynamicType : public absl::monostate {};
struct DynamicType {};

inline bool operator==(const DynamicType&, const DynamicType&) { return true; }
inline bool operator!=(const DynamicType&, const DynamicType&) { return false; }

struct NullType {};
inline bool operator==(const NullType&, const NullType&) { return true; }
inline bool operator!=(const NullType&, const NullType&) { return false; }

using TypeKind =
absl::variant<UnspecifiedType, DynamicType, NullValue, PrimitiveType,
absl::variant<UnspecifiedType, DynamicType, NullType, PrimitiveType,
PrimitiveTypeWrapper, WellKnownType, ListType, MapType,
FunctionType, MessageType, ParamType,
absl_nullable std::unique_ptr<Type>, ErrorType, AbstractType>;
Expand Down Expand Up @@ -599,7 +592,7 @@ class Type {
}

bool has_null() const {
return absl::holds_alternative<NullValue>(type_kind_);
return absl::holds_alternative<NullType>(type_kind_);
}

bool has_primitive() const {
Expand Down Expand Up @@ -646,12 +639,12 @@ class Type {
return absl::holds_alternative<AbstractType>(type_kind_);
}

NullValue null() const {
auto* value = absl::get_if<NullValue>(&type_kind_);
NullType null() const {
auto* value = absl::get_if<NullType>(&type_kind_);
if (value != nullptr) {
return *value;
}
return nullptr;
return {};
}

PrimitiveType primitive() const {
Expand Down
2 changes: 1 addition & 1 deletion common/ast/expr_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ TEST(AstTest, FunctionTypeDefaults) {
}

TEST(AstTest, TypeDefaults) {
EXPECT_EQ(Type().null(), nullptr);
EXPECT_EQ(Type().null(), NullType());
EXPECT_EQ(Type().primitive(), PrimitiveType::kPrimitiveTypeUnspecified);
EXPECT_EQ(Type().wrapper(), PrimitiveType::kPrimitiveTypeUnspecified);
EXPECT_EQ(Type().well_known(), WellKnownType::kWellKnownTypeUnspecified);
Expand Down
7 changes: 3 additions & 4 deletions common/ast_proto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "common/ast_proto.h"

#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
Expand Down Expand Up @@ -57,7 +56,7 @@ using ::cel::ast_internal::FunctionType;
using ::cel::ast_internal::ListType;
using ::cel::ast_internal::MapType;
using ::cel::ast_internal::MessageType;
using ::cel::ast_internal::NullValue;
using ::cel::ast_internal::NullType;
using ::cel::ast_internal::ParamType;
using ::cel::ast_internal::PrimitiveType;
using ::cel::ast_internal::PrimitiveTypeWrapper;
Expand Down Expand Up @@ -234,7 +233,7 @@ absl::StatusOr<Type> ConvertProtoTypeToNative(
case cel::expr::Type::kDyn:
return Type(DynamicType());
case cel::expr::Type::kNull:
return Type(nullptr);
return Type(NullType());
case cel::expr::Type::kPrimitive: {
auto native_primitive = ToNative(type.primitive());
if (!native_primitive.ok()) {
Expand Down Expand Up @@ -395,7 +394,7 @@ struct TypeKindToProtoVisitor {
return absl::OkStatus();
}

absl::Status operator()(std::nullptr_t) {
absl::Status operator()(NullType) {
result->set_null(google::protobuf::NULL_VALUE);
return absl::OkStatus();
}
Expand Down
2 changes: 1 addition & 1 deletion common/ast_proto_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ TEST(AstConvertersTest, NullTypeToNative) {
auto native_type = ConvertProtoTypeToNative(type);

ASSERT_TRUE(native_type->has_null());
EXPECT_EQ(native_type->null(), nullptr);
EXPECT_EQ(native_type->null(), ast_internal::NullType());
}

TEST(AstConvertersTest, PrimitiveTypeWrapperToNative) {
Expand Down
7 changes: 2 additions & 5 deletions runtime/internal/convert_constant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

#include "runtime/internal/convert_constant.h"

#include <cstddef>
#include <cstdint>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/time/time.h"
#include "absl/types/variant.h"
#include "common/allocator.h"
#include "common/ast/expr.h"
#include "common/constant.h"
#include "common/value.h"
#include "eval/internal/errors.h"
Expand All @@ -36,10 +36,7 @@ struct ConvertVisitor {
absl::StatusOr<cel::Value> operator()(absl::monostate) {
return absl::InvalidArgumentError("unspecified constant");
}
absl::StatusOr<cel::Value> operator()(
const cel::ast_internal::NullValue& value) {
return NullValue();
}
absl::StatusOr<cel::Value> operator()(std::nullptr_t) { return NullValue(); }
absl::StatusOr<cel::Value> operator()(bool value) { return BoolValue(value); }
absl::StatusOr<cel::Value> operator()(int64_t value) {
return IntValue(value);
Expand Down
2 changes: 1 addition & 1 deletion testutil/baseline_tests_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ INSTANTIATE_TEST_SUITE_P(
TestCase{AstType(ast_internal::WellKnownType::kTimestamp),
"x~google.protobuf.Timestamp"},
TestCase{AstType(ast_internal::DynamicType()), "x~dyn"},
TestCase{AstType(nullptr), "x~null"},
TestCase{AstType(ast_internal::NullType()), "x~null"},
TestCase{AstType(ast_internal::UnspecifiedType()), "x~<error>"},
TestCase{AstType(ast_internal::MessageType("com.example.Type")),
"x~com.example.Type"},
Expand Down