Skip to content

Commit 8cb7cfe

Browse files
Add array and object comprehensions to stack trace frames (#638)
## Summary - Array comprehensions (`Comp`) and object comprehensions (`ObjBody.ObjComp`) now appear in stack traces as `[array comprehension]` and `[object comprehension]`, giving better context when errors originate inside comprehension bodies. - Adds `exprErrorString` overrides and includes both types in the `isApplyOrBuiltin` frame filter. ## Test plan - [x] JVM tests pass (140/140) - [x] 12 golden files updated with new comprehension frames - [x] 3 EvaluatorTests assertions updated
1 parent 46e11e8 commit 8cb7cfe

15 files changed

Lines changed: 19 additions & 6 deletions

sjsonnet/src/sjsonnet/Error.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class Error(msg: String, val stack: List[Error.Frame] = Nil, underlying: Option[
5454
private def isApplyOrBuiltin(expr: Expr): Boolean = expr match {
5555
case _: Expr.Apply | _: Expr.Apply0 | _: Expr.Apply1 | _: Expr.Apply2 | _: Expr.Apply3 |
5656
_: Expr.ApplyBuiltin | _: Expr.ApplyBuiltin0 | _: Expr.ApplyBuiltin1 |
57-
_: Expr.ApplyBuiltin2 | _: Expr.ApplyBuiltin3 | _: Expr.ApplyBuiltin4 =>
57+
_: Expr.ApplyBuiltin2 | _: Expr.ApplyBuiltin3 | _: Expr.ApplyBuiltin4 | _: Expr.Comp |
58+
_: Expr.ObjBody.ObjComp =>
5859
true
5960
case _ => false
6061
}

sjsonnet/src/sjsonnet/Expr.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ object Expr {
343343
final case class Comp(pos: Position, value: Expr, first: ForSpec, rest: Array[CompSpec])
344344
extends Expr {
345345
final override private[sjsonnet] def tag = ExprTags.Comp
346+
override def exprErrorString: String = "array comprehension"
346347
}
347348
final case class ObjExtend(pos: Position, base: Expr, ext: ObjBody) extends Expr {
348349
final override private[sjsonnet] def tag = ExprTags.ObjExtend
@@ -371,6 +372,7 @@ object Expr {
371372
rest: List[CompSpec])
372373
extends ObjBody {
373374
final override private[sjsonnet] def tag = ExprTags.`ObjBody.ObjComp`
375+
override def exprErrorString: String = "object comprehension"
374376
override def toString: String =
375377
s"ObjComp($pos, ${arrStr(preLocals)}, $key, $value, ${arrStr(postLocals)}, $first, $rest)"
376378
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
sjsonnet.Error: In comprehension, can only iterate over array, not string
2+
at [array comprehension].(array_comp_try_iterate_over_empty_string.jsonnet:1:4)
23
at [<root>].(array_comp_try_iterate_over_empty_string.jsonnet:1:1)
34

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
sjsonnet.Error: In comprehension, can only iterate over array, not object
2+
at [array comprehension].(array_comp_try_iterate_over_obj.jsonnet:1:4)
23
at [<root>].(array_comp_try_iterate_over_obj.jsonnet:1:1)
34

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
sjsonnet.Error: In comprehension, can only iterate over array, not string
2+
at [array comprehension].(array_comp_try_iterate_over_string.jsonnet:1:4)
23
at [<root>].(array_comp_try_iterate_over_string.jsonnet:1:1)
34

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
sjsonnet.Error: x
2+
at [array comprehension].(arrcomp_if6.jsonnet:1:20)
23
at [<root>].(arrcomp_if6.jsonnet:1:1)
34

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
sjsonnet.Error: Condition must be boolean, got number
2+
at [array comprehension].(arrcomp_if7.jsonnet:1:23)
23
at [<root>].(arrcomp_if7.jsonnet:1:1)
34

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
sjsonnet.Error: Duplicate key x in evaluated object comprehension.
1+
sjsonnet.Error: [object comprehension] Duplicate key x in evaluated object comprehension.
22
at [<root>].(object_comp_duplicate.jsonnet:1:1)
33

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
sjsonnet.Error: xxx
2+
at [object comprehension].(object_comp_err_index.jsonnet:1:4)
23
at [<root>].(object_comp_err_index.jsonnet:1:1)
34

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
sjsonnet.Error: Field name must be string or null, not number
1+
sjsonnet.Error: [object comprehension] Field name must be string or null, not number
22
at [<root>].(object_comp_int_index.jsonnet:1:1)
33

0 commit comments

Comments
 (0)