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
18 changes: 18 additions & 0 deletions checker/checker_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ struct CheckerOptions {
// Note: The checked AST will contain the json field names and an extension
// tag, but will require runtime support for resolving the json field names.
bool use_json_field_names = false;

// If true, values bound by cel.bind and cel.@block are treated as
// polymorphic (Hindley-Milner let-polymorphism): type parameters of the
// bound expression that remain unconstrained (e.g. the element type of `[]`)
// are generalized, so each reference to the bound variable is inferred
// independently instead of constraining or widening the others. Type
// parameters shared with enclosing variables are not generalized.
//
// Also reports the type of the root expression with its free type
// parameters preserved as named type parameters (e.g. `list(T1)`) via
// ValidationResult::GetGenericResultType(), so bind-like composition outside
// of the checker (e.g. policy variables) can declare variables that are
// instantiated independently at each reference. Only populated if the AST
// was checked with an explicit arena.
//
// Disabled by default; when disabled, type checking is unchanged. The
// resolved type map and the checked AST type map are unaffected either way.
bool use_polymorphic_bind = false;
};

} // namespace cel
Expand Down
3 changes: 3 additions & 0 deletions checker/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ cc_library(
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:function_ref",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
Expand Down Expand Up @@ -252,7 +253,9 @@ cc_library(
"//common:type_kind",
"@com_google_absl//absl/base:no_destructor",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:node_hash_map",
"@com_google_absl//absl/container:node_hash_set",
"@com_google_absl//absl/functional:function_ref",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
Expand Down
25 changes: 25 additions & 0 deletions checker/internal/type_check_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/container/flat_hash_map.h"
#include "absl/functional/function_ref.h"
#include "absl/log/absl_check.h"
#include "absl/memory/memory.h"
#include "absl/status/status.h"
Expand Down Expand Up @@ -64,6 +65,18 @@ class VariableScope {
return variables_.insert({decl.name(), std::move(decl)}).second;
}

// Updates the type of a variable declared directly in this scope. Returns
// false if absent. The decl is updated in place, so existing pointers to it
// remain valid.
bool UpdateLocalVariableType(absl::string_view name, Type type) {
auto it = variables_.find(name);
if (it == variables_.end()) {
return false;
}
it->second.set_type(std::move(type));
return true;
}

std::unique_ptr<VariableScope> MakeNestedScope() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return absl::WrapUnique(new VariableScope(this));
Expand All @@ -72,6 +85,18 @@ class VariableScope {
const VariableDecl* absl_nullable LookupLocalVariable(
absl::string_view name) const;

// Calls `f` for each variable declared in this scope or its parent scopes.
// Shadowed declarations are included.
void ForEachVisibleVariable(
absl::FunctionRef<void(const VariableDecl&)> f) const {
for (const VariableScope* scope = this; scope != nullptr;
scope = scope->parent_) {
for (const auto& entry : scope->variables_) {
f(entry.second);
}
}
}

private:
explicit VariableScope(
const VariableScope* parent ABSL_ATTRIBUTE_LIFETIME_BOUND)
Expand Down
1 change: 1 addition & 0 deletions checker/internal/type_checker_builder_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class TypeCheckerBuilderImpl : public TypeCheckerBuilder {
ExpressionContainer expression_container) override;

const CheckerOptions& options() const override { return options_; }
CheckerOptions& mutable_options() override { return options_; }

google::protobuf::Arena* absl_nonnull arena() override {
if (type_arena_ == nullptr) {
Expand Down
67 changes: 63 additions & 4 deletions checker/internal/type_checker_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ bool MatchesBlock(const Expr& expr) {
call.args()[0].has_list_expr();
}

// Returns true if the comprehension's loop step is just a reference to the
// accumulator (e.g. the cel.bind expansion). The accumulator is then always
// the value of accu_init.
bool IsBindShapedComprehension(const ComprehensionExpr& comprehension) {
return comprehension.loop_step().has_ident_expr() &&
comprehension.loop_step().ident_expr().name() ==
comprehension.accu_var();
}

// Returns the types of the local variables visible from `scope`, i.e. the
// environment for generalizing a binding declared in a child of `scope`.
//
// Declared (global) variables are not included: their types can only contain
// declared type parameters, which are instantiated with fresh type variables
// at each reference, so they never share inference type variables with a
// binding.
std::vector<Type> VisibleLocalVariableTypes(const VariableScope& scope) {
std::vector<Type> types;
scope.ForEachVisibleVariable(
[&](const VariableDecl& decl) { types.push_back(decl.type()); });
return types;
}

using AstType = cel::TypeSpec;
using Severity = TypeCheckIssue::Severity;

Expand Down Expand Up @@ -206,14 +229,15 @@ class ResolveVisitor : public AstVisitorBase {
const TypeCheckEnv& env, const Ast& ast,
TypeInferenceContext& inference_context,
std::vector<TypeCheckIssue>& issues,
google::protobuf::Arena* absl_nonnull arena)
google::protobuf::Arena* absl_nonnull arena, bool use_polymorphic_bind)
: namespace_generator_(std::move(namespace_generator)),
env_(&env),
inference_context_(&inference_context),
issues_(&issues),
ast_(&ast),
root_scope_(),
arena_(arena),
use_polymorphic_bind_(use_polymorphic_bind),
current_scope_(&root_scope_) {}

void PreVisitExpr(const Expr& expr) override {
Expand Down Expand Up @@ -454,6 +478,8 @@ class ResolveVisitor : public AstVisitorBase {
const Ast* absl_nonnull ast_;
VariableScope root_scope_;
google::protobuf::Arena* absl_nonnull arena_;
// See CheckerOptions::use_polymorphic_bind.
bool use_polymorphic_bind_;

// state tracking for the traversal.
const VariableScope* current_scope_;
Expand Down Expand Up @@ -907,6 +933,21 @@ void ResolveVisitor::PostVisitComprehensionSubexpression(
if (!inference_context_->IsAssignable(step_type, accu_type)) {
ReportTypeMismatch(comprehension.loop_step().id(), accu_type,
step_type);
break;
}
// With polymorphic bind, for bind-shaped comprehensions (the loop step
// is just the accumulator, as in the cel.bind expansion), the
// accumulator is a lazily evaluated alias for accu_init. Generalize its
// type so that each reference in the result is inferred independently,
// like a cel.@block index
// (e.g. `cel.bind(x, [], x + [1] == [1] && x + ['a'] == ['a'])`).
// Type params shared with variables in the enclosing scope are not
// generalized.
if (use_polymorphic_bind_ && IsBindShapedComprehension(comprehension)) {
scope.accu_scope->UpdateLocalVariableType(
comprehension.accu_var(),
inference_context_->Generalize(
accu_type, VisibleLocalVariableTypes(*scope.parent)));
}
break;
}
Expand Down Expand Up @@ -1328,8 +1369,14 @@ void ResolveVisitor::HandleBlockIndex(const Expr* expr) {

// Block is typically manually assembled from logically separate
// expressions so fix the type instead of inferring any remaining free type
// params as for normal subexpressions.
auto type = inference_context_->FinalizeType(GetDeducedType(expr));
// params as for normal subexpressions. With polymorphic bind, generalize any
// remaining free type params instead: each reference to the index is
// inferred independently instead of constraining (or widening) the others.
Type type = use_polymorphic_bind_
? inference_context_->Generalize(
GetDeducedType(expr),
VisibleLocalVariableTypes(*current_scope_))
: inference_context_->FinalizeType(GetDeducedType(expr));

VariableDecl decl = MakeVariableDecl(var_name, std::move(type));

Expand Down Expand Up @@ -1451,7 +1498,8 @@ absl::StatusOr<ValidationResult> TypeCheckerImpl::CheckImpl(
TypeInferenceContext type_inference_context(
arena, options_.enable_legacy_null_assignment);
ResolveVisitor visitor(std::move(generator), env_, *ast,
type_inference_context, issues, arena);
type_inference_context, issues, arena,
options_.use_polymorphic_bind);

TraversalOptions opts;
opts.use_comprehension_callbacks = true;
Expand Down Expand Up @@ -1496,6 +1544,14 @@ absl::StatusOr<ValidationResult> TypeCheckerImpl::CheckImpl(
// Apply updates as needed.
// Happens in a second pass to simplify validating that pointers haven't
// been invalidated by other updates.
std::optional<Type> generic_result_type;
if (options_.use_polymorphic_bind && !type_arena.has_value()) {
if (auto it = visitor.types().find(&ast->root_expr());
it != visitor.types().end()) {
generic_result_type =
type_inference_context.GeneralizeWithCanonicalNames(it->second);
}
}
ValidationResult::TypeMap resolved_types;
ResolveRewriter rewriter(visitor, type_inference_context, options_,
ast->mutable_reference_map(),
Expand All @@ -1518,6 +1574,9 @@ absl::StatusOr<ValidationResult> TypeCheckerImpl::CheckImpl(
// arena is destructed. Only set the resolved type map if we're using the
// caller's arena.
result.SetResolvedTypeMap(std::move(resolved_types));
if (generic_result_type.has_value()) {
result.SetGenericResultType(*std::move(generic_result_type));
}
}

return result;
Expand Down
91 changes: 90 additions & 1 deletion checker/internal/type_checker_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,16 @@ class TestExprBuilder {
// `__result__`.
std::unique_ptr<Ast> ComprehensionAst(Expr accu_init, Expr loop_condition,
Expr loop_step) {
Expr result = Ident("__result__");
return ComprehensionAst(std::move(accu_init), std::move(loop_condition),
std::move(loop_step), std::move(result));
}

// Builds a comprehension over the range `[1, 2]` (iteration variable `i`,
// accumulator `__result__`) with the given accu_init, loop_condition,
// loop_step and result.
std::unique_ptr<Ast> ComprehensionAst(Expr accu_init, Expr loop_condition,
Expr loop_step, Expr result) {
Expr expr = NewExpr();
ComprehensionExpr& comprehension = expr.mutable_comprehension_expr();
comprehension.set_iter_var("i");
Expand All @@ -895,7 +905,7 @@ class TestExprBuilder {
comprehension.set_accu_init(std::move(accu_init));
comprehension.set_loop_condition(std::move(loop_condition));
comprehension.set_loop_step(std::move(loop_step));
comprehension.set_result(Ident("__result__"));
comprehension.set_result(std::move(result));
return std::make_unique<Ast>(std::move(expr), SourceInfo());
}

Expand Down Expand Up @@ -1055,6 +1065,85 @@ TEST(TypeCheckerImplTest, ComprehensionRecursiveAccumulatorIsError) {
"found 'list(list(dyn))'")));
}

// For a bind-shaped comprehension (loop step is the accumulator), references
// to the accumulator in the result are inferred independently.
TEST(TypeCheckerImplTest, BindShapedComprehensionAccumulatorIsGeneralized) {
TypeCheckEnv env(GetSharedTestingDescriptorPool());
google::protobuf::Arena arena;
ASSERT_THAT(RegisterMinimalBuiltins(&arena, env), IsOk());
CheckerOptions options;
options.use_polymorphic_bind = true;
TypeCheckerImpl impl(std::move(env), options);

// [__result__ + [1], __result__ + ['a']]
TestExprBuilder b;
Expr result_expr =
b.List(b.Call("_+_", b.Ident("__result__"), b.List(b.IntConst(1))),
b.Call("_+_", b.Ident("__result__"), b.List(b.StringConst("a"))));
std::unique_ptr<Ast> ast = b.ComprehensionAst(
/*accu_init=*/b.List(), /*loop_condition=*/b.BoolConst(false),
/*loop_step=*/b.Ident("__result__"), std::move(result_expr));
ASSERT_OK_AND_ASSIGN(ValidationResult result,
impl.Check(std::move(ast), &arena));
ASSERT_TRUE(result.IsValid()) << result.FormatError();
const Expr& root = result.GetAst()->root_expr();
EXPECT_EQ(FormatTypeName(result.GetResolvedTypeMap().at(root.id())),
"list(dyn)");
// The accumulator init is not constrained by the references.
EXPECT_EQ(FormatTypeName(result.GetResolvedTypeMap().at(
root.comprehension_expr().accu_init().id())),
"list(dyn)");
}

// Without CheckerOptions::use_polymorphic_bind, references to the accumulator
// of a bind-shaped comprehension share its type params.
TEST(TypeCheckerImplTest,
BindShapedComprehensionAccumulatorNotGeneralizedByDefault) {
TypeCheckEnv env(GetSharedTestingDescriptorPool());
google::protobuf::Arena arena;
ASSERT_THAT(RegisterMinimalBuiltins(&arena, env), IsOk());
TypeCheckerImpl impl(std::move(env));

// [__result__ + [1], __result__ + ['a']]
TestExprBuilder b;
Expr result_expr =
b.List(b.Call("_+_", b.Ident("__result__"), b.List(b.IntConst(1))),
b.Call("_+_", b.Ident("__result__"), b.List(b.StringConst("a"))));
std::unique_ptr<Ast> ast = b.ComprehensionAst(
/*accu_init=*/b.List(), /*loop_condition=*/b.BoolConst(false),
/*loop_step=*/b.Ident("__result__"), std::move(result_expr));
ASSERT_OK_AND_ASSIGN(ValidationResult result, impl.Check(std::move(ast)));
EXPECT_FALSE(result.IsValid());
EXPECT_THAT(result.GetIssues(),
Contains(IsIssueWithSubstring(Severity::kError,
"no matching overload for '_+_'")));
}

// Other comprehensions keep a single (monomorphic) accumulator type, even if
// the loop step leaves it unconstrained.
TEST(TypeCheckerImplTest, NonBindComprehensionAccumulatorIsNotGeneralized) {
TypeCheckEnv env(GetSharedTestingDescriptorPool());
google::protobuf::Arena arena;
ASSERT_THAT(RegisterMinimalBuiltins(&arena, env), IsOk());
TypeCheckerImpl impl(std::move(env));

// loop step: __result__ + []
// result: [__result__ + [1], __result__ + ['a']]
TestExprBuilder b;
Expr result_expr =
b.List(b.Call("_+_", b.Ident("__result__"), b.List(b.IntConst(1))),
b.Call("_+_", b.Ident("__result__"), b.List(b.StringConst("a"))));
std::unique_ptr<Ast> ast = b.ComprehensionAst(
/*accu_init=*/b.List(), /*loop_condition=*/b.BoolConst(true),
/*loop_step=*/b.Call("_+_", b.Ident("__result__"), b.List()),
std::move(result_expr));
ASSERT_OK_AND_ASSIGN(ValidationResult result, impl.Check(std::move(ast)));
EXPECT_FALSE(result.IsValid());
EXPECT_THAT(result.GetIssues(),
Contains(IsIssueWithSubstring(Severity::kError,
"no matching overload for '_+_'")));
}

TEST(TypeCheckerImplTest, MapTypeWithMixedKeys) {
TypeCheckEnv env(GetSharedTestingDescriptorPool());

Expand Down
Loading
Loading