Skip to content

Commit d877a05

Browse files
l46kokcopybara-github
authored andcommitted
Fix type-checker to always rewrite resolved identifiers and functions through container or alias resolutions
PiperOrigin-RevId: 816019607
1 parent 6997259 commit d877a05

26 files changed

+149
-54
lines changed

checker/src/main/java/dev/cel/checker/ExprChecker.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,13 @@ private CelExpr visit(CelExpr expr, CelExpr.CelStruct struct) {
362362
CelType messageType = SimpleType.ERROR;
363363
CelIdentDecl decl =
364364
env.lookupIdent(expr.id(), getPosition(expr), container, struct.messageName());
365+
if (!struct.messageName().equals(decl.name())) {
366+
expr =
367+
expr.toBuilder()
368+
.setStruct(struct.toBuilder().setMessageName(decl.name()).build())
369+
.build();
370+
}
371+
365372
env.setRef(expr, CelReference.newBuilder().setName(decl.name()).build());
366373
CelType type = decl.type();
367374
if (type.kind() != CelKind.ERROR) {

checker/src/test/java/dev/cel/checker/CelProtoExprVisitorTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,17 @@ public void visitSelect() throws Exception {
175175
.isEqualTo(
176176
VisitedReference.newBuilder()
177177
.setCreateStruct(
178-
Expr.CreateStruct.newBuilder().setMessageName("TestAllTypes").build())
178+
Expr.CreateStruct.newBuilder()
179+
.setMessageName("cel.expr.conformance.proto3.TestAllTypes")
180+
.build())
179181
.setSelect(
180182
Expr.Select.newBuilder()
181183
.setOperand(
182184
Expr.newBuilder()
183185
.setId(1)
184186
.setStructExpr(
185187
Expr.CreateStruct.newBuilder()
186-
.setMessageName("TestAllTypes")
188+
.setMessageName("cel.expr.conformance.proto3.TestAllTypes")
187189
.build()))
188190
.setField("single_int64")
189191
.build())
@@ -227,7 +229,9 @@ public void visitCreateStruct() throws Exception {
227229
.isEqualTo(
228230
VisitedReference.newBuilder()
229231
.setCreateStruct(
230-
Expr.CreateStruct.newBuilder().setMessageName("TestAllTypes").build())
232+
Expr.CreateStruct.newBuilder()
233+
.setMessageName("cel.expr.conformance.proto3.TestAllTypes")
234+
.build())
231235
.build());
232236
}
233237

checker/src/test/java/dev/cel/checker/ExprCheckerTest.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ private void runTest() throws Exception {
7373
CelAbstractSyntaxTree ast =
7474
prepareTest(
7575
Arrays.asList(
76-
TestAllTypes.getDescriptor(),
77-
dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor()));
76+
StandaloneGlobalEnum.getDescriptor().getFile(),
77+
TestAllTypes.getDescriptor().getFile(),
78+
dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor().getFile()));
7879
if (ast != null) {
7980
testOutput()
8081
.println(
@@ -239,6 +240,45 @@ public void messageFieldSelect() throws Exception {
239240
runTest();
240241
}
241242

243+
@Test
244+
public void containers() throws Exception {
245+
container =
246+
CelContainer.newBuilder()
247+
.setName("dev.cel.testing.testdata.proto3.StandaloneGlobalEnum")
248+
.addAlias("p3_alias", "cel.expr.conformance.proto3")
249+
.addAlias("foo_bar_alias", "foo.bar")
250+
.addAlias("foo_bar_baz_alias", "foo.bar.baz")
251+
.addAbbreviations("cel.expr.conformance.proto2", "cel.expr.conformance.proto3")
252+
.build();
253+
source = "p3_alias.TestAllTypes{}";
254+
runTest();
255+
256+
source = "proto2.TestAllTypes{}";
257+
runTest();
258+
259+
source = "proto3.TestAllTypes{}";
260+
runTest();
261+
262+
source = "SGAR"; // From StandaloneGlobalEnum
263+
runTest();
264+
265+
declareVariable("foo.bar", SimpleType.STRING);
266+
declareFunction(
267+
"baz",
268+
memberOverload(
269+
"foo_bar_baz_overload", ImmutableList.of(SimpleType.STRING), SimpleType.DYN));
270+
// Member call of "baz()" on "foo.bar" identifier
271+
source = "foo_bar_alias.baz()";
272+
runTest();
273+
274+
declareFunction(
275+
"foo.bar.baz.qux",
276+
globalOverload("foo_bar_baz_qux_overload", ImmutableList.of(), SimpleType.DYN));
277+
// Global call of "foo.bar.baz.qux" as a fully qualified name
278+
source = "foo_bar_baz_alias.qux()";
279+
runTest();
280+
}
281+
242282
@Test
243283
public void messageCreationError() throws Exception {
244284
declareVariable("x", SimpleType.INT);
@@ -851,17 +891,16 @@ public void twoVarComprehensions_incorrectIterVars() throws Exception {
851891
public void twoVarComprehensions_duplicateIterVars() throws Exception {
852892
CelType messageType = StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes");
853893
declareVariable("x", messageType);
854-
source =
855-
"x.repeated_int64.exists(i, i, i < v)";
894+
source = "x.repeated_int64.exists(i, i, i < v)";
856895
runTest();
857896
}
858-
897+
859898
@Test
860899
public void twoVarComprehensions_incorrectNumberOfArgs() throws Exception {
861900
CelType messageType = StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes");
862901
declareVariable("x", messageType);
863902
source =
864-
"[1, 2, 3, 4].exists_one(i, v, i < v, v)"
903+
"[1, 2, 3, 4].exists_one(i, v, i < v, v)"
865904
+ "&& x.map_string_string.transformList(i, i < v) "
866905
+ "&& [1, 2, 3].transformList(i, v, i > 0 && x < 3, (i * v) + v) == [4]";
867906
runTest();
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
Source: TestAllTypes{single_int32: 1, single_int64: 2}
22
=====>
3-
TestAllTypes{
3+
cel.expr.conformance.proto3.TestAllTypes{
44
single_int32:1~int,
55
single_int64:2~int
6-
}~cel.expr.conformance.proto3.TestAllTypes^cel.expr.conformance.proto3.TestAllTypes
7-
6+
}~cel.expr.conformance.proto3.TestAllTypes^cel.expr.conformance.proto3.TestAllTypes
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Source: p3_alias.TestAllTypes{}
2+
=====>
3+
cel.expr.conformance.proto3.TestAllTypes{}~cel.expr.conformance.proto3.TestAllTypes^cel.expr.conformance.proto3.TestAllTypes
4+
5+
Source: proto2.TestAllTypes{}
6+
=====>
7+
cel.expr.conformance.proto2.TestAllTypes{}~cel.expr.conformance.proto2.TestAllTypes^cel.expr.conformance.proto2.TestAllTypes
8+
9+
Source: proto3.TestAllTypes{}
10+
=====>
11+
cel.expr.conformance.proto3.TestAllTypes{}~cel.expr.conformance.proto3.TestAllTypes^cel.expr.conformance.proto3.TestAllTypes
12+
13+
Source: SGAR
14+
=====>
15+
dev.cel.testing.testdata.proto3.StandaloneGlobalEnum.SGAR~int^dev.cel.testing.testdata.proto3.StandaloneGlobalEnum.SGAR
16+
17+
Source: foo_bar_alias.baz()
18+
declare foo.bar {
19+
value string
20+
}
21+
declare baz {
22+
function foo_bar_baz_overload string.() -> dyn
23+
}
24+
=====>
25+
foo.bar~string^foo.bar.baz()~dyn^foo_bar_baz_overload
26+
27+
Source: foo_bar_baz_alias.qux()
28+
declare foo.bar {
29+
value string
30+
}
31+
declare baz {
32+
function foo_bar_baz_overload string.() -> dyn
33+
}
34+
declare foo.bar.baz.qux {
35+
function foo_bar_baz_qux_overload () -> dyn
36+
}
37+
=====>
38+
foo.bar.baz.qux()~dyn^foo_bar_baz_qux_overload

checker/src/test/resources/nestedEnums.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ declare single_nested_enum {
3030
}
3131
=====>
3232
_==_(
33-
TestAllTypes{
33+
cel.expr.conformance.proto3.TestAllTypes{
3434
single_nested_enum:cel.expr.conformance.proto3.TestAllTypes.NestedEnum.BAR~int^cel.expr.conformance.proto3.TestAllTypes.NestedEnum.BAR
3535
}~cel.expr.conformance.proto3.TestAllTypes^cel.expr.conformance.proto3.TestAllTypes.single_nested_enum~int,
3636
1~int
37-
)~bool^equals
37+
)~bool^equals

checker/src/test/resources/nullableMessage.baseline

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ declare x {
1616
_||_(
1717
_==_(
1818
null~null,
19-
TestAllTypes{}~cel.expr.conformance.proto3.TestAllTypes^cel.expr.conformance.proto3.TestAllTypes
19+
cel.expr.conformance.proto3.TestAllTypes{}~cel.expr.conformance.proto3.TestAllTypes^cel.expr.conformance.proto3.TestAllTypes
2020
)~bool^equals,
2121
_==_(
22-
TestAllTypes{}~cel.expr.conformance.proto3.TestAllTypes^cel.expr.conformance.proto3.TestAllTypes,
22+
cel.expr.conformance.proto3.TestAllTypes{}~cel.expr.conformance.proto3.TestAllTypes^cel.expr.conformance.proto3.TestAllTypes,
2323
null~null
2424
)~bool^equals
25-
)~bool^logical_or
25+
)~bool^logical_or

checker/src/test/resources/optionals.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Source: {?'key': {'a': 'b'}.?value}.key
7272

7373
Source: TestAllTypes{?single_int32: {}.?i}
7474
=====>
75-
TestAllTypes{
75+
cel.expr.conformance.proto3.TestAllTypes{
7676
?single_int32:_?._(
7777
{}~map(dyn, int),
7878
"i"
@@ -118,4 +118,4 @@ declare b {
118118
{
119119
?"str"~string:a~optional_type(string)^a,
120120
2~int:3~int
121-
}~map(dyn, dyn)
121+
}~map(dyn, dyn)

common/src/test/java/dev/cel/common/ast/CelExprFormatterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void struct() throws Exception {
191191
assertThat(formattedExpr)
192192
.isEqualTo(
193193
"STRUCT [1] {\n"
194-
+ " name: TestAllTypes\n"
194+
+ " name: cel.expr.conformance.proto3.TestAllTypes\n"
195195
+ " entries: {\n"
196196
+ " ENTRY [2] {\n"
197197
+ " field_key: single_int64\n"

common/src/test/java/dev/cel/common/ast/CelExprVisitorTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,19 @@ public void visitSelect() throws Exception {
206206
assertThat(visited)
207207
.isEqualTo(
208208
VisitedReference.newBuilder()
209-
.setStruct(CelStruct.newBuilder().setMessageName("TestAllTypes").build())
209+
.setStruct(
210+
CelStruct.newBuilder()
211+
.setMessageName("cel.expr.conformance.proto3.TestAllTypes")
212+
.build())
210213
.setSelect(
211214
CelSelect.newBuilder()
212215
.setOperand(
213216
CelExpr.newBuilder()
214217
.setId(1)
215218
.setStruct(
216-
CelStruct.newBuilder().setMessageName("TestAllTypes").build())
219+
CelStruct.newBuilder()
220+
.setMessageName("cel.expr.conformance.proto3.TestAllTypes")
221+
.build())
217222
.build())
218223
.setField("single_int64")
219224
.build())
@@ -268,7 +273,7 @@ public void visitStruct_fieldkey() throws Exception {
268273
.setFieldKey("single_int64")
269274
.setValue(CelExpr.ofConstant(3, longConstant))
270275
.build())
271-
.setMessageName("TestAllTypes")
276+
.setMessageName("cel.expr.conformance.proto3.TestAllTypes")
272277
.build())
273278
.build());
274279
}

0 commit comments

Comments
 (0)