Skip to content

Improve type error message when referencing functions #8253

Merged
johanfylling merged 5 commits intoopen-policy-agent:mainfrom
sspaink:improvederrormsg
Jan 29, 2026
Merged

Improve type error message when referencing functions #8253
johanfylling merged 5 commits intoopen-policy-agent:mainfrom
sspaink:improvederrormsg

Conversation

@sspaink
Copy link
Copy Markdown
Member

@sspaink sspaink commented Jan 27, 2026

resolve #6840

For the example provided in the issue, the new error will be:

❯ ./opa_darwin_arm64 check test.rego
1 error occurred: test.rego:8: rego_type_error: function data.play.f[__local1__] used as reference, not called
        data.play.f[__local1__]
        ^^^^^^^^^^^
        have: (any) => any

This does give the user more information that f is a function, unfortunately couldn't figure out a way to make __local1__ easier to understand.

Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
@netlify
Copy link
Copy Markdown

netlify bot commented Jan 27, 2026

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 9139b35
🔍 Latest deploy log https://app.netlify.com/projects/openpolicyagent/deploys/697b395f14e997000829f856
😎 Deploy Preview https://deploy-preview-8253--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

LGTM

Comment thread v1/ast/check_test.go Outdated
allow if {
some x in f
}`,
expectedError: "policy.rego:8: rego_type_error: function data.play.f[__local1__] used as reference, not called",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we perhaps go the extra mile and remove the [__local1__], too? If it's easy to do, figure out the prefix that is a function vs the tail of the ref... If it's cumbersome, forget about it, the new message is so much better than the old message already. This is just icing on the cake (I like icing on the cake).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Here you go some icing! Added a check to match the last part of the reference to the regex __local[0-9]+__ and drop it. Wish there was more context identifying what part is the function 🤔

but this does get us:

1 error occurred: test.rego:8: rego_type_error: function data.play.f used as reference, not called
        data.play.f[__local1__]
        ^^^^^^^^^^^
        have: (any) => any

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is also this logic that wraps the __local1__ in brackets that could be repurposed: https://github.com/open-policy-agent/opa/blob/main/v1/ast/term_appenders.go#L198-L223

Although it is vague enough that I am not sure what all the scenarios would be when something is wrapped in brackets. So I prefer the approach of handling this specific scenario until we find an example it doesn't work.

Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
@sspaink sspaink requested a review from srenatus January 27, 2026 20:33
sspaink and others added 2 commits January 27, 2026 14:38
@johanfylling johanfylling merged commit 0fed5e8 into open-policy-agent:main Jan 29, 2026
55 of 57 checks passed
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.

Hard to understand type error when referencing function as anything but function

3 participants