opa icon indicating copy to clipboard operation
opa copied to clipboard

Type checker failure on referencing generated map with numeric keys

Open anderseknert opened this issue 1 month ago • 2 comments

Trying to reference an item in a generated map by its numeric index in a some .. in iteration seems to have the type checker confused. I'd expect this to work:

package p

import rego.v1

ns := [1, 2, 3]

nums[x] contains ns if some x in ns

bug if {
	some n1 in ns
	some n2 in nums[n1]
}

But it doesn't:

❯ opa eval -f pretty -d p.rego data.p
1 error occurred: p.rego:11: rego_type_error: undefined ref: data.p.nums[n1][__local6__]
	data.p.nums[n1][__local6__]
	            ^
	            have (type): number
	            want (one of): [__local2__]

anderseknert avatar May 07 '24 08:05 anderseknert

Some further digging, and I've removed the "some .. in" constraint from the description as that wasn't required.

package p

import rego.v1

nums[x] contains x if some x in [1, 2, 3]

bug if {
    n1 := 1
    nums[n1]
}
❯ opa eval -f pretty -d p.rego data.p
1 error occurred: p.rego:9: rego_type_error: undefined ref: data.p.nums[n1]
	data.p.nums[n1]
	            ^
	            have (type): number
	            want (one of): [__local2__]

Using a numeric literal instead of a variable works:

not_bug if {
    nums[1]
}

It seems to only happen for numeric keys, as the same policy works when using string keys:

package p

import rego.v1

nums[x] contains x if some x in ["1", "2", "3"]

bug if {
    n1 := "1"
    nums[n1]
}

anderseknert avatar May 07 '24 11:05 anderseknert