Skip to content

Commit 974681c

Browse files
jnthntatumcopybara-github
authored andcommitted
Update assignability check to allow wrapper types to match their corresponding primitives.
PiperOrigin-RevId: 689153309
1 parent 9157115 commit 974681c

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

checker/internal/type_checker_impl_test.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,11 +1306,14 @@ struct CheckedExprTestCase {
13061306
class WktCreationTest : public testing::TestWithParam<CheckedExprTestCase> {};
13071307

13081308
TEST_P(WktCreationTest, MessageCreation) {
1309+
google::protobuf::Arena arena;
13091310
const CheckedExprTestCase& test_case = GetParam();
13101311
TypeCheckEnv env;
13111312
env.AddTypeProvider(std::make_unique<TypeIntrospector>());
13121313
env.set_container("google.protobuf");
13131314

1315+
ASSERT_THAT(RegisterMinimalBuiltins(&arena, env), IsOk());
1316+
13141317
TypeCheckerImpl impl(std::move(env));
13151318
ASSERT_OK_AND_ASSIGN(auto ast, MakeTestParsedAst(test_case.expr));
13161319
ASSERT_OK_AND_ASSIGN(ValidationResult result, impl.Check(std::move(ast)));
@@ -1450,6 +1453,15 @@ INSTANTIATE_TEST_SUITE_P(
14501453
value: b''
14511454
})cel",
14521455
.expected_result_type = AstType(ast_internal::WellKnownType::kAny),
1456+
},
1457+
CheckedExprTestCase{
1458+
.expr = "Int64Value{value: 10} + 1",
1459+
.expected_result_type =
1460+
AstType(ast_internal::PrimitiveType::kInt64),
1461+
},
1462+
CheckedExprTestCase{
1463+
.expr = "BoolValue{value: false} || true",
1464+
.expected_result_type = AstType(ast_internal::PrimitiveType::kBool),
14531465
}));
14541466

14551467
class GenericMessagesTest : public testing::TestWithParam<CheckedExprTestCase> {

checker/internal/type_inference_context.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,15 @@ bool TypeInferenceContext::IsAssignableInternal(
287287
return true;
288288
}
289289

290+
// Wrapper types are assignable to their corresponding primitive type (
291+
// somewhat similar to auto unboxing). This is a bit odd with CEL's null_type,
292+
// but there isn't a dedicated syntax for narrowing from the nullable.
293+
if (auto from_wrapper = WrapperToPrimitive(from_subs);
294+
from_wrapper.has_value()) {
295+
return IsAssignableInternal(*from_wrapper, to_subs,
296+
prospective_substitutions);
297+
}
298+
290299
if (enable_legacy_null_assignment_) {
291300
if (from_subs.IsNull() && IsLegacyNullable(to_subs)) {
292301
return true;

0 commit comments

Comments
 (0)