Skip to content

Conversation

@abenea
Copy link

@abenea abenea commented Jan 6, 2026

The old implementation outputs the wrong source information for policy files derived from the dummy AST created by RuleComposer.Compose. This change fixes that by preserving the correct source and merging the offset ranges of the rule match expressions inserted by the composer optimizer into the final AST.

@seirl
Copy link
Collaborator

seirl commented Jan 7, 2026

/gcbrun

cel/optimizer.go Outdated
func updateOffsetRanges(idGen ast.IDGenerator, info *ast.SourceInfo) {
newRanges := make(map[int64]ast.OffsetRange)
sortedOldIDs := []int64{}
for oldID := range info.OffsetRanges() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer copy(dst, src) to the for-loop

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://pkg.go.dev/builtin#copy it only works with slices, but in this case I am trying to copy the keys from a map.

keepIDs = make(map[int64]bool)
} else {
// Remove offset ranges for ids without a corresponding AST node. This can happen because the
// checker deletes some nodes while rewriting the AST. For example the Select operand is deleted
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we should be able to do this cleanup within the parser and the checker rather than here. Could we explore that approach instead? Maybe as a separate PR so we can remove the cleanup here after that one is submitted?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #1258.

The old implementation outputs the wrong source information for policy files derived from the dummy AST created by RuleComposer.Compose. This change fixes that by preserving the correct source and merging the offset ranges of the rule match expressions inserted by the composer optimizer into the final AST.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants