Skip to content
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
35 changes: 35 additions & 0 deletions validator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,41 @@ cc_library(
],
)

cc_library(
name = "homogeneous_literal_validator",
srcs = ["homogeneous_literal_validator.cc"],
hdrs = ["homogeneous_literal_validator.h"],
deps = [
":validator",
"//common:ast",
"//common:expr",
"//common:navigable_ast",
"@com_google_absl//absl/strings",
],
)

cc_test(
name = "homogeneous_literal_validator_test",
srcs = ["homogeneous_literal_validator_test.cc"],
deps = [
":homogeneous_literal_validator",
":validator",
"//checker:validation_result",
"//common:decl",
"//common:type",
"//compiler",
"//compiler:compiler_factory",
"//compiler:optional",
"//compiler:standard_library",
"//extensions:strings",
"//internal:status_macros",
"//internal:testing",
"//internal:testing_descriptor_pool",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:string_view",
],
)

cc_test(
name = "ast_depth_validator_test",
srcs = ["ast_depth_validator_test.cc"],
Expand Down
190 changes: 190 additions & 0 deletions validator/homogeneous_literal_validator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "validator/homogeneous_literal_validator.h"

#include <cstdint>
#include <string>
#include <utility>
#include <vector>

#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "common/ast.h"
#include "common/expr.h"
#include "common/navigable_ast.h"
#include "validator/validator.h"

namespace cel {

namespace {

bool InExemptFunction(const NavigableAstNode& node,
const std::vector<std::string>& exempt_functions) {
const NavigableAstNode* parent = node.parent();
while (parent != nullptr) {
if (parent->node_kind() == NodeKind::kCall) {
absl::string_view fn_name = parent->expr()->call_expr().function();
for (const auto& exempt : exempt_functions) {
if (exempt == fn_name) {
return true;
}
}
}
parent = parent->parent();
}
return false;
}

bool IsOptional(const TypeSpec& t) {
return t.has_abstract_type() && t.abstract_type().name() == "optional_type";
}

const TypeSpec& GetOptionalParameter(const TypeSpec& t) {
return t.abstract_type().parameter_types()[0];
}

void TypeMismatch(ValidationContext& context, int64_t id,
const TypeSpec& expected, const TypeSpec& actual) {
context.ReportErrorAt(
id, absl::StrCat("expected type '", FormatTypeSpec(expected),
"' but found '", FormatTypeSpec(actual), "'"));
}

bool TypeEquiv(const TypeSpec& a, const TypeSpec& b) {
if (a == b) {
return true;
}

if (a.has_error() || b.has_error()) {
// Don't report mismatch if there's an error (type checking failed for the
// expression).
return true;
}

if (a.has_wrapper() && b.has_primitive()) {
return a.wrapper() == b.primitive();
} else if (a.has_primitive() && b.has_wrapper()) {
return a.primitive() == b.wrapper();
}

if (a.has_list_type() && b.has_list_type()) {
return TypeEquiv(a.list_type().elem_type(), b.list_type().elem_type());
}

if (a.has_map_type() && b.has_map_type()) {
return TypeEquiv(a.map_type().key_type(), b.map_type().key_type()) &&
TypeEquiv(a.map_type().value_type(), b.map_type().value_type());
}

if (a.has_abstract_type() && b.has_abstract_type() &&
a.abstract_type().name() == b.abstract_type().name() &&
a.abstract_type().parameter_types().size() ==
b.abstract_type().parameter_types().size()) {
for (int i = 0; i < a.abstract_type().parameter_types().size(); ++i) {
if (!TypeEquiv(a.abstract_type().parameter_types()[i],
b.abstract_type().parameter_types()[i])) {
return false;
}
}
return true;
}

return false;
}

} // namespace

Validation HomogeneousLiteralValidator(
std::vector<std::string> exempt_functions) {
return Validation([exempt_functions = std::move(exempt_functions)](
ValidationContext& context) -> bool {
bool valid = true;
for (const auto& node :
context.navigable_ast().Root().DescendantsPostorder()) {
if (node.node_kind() == NodeKind::kList) {
if (InExemptFunction(node, exempt_functions)) {
continue;
}
const auto& list_expr = node.expr()->list_expr();
const auto& elements = list_expr.elements();
const TypeSpec* expected_type = nullptr;

for (const auto& element : elements) {
int64_t id = element.expr().id();
const TypeSpec& actual_type = context.ast().GetTypeOrDyn(id);
const TypeSpec* type_to_check = &actual_type;

if (element.optional() && IsOptional(actual_type)) {
type_to_check = &GetOptionalParameter(actual_type);
}

if (expected_type == nullptr) {
expected_type = type_to_check;
continue;
}

if (!(TypeEquiv(*expected_type, *type_to_check))) {
TypeMismatch(context, id, *expected_type, *type_to_check);
valid = false;
break;
}
}
} else if (node.node_kind() == NodeKind::kMap) {
if (InExemptFunction(node, exempt_functions)) {
continue;
}
const auto& map_expr = node.expr()->map_expr();
const auto& entries = map_expr.entries();
const TypeSpec* expected_key_type = nullptr;
const TypeSpec* expected_value_type = nullptr;

for (const auto& entry : entries) {
int64_t key_id = entry.key().id();
int64_t val_id = entry.value().id();
const TypeSpec& actual_key_type = context.ast().GetTypeOrDyn(key_id);
const TypeSpec& actual_val_type = context.ast().GetTypeOrDyn(val_id);
const TypeSpec* key_type_to_check = &actual_key_type;
const TypeSpec* val_type_to_check = &actual_val_type;

if (entry.optional() && IsOptional(actual_val_type)) {
val_type_to_check = &GetOptionalParameter(actual_val_type);
}

if (expected_key_type == nullptr) {
expected_key_type = key_type_to_check;
expected_value_type = val_type_to_check;
continue;
}

if (!(TypeEquiv(*expected_key_type, *key_type_to_check))) {
TypeMismatch(context, key_id, *expected_key_type,
*key_type_to_check);
valid = false;
break;
}
if (!(TypeEquiv(*expected_value_type, *val_type_to_check))) {
TypeMismatch(context, val_id, *expected_value_type,
*val_type_to_check);
valid = false;
break;
}
}
}
}
return valid;
});
}

} // namespace cel
38 changes: 38 additions & 0 deletions validator/homogeneous_literal_validator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_CEL_CPP_VALIDATOR_HOMOGENEOUS_LITERAL_VALIDATOR_H_
#define THIRD_PARTY_CEL_CPP_VALIDATOR_HOMOGENEOUS_LITERAL_VALIDATOR_H_

#include <string>
#include <vector>

#include "validator/validator.h"

namespace cel {

// Returns a `Validation` that checks that all literals in map or list literals
// are the same type. If the list or map is part of an argument to an exempted
// function, it is not checked.
Validation HomogeneousLiteralValidator(
std::vector<std::string> exempt_functions);

inline Validation HomogeneousLiteralValidator() {
// Default to exempting the strings extension "format" function.
return HomogeneousLiteralValidator({"format"});
}

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_VALIDATOR_HOMOGENEOUS_LITERAL_VALIDATOR_H_
Loading