Skip to content

Commit

Permalink
Fix nil-type when two-var comprehension has a dyn range (#1077)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristonianJones authored Nov 22, 2024
1 parent ff1302f commit 933f926
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,15 @@ func (c *checker) checkComprehension(e ast.Expr) {
c.isAssignable(types.DynType, rangeType)
// Set the range iteration variable to type DYN as well.
varType = types.DynType
if comp.HasIterVar2() {
var2Type = types.DynType
}
default:
c.errors.notAComprehensionRange(comp.IterRange().ID(), c.location(comp.IterRange()), rangeType)
varType = types.ErrorType
if comp.HasIterVar2() {
var2Type = types.ErrorType
}
}

// Create a block scope for the loop.
Expand Down
7 changes: 7 additions & 0 deletions ext/comprehensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ func TestTwoVarComprehensions(t *testing.T) {
{'Hello': 'world'}.transformList(k, v, "%s=%s".format([k.lowerAscii(), v])) == ["hello=world"]
`},
{expr: `
dyn({'Hello': 'world'}).transformList(k, v, "%s=%s".format([k.lowerAscii(), v])) == ["hello=world"]
`},
{expr: `
{'hello': 'world'}.transformList(k, v, k.startsWith('greeting'), "%s=%s".format([k, v])) == []
`},
{expr: `
Expand All @@ -155,6 +158,10 @@ func TestTwoVarComprehensions(t *testing.T) {
== {'hello': 'hello, world!', 'goodbye': 'goodbye, cruel world!'}
`},
{expr: `
dyn({'hello': 'world', 'goodbye': 'cruel world'}).transformMap(k, v, "%s, %s!".format([k, v]))
== {'hello': 'hello, world!', 'goodbye': 'goodbye, cruel world!'}
`},
{expr: `
{'hello': 'world', 'goodbye': 'cruel world'}.transformMap(k, v, v.startsWith('world'), "%s, %s!".format([k, v]))
== {'hello': 'hello, world!'}
`},
Expand Down
4 changes: 4 additions & 0 deletions policy/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ func (r *runner) setup(t testing.TB) {
if err != nil {
t.Fatalf("cel.AstToString() failed: %v", err)
}
_, err = cel.AstToCheckedExpr(ast)
if err != nil {
t.Fatalf("cel.AstToCheckedExpr() failed: %v", err)
}
if r.expr != "" && normalize(pExpr) != normalize(r.expr) {
t.Errorf("cel.AstToString() got %s, wanted %s", pExpr, r.expr)
}
Expand Down

0 comments on commit 933f926

Please sign in to comment.