Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic on undefined variables in evaluator::arr::get_lazy #172

Closed
cpg314 opened this issue Jun 30, 2024 · 0 comments
Closed

Panic on undefined variables in evaluator::arr::get_lazy #172

cpg314 opened this issue Jun 30, 2024 · 0 comments

Comments

@cpg314
Copy link

cpg314 commented Jun 30, 2024

The following (invalid, as b is not defined) snippet

[x.value for x in std.objectKeysValues({ a: b })]

seems to trigger a panic during evaluation:

thread 'main' panicked at jrsonnet/crates/jrsonnet-evaluator/src/arr/spec.rs:658:14:
convertible: LocError((VariableIsNotDefined("b", []), StackTrace([StackTraceElement { location: Some(Source((SourcePath(SourceVirtual("input.jsonnet")), "[x.value for x in std.objectKeysValues({ a: b })]")):44-45), desc: "variable <b> access" }])))

The relevant code is here:

fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
let key = self.keys.get(index)?;
// Nothing can fail in the key part, yet value is still
// lazy-evaluated
Some(Thunk::evaluated(
KeyValue::into_untyped(KeyValue {
key: key.clone(),
value: self.obj.get_lazy_or_bail(key.clone()),
})
.expect("convertible"),

The following patch seems to work to propagate the error without a panic:

modified   crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -648,15 +648,14 @@ impl ArrayLike for PickObjectKeyValues {
 
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
 		let key = self.keys.get(index)?;
-		// Nothing can fail in the key part, yet value is still
-		// lazy-evaluated
-		Some(Thunk::evaluated(
+		Some(
 			KeyValue::into_untyped(KeyValue {
 				key: key.clone(),
 				value: self.obj.get_lazy_or_bail(key.clone()),
 			})
-			.expect("convertible"),
-		))
+			.map(Thunk::evaluated)
+			.unwrap_or_else(Thunk::errored),
+		)
 	}
LocError((VariableIsNotDefined("b", []), StackTrace([StackTraceElement { location: Some(Source((SourcePath(SourceVirtual("input.jsonnet")), "[x.value for x in std.objectKeysValues({ a: b })]")):44-45), desc: "variable <b> access" }, StackTraceElement { location: Some(Source((SourcePath(SourceVirtual("input.jsonnet")), "[x.value for x in std.objectKeysValues({ a: b })]")):1-2), desc: "variable <x> access" }])))
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

No branches or pull requests

1 participant