wasm: include source info in runtime errors
Note that due to the mapping of rego to imperative code, the errors
may look different than what you'd expect. For example, with this rego,
package fr
p {
f(3)
}
f(x) = "y" {
x > 1
}
f(x) = "z" {
x > 2
}
the runtime error is
fun_rets.rego:12:3: var assignment conflict
Whereas `opa eval` gives you
$ opa eval 'data.fr.p' -d fun_rets.rego --format=pretty
1 error occurred: fun_rets.rego:11: eval_conflict_error: functions must not produce multiple outputs for same inputs
To get here, we're adding a new section to the static part of an ir.Policy: Files.
It's carrying the file name constants, and one special entry called `<query>`.
Most of the work in this change is ensuring that source location info is properly
carried over into the IR statements from AST nodes. There are new tests for the
planner "TestLocations", to assert a bunch of those mappings for different structures.
They attempt to not cover every statement, but a few select ones for each case.
The mappings works in an indirect way: there's a location stored in the planner's
state, `p.loc`, and it's used from `p.appendStmt`. For some constructions that bypass
this helper, there is an extra method, `p.appendStmtToBlock`, to carry over the location
from p.loc into the statements added that way.
Also:
* internal/ir: change location struct to include file and text
This helps with debugging:
| | | | *ir.BlockStmt BlockStmt (1 blocks)
| | | | | *ir.Block Block (6 statements)
| | | | | | *ir.MakeNumberRefStmt &{Index:1 Target:6 Location:{Index:0 Col:23 Row:4 file:module-0.rego text:1 > 0}}
| | | | | | *ir.MakeNumberRefStmt &{Index:2 Target:7 Location:{Index:0 Col:23 Row:4 file:module-0.rego text:1 > 0}}
| | | | | | *ir.GreaterThanStmt &{A:6 B:7 Location:{Index:0 Col:23 Row:4 file:module-0.rego text:1 > 0}}
| | | | | | *ir.MakeStringStmt &{Index:3 Target:8 Location:{Index:0 Col:23 Row:4 file:module-0.rego text:1 > 0}}
| | | | | | *ir.MakeStringStmt &{Index:4 Target:9 Location:{Index:0 Col:23 Row:4 file:module-0.rego text:1 > 0}}
| | | | | | *ir.ObjectInsertOnceStmt &{Key:8 Value:9 Object:5 Location:{Index:0 Col:23 Row:4 file:module-0.rego text:1 > 0}}
| | | | *ir.AssignVarOnceStmt &{Target:2 Source:5 Location:{Index:0 Col:23 Row:4 file:module-0.rego text:1 > 0}}
The fields are intentionally not exported, so they don't make their way
into the WASM module.
* Fixes error return of ir.Walk()
w.err would be ignored for the code path that walks leaves on the same level:
w.walk(x.Static) // error set here
w.walk(x.Plans) // would be overwritten here
w.walk(x.Funcs)
Co-Authored-By: Torin Sandall <torinsandall@gmail.com>
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com> S
Stephan Renatus committed
b40244fed614484f70934afff92b2220cf17cbbc
Parent: 346458b
Committed by Torin Sandall <torinsandall@gmail.com>
on 12/11/2020, 7:03:34 PM