difftreelog
Merge pull request #146 from CertainLach/fix/tests
in: master
Fix failing CI for tests and lints
22 files changed
cmds/jrsonnet-fmt/src/comments.rsdiffbeforeafterboth--- a/cmds/jrsonnet-fmt/src/comments.rs
+++ b/cmds/jrsonnet-fmt/src/comments.rs
@@ -72,7 +72,7 @@
if matches!(loc, CommentLocation::ItemInline) {
p!(pi: str(" "));
}
- p!(pi: str("/* ") string(lines[0].trim().to_string()) str(" */"))
+ p!(pi: str("/* ") string(lines[0].trim().to_string()) str(" */") nl)
} else if !lines.is_empty() {
fn common_ws_prefix<'a>(a: &'a str, b: &str) -> &'a str {
let offset = a
cmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet-fmt/src/main.rs
+++ b/cmds/jrsonnet-fmt/src/main.rs
@@ -372,8 +372,8 @@
return p!(new: str("{ }"));
}
let mut pi = p!(new: str("{") >i nl);
- for mem in children.into_iter() {
- if mem.should_start_with_newline {
+ for (i, mem) in children.into_iter().enumerate() {
+ if mem.should_start_with_newline && i != 0 {
p!(pi: nl);
}
p!(pi: items(format_comments(&mem.before_trivia, CommentLocation::AboveItem)));
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -6,7 +6,7 @@
};
use jrsonnet_stdlib::{TomlFormat, YamlFormat};
-#[derive(Clone, ValueEnum)]
+#[derive(Clone, Copy, ValueEnum)]
pub enum ManifestFormatName {
/// Expect string as output, and write them directly
String,
@@ -18,9 +18,11 @@
#[derive(Parser)]
#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]
pub struct ManifestOpts {
- /// Output format, wraps resulting value to corresponding std.manifest call.
- #[clap(long, short = 'f', default_value = "json")]
- format: ManifestFormatName,
+ /// Output format, wraps resulting value to corresponding std.manifest call
+ ///
+ /// [default: json, yaml when -y is used]
+ #[clap(long, short = 'f')]
+ format: Option<ManifestFormatName>,
/// Expect plain string as output.
/// Mutually exclusive with `--format`
#[clap(long, short = 'S', conflicts_with = "format")]
@@ -29,7 +31,9 @@
#[clap(long, short = 'y', conflicts_with = "string")]
yaml_stream: bool,
/// Number of spaces to pad output manifest with.
- /// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml/toml]
+ /// `0` for hard tabs, `-1` for single line output
+ ///
+ /// [default: 3 for json, 2 for yaml/toml]
#[clap(long)]
line_padding: Option<usize>,
/// Preserve order in object manifestification
@@ -44,7 +48,12 @@
} else {
#[cfg(feature = "exp-preserve-order")]
let preserve_order = self.preserve_order;
- match self.format {
+ let format = match self.format {
+ Some(v) => v,
+ None if self.yaml_stream => ManifestFormatName::Yaml,
+ None => ManifestFormatName::Json,
+ };
+ match format {
ManifestFormatName::String => Box::new(ToStringFormat),
ManifestFormatName::Json => Box::new(JsonFormat::cli(
self.line_padding.unwrap_or(3),
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -372,7 +372,7 @@
pub fn new_inclusive(start: i32, end: i32) -> Self {
Self { start, end }
}
- fn range(&self) -> impl Iterator<Item = i32> + ExactSizeIterator + DoubleEndedIterator {
+ fn range(&self) -> impl ExactSizeIterator<Item = i32> + DoubleEndedIterator {
WithExactSize(
self.start..=self.end,
(self.end as usize)
@@ -461,7 +461,7 @@
ArrayThunk::Waiting(..) => {}
};
- let ArrayThunk::Waiting(_) =
+ let ArrayThunk::Waiting(()) =
replace(&mut self.cached.borrow_mut()[index], ArrayThunk::Pending)
else {
unreachable!()
@@ -508,7 +508,7 @@
match &self.cached.borrow()[index] {
ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),
ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),
- ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}
+ ArrayThunk::Waiting(()) | ArrayThunk::Pending => {}
};
Some(Thunk::new(ArrayElement {
@@ -597,9 +597,7 @@
}
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
- let Some(key) = self.keys.get(index) else {
- return None;
- };
+ let key = self.keys.get(index)?;
Some(self.obj.get_lazy_or_bail(key.clone()))
}
@@ -649,9 +647,7 @@
}
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
- let Some(key) = self.keys.get(index) else {
- return None;
- };
+ let key = self.keys.get(index)?;
// Nothing can fail in the key part, yet value is still
// lazy-evaluated
Some(Thunk::evaluated(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13 arr::ArrValue,14 bail,15 destructure::evaluate_dest,16 error::{suggest_object_fields, ErrorKind::*},17 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},18 function::{CallLocation, FuncDesc, FuncVal},19 typed::Typed,20 val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},21 Context, Error, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,22 ResultExt, State, Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28 fn is_trivial(expr: &LocExpr) -> bool {29 match &*expr.0 {30 Expr::Str(_)31 | Expr::Num(_)32 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33 Expr::Arr(a) => a.iter().all(is_trivial),34 Expr::Parened(e) => is_trivial(e),35 _ => false,36 }37 }38 Some(match &*expr.0 {39 Expr::Str(s) => Val::string(s.clone()),40 Expr::Num(n) => Val::Num(*n),41 Expr::Literal(LiteralType::False) => Val::Bool(false),42 Expr::Literal(LiteralType::True) => Val::Bool(true),43 Expr::Literal(LiteralType::Null) => Val::Null,44 Expr::Arr(n) => {45 if n.iter().any(|e| !is_trivial(e)) {46 return None;47 }48 Val::Arr(ArrValue::eager(49 n.iter()50 .map(evaluate_trivial)51 .map(|e| e.expect("checked trivial"))52 .collect(),53 ))54 }55 Expr::Parened(e) => evaluate_trivial(e)?,56 _ => return None,57 })58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62 name,63 ctx,64 params,65 body,66 })))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70 Ok(match field_name {71 FieldName::Fixed(n) => Some(n.clone()),72 FieldName::Dyn(expr) => State::push(73 CallLocation::new(&expr.1),74 || "evaluating field name".to_string(),75 || {76 let value = evaluate(ctx, expr)?;77 if matches!(value, Val::Null) {78 Ok(None)79 } else {80 Ok(Some(IStr::from_untyped(value)?))81 }82 },83 )?,84 })85}8687pub fn evaluate_comp(88 ctx: Context,89 specs: &[CompSpec],90 callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92 match specs.get(0) {93 None => callback(ctx)?,94 Some(CompSpec::IfSpec(IfSpecData(cond))) => {95 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96 evaluate_comp(ctx, &specs[1..], callback)?;97 }98 }99 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100 Val::Arr(list) => {101 for item in list.iter_lazy() {102 let fctx = Pending::new();103 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104 destruct(var, item, fctx.clone(), &mut new_bindings)?;105 let ctx = ctx106 .clone()107 .extend(new_bindings, None, None, None)108 .into_future(fctx);109110 evaluate_comp(ctx, &specs[1..], callback)?;111 }112 }113 #[cfg(feature = "exp-object-iteration")]114 Val::Obj(obj) => {115 for field in obj.fields(116 // TODO: Should there be ability to preserve iteration order?117 #[cfg(feature = "exp-preserve-order")]118 false,119 ) {120 #[derive(Trace)]121 struct ObjectFieldThunk {122 obj: ObjValue,123 field: IStr,124 }125 impl ThunkValue for ObjectFieldThunk {126 type Output = Val;127128 fn get(self: Box<Self>) -> Result<Self::Output> {129 self.obj.get(self.field).transpose().expect(130 "field exists, as field name was obtained from object.fields()",131 )132 }133 }134135 let fctx = Pending::new();136 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![138 Thunk::evaluated(Val::string(field.clone())),139 Thunk::new(ObjectFieldThunk {140 field: field.clone(),141 obj: obj.clone(),142 }),143 ])));144 destruct(var, value, fctx.clone(), &mut new_bindings)?;145 let ctx = ctx146 .clone()147 .extend(new_bindings, None, None, None)148 .into_future(fctx);149150 evaluate_comp(ctx, &specs[1..], callback)?;151 }152 }153 _ => bail!(InComprehensionCanOnlyIterateOverArray),154 },155 }156 Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163 fctx: Pending<Context>,164 locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166 #[derive(Trace, Clone)]167 struct UnboundLocals {168 fctx: Pending<Context>,169 locals: Rc<Vec<BindSpec>>,170 }171 impl Unbound for UnboundLocals {172 type Bound = Context;173174 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175 let fctx = Context::new_future();176 let mut new_bindings =177 GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178 for b in self.locals.iter() {179 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180 }181182 let ctx = self.fctx.unwrap();183 let new_dollar = ctx.dollar().cloned().or_else(|| this.clone());184185 let ctx = ctx186 .extend(new_bindings, new_dollar, sup, this)187 .into_future(fctx);188189 Ok(ctx)190 }191 }192193 UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197 builder: &mut ObjValueBuilder,198 ctx: Context,199 uctx: B,200 field: &FieldMember,201) -> Result<()> {202 let name = evaluate_field_name(ctx, &field.name)?;203 let Some(name) = name else {204 return Ok(());205 };206207 match field {208 FieldMember {209 plus,210 params: None,211 visibility,212 value,213 ..214 } => {215 #[derive(Trace)]216 struct UnboundValue<B: Trace> {217 uctx: B,218 value: LocExpr,219 name: IStr,220 }221 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222 type Bound = Val;223 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224 evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225 }226 }227228 builder229 .field(name.clone())230 .with_add(*plus)231 .with_visibility(*visibility)232 .with_location(value.1.clone())233 .bindable(UnboundValue {234 uctx,235 value: value.clone(),236 name,237 })?;238 }239 FieldMember {240 params: Some(params),241 visibility,242 value,243 ..244 } => {245 #[derive(Trace)]246 struct UnboundMethod<B: Trace> {247 uctx: B,248 value: LocExpr,249 params: ParamsDesc,250 name: IStr,251 }252 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253 type Bound = Val;254 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255 Ok(evaluate_method(256 self.uctx.bind(sup, this)?,257 self.name.clone(),258 self.params.clone(),259 self.value.clone(),260 ))261 }262 }263264 builder265 .field(name.clone())266 .with_visibility(*visibility)267 .with_location(value.1.clone())268 .bindable(UnboundMethod {269 uctx,270 value: value.clone(),271 params: params.clone(),272 name,273 })?;274 }275 }276 Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281 let mut builder = ObjValueBuilder::new();282 let locals = Rc::new(283 members284 .iter()285 .filter_map(|m| match m {286 Member::BindStmt(bind) => Some(bind.clone()),287 _ => None,288 })289 .collect::<Vec<_>>(),290 );291292 let fctx = Context::new_future();293294 // We have single context for all fields, so we can cache binds295 let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297 for member in members {298 match member {299 Member::Field(field) => {300 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301 }302 Member::AssertStmt(stmt) => {303 #[derive(Trace)]304 struct ObjectAssert<B: Trace> {305 uctx: B,306 assert: AssertStmt,307 }308 impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309 fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310 let ctx = self.uctx.bind(sup, this)?;311 evaluate_assert(ctx, &self.assert)312 }313 }314 builder.assert(ObjectAssert {315 uctx: uctx.clone(),316 assert: stmt.clone(),317 });318 }319 Member::BindStmt(_) => {320 // Already handled321 }322 }323 }324 let this = builder.build();325 fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326 Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330 Ok(match object {331 ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332 ObjBody::ObjComp(obj) => {333 let mut builder = ObjValueBuilder::new();334 let locals = Rc::new(335 obj.pre_locals336 .iter()337 .chain(obj.post_locals.iter())338 .cloned()339 .collect::<Vec<_>>(),340 );341 let mut ctxs = vec![];342 evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343 let fctx = Context::new_future();344 ctxs.push((ctx.clone(), fctx.clone()));345 let uctx = evaluate_object_locals(fctx, locals.clone());346347 evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348 })?;349350 let this = builder.build();351 for (ctx, fctx) in ctxs {352 let _ctx = ctx353 .extend(GcHashMap::new(), None, None, Some(this.clone()))354 .into_future(fctx);355 }356 this357 }358 })359}360361pub fn evaluate_apply(362 ctx: Context,363 value: &LocExpr,364 args: &ArgsDesc,365 loc: CallLocation<'_>,366 tailstrict: bool,367) -> Result<Val> {368 let value = evaluate(ctx.clone(), value)?;369 Ok(match value {370 Val::Func(f) => {371 let body = || f.evaluate(ctx, loc, args, tailstrict);372 if tailstrict {373 body()?374 } else {375 State::push(loc, || format!("function <{}> call", f.name()), body)?376 }377 }378 v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),379 })380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383 let value = &assertion.0;384 let msg = &assertion.1;385 let assertion_result = State::push(386 CallLocation::new(&value.1),387 || "assertion condition".to_owned(),388 || bool::from_untyped(evaluate(ctx.clone(), value)?),389 )?;390 if !assertion_result {391 State::push(392 CallLocation::new(&value.1),393 || "assertion failure".to_owned(),394 || {395 if let Some(msg) = msg {396 bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397 }398 bail!(AssertionFailed(Val::Null.to_string()?));399 },400 )?;401 }402 Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406 use Expr::*;407 let LocExpr(raw_expr, _loc) = expr;408 Ok(match &**raw_expr {409 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410 _ => evaluate(ctx, expr)?,411 })412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416 use Expr::*;417418 if let Some(trivial) = evaluate_trivial(expr) {419 return Ok(trivial);420 }421 let LocExpr(expr, loc) = expr;422 Ok(match &**expr {423 Literal(LiteralType::This) => {424 Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())425 }426 Literal(LiteralType::Super) => Val::Obj(427 ctx.super_obj().ok_or(NoSuperFound)?.with_this(428 ctx.this()429 .expect("if super exists - then this should too")430 .clone(),431 ),432 ),433 Literal(LiteralType::Dollar) => {434 Val::Obj(ctx.dollar().ok_or(NoTopLevelObjectFound)?.clone())435 }436 Literal(LiteralType::True) => Val::Bool(true),437 Literal(LiteralType::False) => Val::Bool(false),438 Literal(LiteralType::Null) => Val::Null,439 Parened(e) => evaluate(ctx, e)?,440 Str(v) => Val::string(v.clone()),441 Num(v) => Val::new_checked_num(*v)?,442 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,443 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,444 Var(name) => State::push(445 CallLocation::new(loc),446 || format!("variable <{name}> access"),447 || ctx.binding(name.clone())?.evaluate(),448 )?,449 Index { indexable, parts } => {450 let mut parts = parts.iter();451 let mut indexable = match &indexable {452 // Cheaper to execute than creating object with overriden `this`453 LocExpr(v, _) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {454 let part = parts.next().expect("at least part should exist");455 let Some(super_obj) = ctx.super_obj() else {456 #[cfg(feature = "exp-null-coaelse")]457 if part.null_coaelse {458 return Ok(Val::Null);459 }460 bail!(NoSuperFound)461 };462 let name = evaluate(ctx.clone(), &part.value)?;463464 let Val::Str(name) = name else {465 bail!(ValueIndexMustBeTypeGot(466 ValType::Obj,467 ValType::Str,468 name.value_type(),469 ))470 };471472 let this = ctx473 .this()474 .expect("no this found, while super present, should not happen");475 let name = name.into_flat();476 match super_obj477 .get_for(name.clone(), this.clone())478 .with_description_src(&part.value, || format!("field <{name}> access"))?479 {480 Some(v) => v,481 #[cfg(feature = "exp-null-coaelse")]482 None if part.null_coaelse => return Ok(Val::Null),483 None => {484 let suggestions = suggest_object_fields(super_obj, name.clone());485486 bail!(NoSuchField(name, suggestions))487 }488 }489 }490 e => evaluate(ctx.clone(), e)?,491 };492493 for part in parts {494 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {495 (Val::Obj(v), Val::Str(key)) => match v496 .get(key.clone().into_flat())497 .with_description_src(&part.value, || format!("field <{key}> access"))?498 {499 Some(v) => v,500 #[cfg(feature = "exp-null-coaelse")]501 None if part.null_coaelse => return Ok(Val::Null),502 None => {503 let suggestions = suggest_object_fields(&v, key.clone().into_flat());504505 return Err(Error::from(NoSuchField(506 key.clone().into_flat(),507 suggestions,508 )))509 .with_description_src(&part.value, || format!("field <{key}> access"));510 }511 },512 (Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(513 ValType::Obj,514 ValType::Str,515 n.value_type(),516 )),517 (Val::Arr(v), Val::Num(n)) => {518 if n.fract() > f64::EPSILON {519 bail!(FractionalIndex)520 }521 if n < 0.0 {522 bail!(ArrayBoundsError(n as isize, v.len()));523 }524 v.get(n as usize)?525 .ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?526 }527 (Val::Arr(_), Val::Str(n)) => {528 bail!(AttemptedIndexAnArrayWithString(n.into_flat()))529 }530 (Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(531 ValType::Arr,532 ValType::Num,533 n.value_type(),534 )),535536 (Val::Str(s), Val::Num(n)) => Val::Str({537 let v: IStr = s538 .clone()539 .into_flat()540 .chars()541 .skip(n as usize)542 .take(1)543 .collect::<String>()544 .into();545 if v.is_empty() {546 let size = s.into_flat().chars().count();547 bail!(StringBoundsError(n as usize, size))548 }549 StrValue::Flat(v)550 }),551 (Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(552 ValType::Str,553 ValType::Num,554 n.value_type(),555 )),556 #[cfg(feature = "exp-null-coaelse")]557 (Val::Null, _) if part.null_coaelse => return Ok(Val::Null),558 (v, _) => bail!(CantIndexInto(v.value_type())),559 };560 }561 indexable562 }563 LocalExpr(bindings, returned) => {564 let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =565 GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());566 let fctx = Context::new_future();567 for b in bindings {568 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;569 }570 let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);571 evaluate(ctx, &returned.clone())?572 }573 Arr(items) => {574 if items.is_empty() {575 Val::Arr(ArrValue::empty())576 } else if items.len() == 1 {577 #[derive(Trace)]578 struct ArrayElement {579 ctx: Context,580 item: LocExpr,581 }582 impl ThunkValue for ArrayElement {583 type Output = Val;584 fn get(self: Box<Self>) -> Result<Val> {585 evaluate(self.ctx, &self.item)586 }587 }588 Val::Arr(ArrValue::lazy(vec![Thunk::new(ArrayElement {589 ctx,590 item: items[0].clone(),591 })]))592 } else {593 Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))594 }595 }596 ArrComp(expr, comp_specs) => {597 let mut out = Vec::new();598 evaluate_comp(ctx, comp_specs, &mut |ctx| {599 out.push(evaluate(ctx, expr)?);600 Ok(())601 })?;602 Val::Arr(ArrValue::eager(out))603 }604 Obj(body) => Val::Obj(evaluate_object(ctx, body)?),605 ObjExtend(a, b) => evaluate_add_op(606 &evaluate(ctx.clone(), a)?,607 &Val::Obj(evaluate_object(ctx, b)?),608 )?,609 Apply(value, args, tailstrict) => {610 evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?611 }612 Function(params, body) => {613 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())614 }615 AssertExpr(assert, returned) => {616 evaluate_assert(ctx.clone(), assert)?;617 evaluate(ctx, returned)?618 }619 ErrorStmt(e) => State::push(620 CallLocation::new(loc),621 || "error statement".to_owned(),622 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),623 )?,624 IfElse {625 cond,626 cond_then,627 cond_else,628 } => {629 if State::push(630 CallLocation::new(loc),631 || "if condition".to_owned(),632 || bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),633 )? {634 evaluate(ctx, cond_then)?635 } else {636 match cond_else {637 Some(v) => evaluate(ctx, v)?,638 None => Val::Null,639 }640 }641 }642 Slice(value, desc) => {643 fn parse_idx<T: Typed>(644 loc: CallLocation<'_>,645 ctx: &Context,646 expr: Option<&LocExpr>,647 desc: &'static str,648 ) -> Result<Option<T>> {649 if let Some(value) = expr {650 Ok(Some(State::push(651 loc,652 || format!("slice {desc}"),653 || T::from_untyped(evaluate(ctx.clone(), value)?),654 )?))655 } else {656 Ok(None)657 }658 }659660 let indexable = evaluate(ctx.clone(), value)?;661 let loc = CallLocation::new(loc);662663 let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;664 let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;665 let step = parse_idx(loc, &ctx, desc.step.as_ref(), "step")?;666667 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?668 }669 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {670 let Expr::Str(path) = &*path.0 else {671 bail!("computed imports are not supported")672 };673 let tmp = loc.clone().0;674 let s = ctx.state();675 let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;676 match i {677 Import(_) => State::push(678 CallLocation::new(loc),679 || format!("import {:?}", path.clone()),680 || s.import_resolved(resolved_path),681 )?,682 ImportStr(_) => Val::string(s.import_resolved_str(resolved_path)?),683 ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),684 _ => unreachable!(),685 }686 }687 })688}1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13 arr::ArrValue,14 bail,15 destructure::evaluate_dest,16 error::{suggest_object_fields, ErrorKind::*},17 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},18 function::{CallLocation, FuncDesc, FuncVal},19 typed::Typed,20 val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},21 Context, Error, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,22 ResultExt, State, Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28 fn is_trivial(expr: &LocExpr) -> bool {29 match &*expr.0 {30 Expr::Str(_)31 | Expr::Num(_)32 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33 Expr::Arr(a) => a.iter().all(is_trivial),34 Expr::Parened(e) => is_trivial(e),35 _ => false,36 }37 }38 Some(match &*expr.0 {39 Expr::Str(s) => Val::string(s.clone()),40 Expr::Num(n) => Val::Num(*n),41 Expr::Literal(LiteralType::False) => Val::Bool(false),42 Expr::Literal(LiteralType::True) => Val::Bool(true),43 Expr::Literal(LiteralType::Null) => Val::Null,44 Expr::Arr(n) => {45 if n.iter().any(|e| !is_trivial(e)) {46 return None;47 }48 Val::Arr(ArrValue::eager(49 n.iter()50 .map(evaluate_trivial)51 .map(|e| e.expect("checked trivial"))52 .collect(),53 ))54 }55 Expr::Parened(e) => evaluate_trivial(e)?,56 _ => return None,57 })58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62 name,63 ctx,64 params,65 body,66 })))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70 Ok(match field_name {71 FieldName::Fixed(n) => Some(n.clone()),72 FieldName::Dyn(expr) => State::push(73 CallLocation::new(&expr.1),74 || "evaluating field name".to_string(),75 || {76 let value = evaluate(ctx, expr)?;77 if matches!(value, Val::Null) {78 Ok(None)79 } else {80 Ok(Some(IStr::from_untyped(value)?))81 }82 },83 )?,84 })85}8687pub fn evaluate_comp(88 ctx: Context,89 specs: &[CompSpec],90 callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92 match specs.first() {93 None => callback(ctx)?,94 Some(CompSpec::IfSpec(IfSpecData(cond))) => {95 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96 evaluate_comp(ctx, &specs[1..], callback)?;97 }98 }99 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100 Val::Arr(list) => {101 for item in list.iter_lazy() {102 let fctx = Pending::new();103 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104 destruct(var, item, fctx.clone(), &mut new_bindings)?;105 let ctx = ctx106 .clone()107 .extend(new_bindings, None, None, None)108 .into_future(fctx);109110 evaluate_comp(ctx, &specs[1..], callback)?;111 }112 }113 #[cfg(feature = "exp-object-iteration")]114 Val::Obj(obj) => {115 for field in obj.fields(116 // TODO: Should there be ability to preserve iteration order?117 #[cfg(feature = "exp-preserve-order")]118 false,119 ) {120 #[derive(Trace)]121 struct ObjectFieldThunk {122 obj: ObjValue,123 field: IStr,124 }125 impl ThunkValue for ObjectFieldThunk {126 type Output = Val;127128 fn get(self: Box<Self>) -> Result<Self::Output> {129 self.obj.get(self.field).transpose().expect(130 "field exists, as field name was obtained from object.fields()",131 )132 }133 }134135 let fctx = Pending::new();136 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![138 Thunk::evaluated(Val::string(field.clone())),139 Thunk::new(ObjectFieldThunk {140 field: field.clone(),141 obj: obj.clone(),142 }),143 ])));144 destruct(var, value, fctx.clone(), &mut new_bindings)?;145 let ctx = ctx146 .clone()147 .extend(new_bindings, None, None, None)148 .into_future(fctx);149150 evaluate_comp(ctx, &specs[1..], callback)?;151 }152 }153 _ => bail!(InComprehensionCanOnlyIterateOverArray),154 },155 }156 Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163 fctx: Pending<Context>,164 locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166 #[derive(Trace, Clone)]167 struct UnboundLocals {168 fctx: Pending<Context>,169 locals: Rc<Vec<BindSpec>>,170 }171 impl Unbound for UnboundLocals {172 type Bound = Context;173174 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175 let fctx = Context::new_future();176 let mut new_bindings =177 GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178 for b in self.locals.iter() {179 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180 }181182 let ctx = self.fctx.unwrap();183 let new_dollar = ctx.dollar().cloned().or_else(|| this.clone());184185 let ctx = ctx186 .extend(new_bindings, new_dollar, sup, this)187 .into_future(fctx);188189 Ok(ctx)190 }191 }192193 UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197 builder: &mut ObjValueBuilder,198 ctx: Context,199 uctx: B,200 field: &FieldMember,201) -> Result<()> {202 let name = evaluate_field_name(ctx, &field.name)?;203 let Some(name) = name else {204 return Ok(());205 };206207 match field {208 FieldMember {209 plus,210 params: None,211 visibility,212 value,213 ..214 } => {215 #[derive(Trace)]216 struct UnboundValue<B: Trace> {217 uctx: B,218 value: LocExpr,219 name: IStr,220 }221 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222 type Bound = Val;223 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224 evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225 }226 }227228 builder229 .field(name.clone())230 .with_add(*plus)231 .with_visibility(*visibility)232 .with_location(value.1.clone())233 .bindable(UnboundValue {234 uctx,235 value: value.clone(),236 name,237 })?;238 }239 FieldMember {240 params: Some(params),241 visibility,242 value,243 ..244 } => {245 #[derive(Trace)]246 struct UnboundMethod<B: Trace> {247 uctx: B,248 value: LocExpr,249 params: ParamsDesc,250 name: IStr,251 }252 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253 type Bound = Val;254 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255 Ok(evaluate_method(256 self.uctx.bind(sup, this)?,257 self.name.clone(),258 self.params.clone(),259 self.value.clone(),260 ))261 }262 }263264 builder265 .field(name.clone())266 .with_visibility(*visibility)267 .with_location(value.1.clone())268 .bindable(UnboundMethod {269 uctx,270 value: value.clone(),271 params: params.clone(),272 name,273 })?;274 }275 }276 Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281 let mut builder = ObjValueBuilder::new();282 let locals = Rc::new(283 members284 .iter()285 .filter_map(|m| match m {286 Member::BindStmt(bind) => Some(bind.clone()),287 _ => None,288 })289 .collect::<Vec<_>>(),290 );291292 let fctx = Context::new_future();293294 // We have single context for all fields, so we can cache binds295 let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297 for member in members {298 match member {299 Member::Field(field) => {300 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301 }302 Member::AssertStmt(stmt) => {303 #[derive(Trace)]304 struct ObjectAssert<B: Trace> {305 uctx: B,306 assert: AssertStmt,307 }308 impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309 fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310 let ctx = self.uctx.bind(sup, this)?;311 evaluate_assert(ctx, &self.assert)312 }313 }314 builder.assert(ObjectAssert {315 uctx: uctx.clone(),316 assert: stmt.clone(),317 });318 }319 Member::BindStmt(_) => {320 // Already handled321 }322 }323 }324 let this = builder.build();325 fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326 Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330 Ok(match object {331 ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332 ObjBody::ObjComp(obj) => {333 let mut builder = ObjValueBuilder::new();334 let locals = Rc::new(335 obj.pre_locals336 .iter()337 .chain(obj.post_locals.iter())338 .cloned()339 .collect::<Vec<_>>(),340 );341 let mut ctxs = vec![];342 evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343 let fctx = Context::new_future();344 ctxs.push((ctx.clone(), fctx.clone()));345 let uctx = evaluate_object_locals(fctx, locals.clone());346347 evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348 })?;349350 let this = builder.build();351 for (ctx, fctx) in ctxs {352 let _ctx = ctx353 .extend(GcHashMap::new(), None, None, Some(this.clone()))354 .into_future(fctx);355 }356 this357 }358 })359}360361pub fn evaluate_apply(362 ctx: Context,363 value: &LocExpr,364 args: &ArgsDesc,365 loc: CallLocation<'_>,366 tailstrict: bool,367) -> Result<Val> {368 let value = evaluate(ctx.clone(), value)?;369 Ok(match value {370 Val::Func(f) => {371 let body = || f.evaluate(ctx, loc, args, tailstrict);372 if tailstrict {373 body()?374 } else {375 State::push(loc, || format!("function <{}> call", f.name()), body)?376 }377 }378 v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),379 })380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383 let value = &assertion.0;384 let msg = &assertion.1;385 let assertion_result = State::push(386 CallLocation::new(&value.1),387 || "assertion condition".to_owned(),388 || bool::from_untyped(evaluate(ctx.clone(), value)?),389 )?;390 if !assertion_result {391 State::push(392 CallLocation::new(&value.1),393 || "assertion failure".to_owned(),394 || {395 if let Some(msg) = msg {396 bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397 }398 bail!(AssertionFailed(Val::Null.to_string()?));399 },400 )?;401 }402 Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406 use Expr::*;407 let LocExpr(raw_expr, _loc) = expr;408 Ok(match &**raw_expr {409 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410 _ => evaluate(ctx, expr)?,411 })412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416 use Expr::*;417418 if let Some(trivial) = evaluate_trivial(expr) {419 return Ok(trivial);420 }421 let LocExpr(expr, loc) = expr;422 Ok(match &**expr {423 Literal(LiteralType::This) => {424 Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())425 }426 Literal(LiteralType::Super) => Val::Obj(427 ctx.super_obj().ok_or(NoSuperFound)?.with_this(428 ctx.this()429 .expect("if super exists - then this should too")430 .clone(),431 ),432 ),433 Literal(LiteralType::Dollar) => {434 Val::Obj(ctx.dollar().ok_or(NoTopLevelObjectFound)?.clone())435 }436 Literal(LiteralType::True) => Val::Bool(true),437 Literal(LiteralType::False) => Val::Bool(false),438 Literal(LiteralType::Null) => Val::Null,439 Parened(e) => evaluate(ctx, e)?,440 Str(v) => Val::string(v.clone()),441 Num(v) => Val::new_checked_num(*v)?,442 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,443 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,444 Var(name) => State::push(445 CallLocation::new(loc),446 || format!("variable <{name}> access"),447 || ctx.binding(name.clone())?.evaluate(),448 )?,449 Index { indexable, parts } => {450 let mut parts = parts.iter();451 let mut indexable = match &indexable {452 // Cheaper to execute than creating object with overriden `this`453 LocExpr(v, _) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {454 let part = parts.next().expect("at least part should exist");455 let Some(super_obj) = ctx.super_obj() else {456 #[cfg(feature = "exp-null-coaelse")]457 if part.null_coaelse {458 return Ok(Val::Null);459 }460 bail!(NoSuperFound)461 };462 let name = evaluate(ctx.clone(), &part.value)?;463464 let Val::Str(name) = name else {465 bail!(ValueIndexMustBeTypeGot(466 ValType::Obj,467 ValType::Str,468 name.value_type(),469 ))470 };471472 let this = ctx473 .this()474 .expect("no this found, while super present, should not happen");475 let name = name.into_flat();476 match super_obj477 .get_for(name.clone(), this.clone())478 .with_description_src(&part.value, || format!("field <{name}> access"))?479 {480 Some(v) => v,481 #[cfg(feature = "exp-null-coaelse")]482 None if part.null_coaelse => return Ok(Val::Null),483 None => {484 let suggestions = suggest_object_fields(super_obj, name.clone());485486 bail!(NoSuchField(name, suggestions))487 }488 }489 }490 e => evaluate(ctx.clone(), e)?,491 };492493 for part in parts {494 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {495 (Val::Obj(v), Val::Str(key)) => match v496 .get(key.clone().into_flat())497 .with_description_src(&part.value, || format!("field <{key}> access"))?498 {499 Some(v) => v,500 #[cfg(feature = "exp-null-coaelse")]501 None if part.null_coaelse => return Ok(Val::Null),502 None => {503 let suggestions = suggest_object_fields(&v, key.clone().into_flat());504505 return Err(Error::from(NoSuchField(506 key.clone().into_flat(),507 suggestions,508 )))509 .with_description_src(&part.value, || format!("field <{key}> access"));510 }511 },512 (Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(513 ValType::Obj,514 ValType::Str,515 n.value_type(),516 )),517 (Val::Arr(v), Val::Num(n)) => {518 if n.fract() > f64::EPSILON {519 bail!(FractionalIndex)520 }521 if n < 0.0 {522 bail!(ArrayBoundsError(n as isize, v.len()));523 }524 v.get(n as usize)?525 .ok_or_else(|| ArrayBoundsError(n as isize, v.len()))?526 }527 (Val::Arr(_), Val::Str(n)) => {528 bail!(AttemptedIndexAnArrayWithString(n.into_flat()))529 }530 (Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(531 ValType::Arr,532 ValType::Num,533 n.value_type(),534 )),535536 (Val::Str(s), Val::Num(n)) => Val::Str({537 let v: IStr = s538 .clone()539 .into_flat()540 .chars()541 .skip(n as usize)542 .take(1)543 .collect::<String>()544 .into();545 if v.is_empty() {546 let size = s.into_flat().chars().count();547 bail!(StringBoundsError(n as usize, size))548 }549 StrValue::Flat(v)550 }),551 (Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(552 ValType::Str,553 ValType::Num,554 n.value_type(),555 )),556 #[cfg(feature = "exp-null-coaelse")]557 (Val::Null, _) if part.null_coaelse => return Ok(Val::Null),558 (v, _) => bail!(CantIndexInto(v.value_type())),559 };560 }561 indexable562 }563 LocalExpr(bindings, returned) => {564 let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =565 GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());566 let fctx = Context::new_future();567 for b in bindings {568 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;569 }570 let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);571 evaluate(ctx, &returned.clone())?572 }573 Arr(items) => {574 if items.is_empty() {575 Val::Arr(ArrValue::empty())576 } else if items.len() == 1 {577 #[derive(Trace)]578 struct ArrayElement {579 ctx: Context,580 item: LocExpr,581 }582 impl ThunkValue for ArrayElement {583 type Output = Val;584 fn get(self: Box<Self>) -> Result<Val> {585 evaluate(self.ctx, &self.item)586 }587 }588 Val::Arr(ArrValue::lazy(vec![Thunk::new(ArrayElement {589 ctx,590 item: items[0].clone(),591 })]))592 } else {593 Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))594 }595 }596 ArrComp(expr, comp_specs) => {597 let mut out = Vec::new();598 evaluate_comp(ctx, comp_specs, &mut |ctx| {599 out.push(evaluate(ctx, expr)?);600 Ok(())601 })?;602 Val::Arr(ArrValue::eager(out))603 }604 Obj(body) => Val::Obj(evaluate_object(ctx, body)?),605 ObjExtend(a, b) => evaluate_add_op(606 &evaluate(ctx.clone(), a)?,607 &Val::Obj(evaluate_object(ctx, b)?),608 )?,609 Apply(value, args, tailstrict) => {610 evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?611 }612 Function(params, body) => {613 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())614 }615 AssertExpr(assert, returned) => {616 evaluate_assert(ctx.clone(), assert)?;617 evaluate(ctx, returned)?618 }619 ErrorStmt(e) => State::push(620 CallLocation::new(loc),621 || "error statement".to_owned(),622 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),623 )?,624 IfElse {625 cond,626 cond_then,627 cond_else,628 } => {629 if State::push(630 CallLocation::new(loc),631 || "if condition".to_owned(),632 || bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),633 )? {634 evaluate(ctx, cond_then)?635 } else {636 match cond_else {637 Some(v) => evaluate(ctx, v)?,638 None => Val::Null,639 }640 }641 }642 Slice(value, desc) => {643 fn parse_idx<T: Typed>(644 loc: CallLocation<'_>,645 ctx: &Context,646 expr: Option<&LocExpr>,647 desc: &'static str,648 ) -> Result<Option<T>> {649 if let Some(value) = expr {650 Ok(Some(State::push(651 loc,652 || format!("slice {desc}"),653 || T::from_untyped(evaluate(ctx.clone(), value)?),654 )?))655 } else {656 Ok(None)657 }658 }659660 let indexable = evaluate(ctx.clone(), value)?;661 let loc = CallLocation::new(loc);662663 let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;664 let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;665 let step = parse_idx(loc, &ctx, desc.step.as_ref(), "step")?;666667 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?668 }669 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {670 let Expr::Str(path) = &*path.0 else {671 bail!("computed imports are not supported")672 };673 let tmp = loc.clone().0;674 let s = ctx.state();675 let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;676 match i {677 Import(_) => State::push(678 CallLocation::new(loc),679 || format!("import {:?}", path.clone()),680 || s.import_resolved(resolved_path),681 )?,682 ImportStr(_) => Val::string(s.import_resolved_str(resolved_path)?),683 ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),684 _ => unreachable!(),685 }686 }687 })688}crates/jrsonnet-evaluator/src/function/builtin.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/builtin.rs
+++ b/crates/jrsonnet-evaluator/src/function/builtin.rs
@@ -6,8 +6,8 @@
use super::{arglike::ArgsLike, parse::parse_builtin_call, CallLocation};
use crate::{gc::TraceBox, tb, Context, Result, Val};
-/// Can't have str | IStr, because constant BuiltinParam causes
-/// E0492: constant functions cannot refer to interior mutable data
+/// Can't have `str` | `IStr`, because constant `BuiltinParam` causes
+/// `E0492: constant functions cannot refer to interior mutable data`
#[derive(Clone, Trace)]
pub struct ParamName(Option<Cow<'static, str>>);
impl ParamName {
@@ -27,10 +27,9 @@
}
impl PartialEq<IStr> for ParamName {
fn eq(&self, other: &IStr) -> bool {
- match &self.0 {
- Some(s) => s.as_bytes() == other.as_bytes(),
- None => false,
- }
+ self.0
+ .as_ref()
+ .map_or(false, |s| s.as_bytes() == other.as_bytes())
}
}
@@ -87,7 +86,7 @@
params: params
.into_iter()
.map(|n| BuiltinParam {
- name: ParamName::new_dynamic(n.to_string()),
+ name: ParamName::new_dynamic(n),
has_default: false,
})
.collect(),
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -159,11 +159,11 @@
Val::Null => serializer.serialize_none(),
Val::Str(s) => serializer.serialize_str(&s.clone().into_flat()),
Val::Num(n) => {
- if n.fract() != 0.0 {
- serializer.serialize_f64(*n)
- } else {
+ if n.fract() == 0.0 {
let n = *n as i64;
serializer.serialize_i64(n)
+ } else {
+ serializer.serialize_f64(*n)
}
}
#[cfg(feature = "exp-bigint")]
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -41,10 +41,12 @@
clippy::missing_const_for_fn,
// too many false-positives with .expect() calls
clippy::missing_panics_doc,
- // false positive for IStr type. There is an configuration option for
- // such cases, but it doesn't work:
- // https://github.com/rust-lang/rust-clippy/issues/9801
- clippy::mutable_key_type,
+ // false positive for IStr type. There is an configuration option for
+ // such cases, but it doesn't work:
+ // https://github.com/rust-lang/rust-clippy/issues/9801
+ clippy::mutable_key_type,
+ // false positives
+ clippy::redundant_pub_crate,
)]
// For jrsonnet-macros
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -175,6 +175,8 @@
manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;
Ok(out)
}
+
+#[allow(clippy::too_many_lines)]
fn manifest_json_ex_buf(
val: &Val,
buf: &mut String,
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -171,7 +171,7 @@
// .field("assertions_ran", &self.assertions_ran)
.field("this_entries", &self.this_entries)
// .field("value_cache", &self.value_cache)
- .finish()
+ .finish_non_exhaustive()
}
}
@@ -347,7 +347,7 @@
out.with_super(self);
let mut member = out.field(key);
if value.flags.add() {
- member = member.add()
+ member = member.add();
}
if let Some(loc) = value.location {
member = member.with_location(loc);
@@ -395,7 +395,7 @@
pub fn get(&self, key: IStr) -> Result<Option<Val>> {
self.run_assertions()?;
- self.get_for(key, self.0.this().unwrap_or(self.clone()))
+ self.get_for(key, self.0.this().unwrap_or_else(|| self.clone()))
}
pub fn get_for(&self, key: IStr, this: ObjValue) -> Result<Option<Val>> {
@@ -474,7 +474,7 @@
type Output = Val;
fn get(self: Box<Self>) -> Result<Self::Output> {
- Ok(self.obj.get_or_bail(self.key)?)
+ self.obj.get_or_bail(self.key)
}
}
@@ -495,7 +495,7 @@
SuperDepth::default(),
&mut |depth, index, name, visibility| {
let new_sort_key = FieldSortKey::new(depth, index);
- let entry = out.entry(name.clone());
+ let entry = out.entry(name);
let (visible, _) = entry.or_insert((true, new_sort_key));
match visibility {
Visibility::Normal => {}
@@ -634,7 +634,7 @@
SuperDepth::default(),
&mut |depth, index, name, visibility| {
let new_sort_key = FieldSortKey::new(depth, index);
- let entry = out.entry(name.clone());
+ let entry = out.entry(name);
let (visible, _) = entry.or_insert((true, new_sort_key));
match visibility {
Visibility::Normal => {}
crates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -248,7 +248,7 @@
let (cflags, str) = try_parse_cflags(str)?;
let (width, str) = try_parse_field_width(str)?;
let (precision, str) = try_parse_precision(str)?;
- let (_, str) = try_parse_length_modifier(str)?;
+ let ((), str) = try_parse_length_modifier(str)?;
let (convtype, str) = parse_conversion_type(str)?;
Ok((
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -449,25 +449,21 @@
}
fn from_untyped(value: Val) -> Result<Self> {
- match &value {
- Val::Arr(a) => {
- if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {
- return Ok(bytes.0.as_slice().into());
- };
- <Self as Typed>::TYPE.check(&value)?;
- // Any::downcast_ref::<ByteArray>(&a);
- let mut out = Vec::with_capacity(a.len());
- for e in a.iter() {
- let r = e?;
- out.push(u8::from_untyped(r)?);
- }
- Ok(out.as_slice().into())
- }
- _ => {
- <Self as Typed>::TYPE.check(&value)?;
- unreachable!()
- }
+ let Val::Arr(a) = &value else {
+ <Self as Typed>::TYPE.check(&value)?;
+ unreachable!()
+ };
+ if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {
+ return Ok(bytes.0.as_slice().into());
+ };
+ <Self as Typed>::TYPE.check(&value)?;
+ // Any::downcast_ref::<ByteArray>(&a);
+ let mut out = Vec::with_capacity(a.len());
+ for e in a.iter() {
+ let r = e?;
+ out.push(u8::from_untyped(r)?);
}
+ Ok(out.as_slice().into())
}
}
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -90,7 +90,7 @@
item: impl Fn() -> Result<()>,
) -> Result<()> {
State::push_description(error_reason, || match item() {
- Ok(_) => Ok(()),
+ Ok(()) => Ok(()),
Err(mut e) => {
if let ErrorKind::TypeError(e) = &mut e.error_mut() {
(e.1).0.push(path());
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -351,6 +351,8 @@
}
}
impl PartialEq for StrValue {
+ // False positive, into_flat returns not StrValue, but IStr, thus no infinite recursion here.
+ #[allow(clippy::unconditional_recursion)]
fn eq(&self, other: &Self) -> bool {
let a = self.clone().into_flat();
let b = other.clone().into_flat();
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -6,7 +6,7 @@
#![warn(clippy::pedantic, clippy::nursery)]
#![allow(clippy::missing_const_for_fn)]
use std::{
- borrow::{Borrow, Cow},
+ borrow::Cow,
cell::RefCell,
fmt::{self, Display},
hash::{BuildHasherDefault, Hash, Hasher},
@@ -14,7 +14,7 @@
str,
};
-use hashbrown::HashMap;
+use hashbrown::{hash_map::RawEntryMut, HashMap};
use jrsonnet_gcmodule::Trace;
use rustc_hash::FxHasher;
@@ -57,17 +57,6 @@
}
}
-impl Borrow<str> for IStr {
- fn borrow(&self) -> &str {
- self.as_str()
- }
-}
-impl Borrow<[u8]> for IStr {
- fn borrow(&self) -> &[u8] {
- self.as_bytes()
- }
-}
-
impl PartialEq for IStr {
fn eq(&self, other: &Self) -> bool {
// all IStr should be inlined into same pool
@@ -142,12 +131,6 @@
type Target = [u8];
fn deref(&self) -> &Self::Target {
- self.0.as_slice()
- }
-}
-
-impl Borrow<[u8]> for IBytes {
- fn borrow(&self) -> &[u8] {
self.0.as_slice()
}
}
@@ -285,9 +268,9 @@
let mut pool = pool.borrow_mut();
let entry = pool.raw_entry_mut().from_key(bytes);
match entry {
- hashbrown::hash_map::RawEntryMut::Occupied(i) => IBytes(i.get_key_value().0.clone()),
- hashbrown::hash_map::RawEntryMut::Vacant(e) => {
- let (k, _) = e.insert(Inner::new_bytes(bytes), ());
+ RawEntryMut::Occupied(i) => IBytes(i.get_key_value().0.clone()),
+ RawEntryMut::Vacant(e) => {
+ let (k, ()) = e.insert(Inner::new_bytes(bytes), ());
IBytes(k.clone())
}
}
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -374,6 +374,7 @@
fn params(&self) -> &[BuiltinParam] {
PARAMS
}
+ #[allow(unused_variable)]
fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {
let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;
crates/jrsonnet-stdlib/src/encoding.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/encoding.rs
+++ b/crates/jrsonnet-stdlib/src/encoding.rs
@@ -39,5 +39,5 @@
let bytes = STANDARD
.decode(str.as_bytes())
.map_err(|e| runtime_error!("invalid base64: {e}"))?;
- Ok(String::from_utf8(bytes).map_err(|_| runtime_error!("bad utf8"))?)
+ String::from_utf8(bytes).map_err(|_| runtime_error!("bad utf8"))
}
crates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/yaml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/yaml.rs
@@ -134,6 +134,14 @@
buf.push_str(&options.padding);
buf.push_str(line);
}
+ } else if s.contains('\n') {
+ buf.push_str("|-");
+ for line in s.split('\n') {
+ buf.push('\n');
+ buf.push_str(cur_padding);
+ buf.push_str(&options.padding);
+ buf.push_str(line);
+ }
} else if !options.quote_keys && !yaml_needs_quotes(&s) {
buf.push_str(&s);
} else {
crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -47,7 +47,7 @@
.ext_natives
.get(&x)
.cloned()
- .map_or(Val::Null, |v| Val::Func(v))
+ .map_or(Val::Null, Val::Func)
}
#[builtin(fields(
flake.lockdiffbeforeafterboth--- a/flake.lock
+++ b/flake.lock
@@ -5,11 +5,11 @@
"systems": "systems"
},
"locked": {
- "lastModified": 1694529238,
- "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
+ "lastModified": 1705309234,
+ "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
- "rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
+ "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
@@ -20,11 +20,11 @@
},
"nixpkgs": {
"locked": {
- "lastModified": 1701376520,
- "narHash": "sha256-U3iGiOZqgu7wvVzgfoQzGGFMqNsDj/q/6zPIjCy7ajg=",
+ "lastModified": 1705391267,
+ "narHash": "sha256-gGVm9QudiRtYTX8PN9cTTy7uuJcL4I2lRMoPx496kXk=",
"owner": "nixos",
"repo": "nixpkgs",
- "rev": "c74cc3c3db2ed5e68895953d75c397797d499133",
+ "rev": "41a9a7f170c740acb24f3390323877d11c69d5ee",
"type": "github"
},
"original": {
@@ -50,11 +50,11 @@
]
},
"locked": {
- "lastModified": 1701310566,
- "narHash": "sha256-CL9J3xUR2Ejni4LysrEGX0IdO+Y4BXCiH/By0lmF3eQ=",
+ "lastModified": 1705371439,
+ "narHash": "sha256-P1kulUXpYWkcrjiX3sV4j8ACJZh9XXSaaD+jDLBDLKo=",
"owner": "oxalica",
"repo": "rust-overlay",
- "rev": "6d3c6e185198b8bf7ad639f22404a75aa9a09bff",
+ "rev": "b21f3c0d5bf0f0179f5f0140e8e0cd099618bd04",
"type": "github"
},
"original": {
flake.nixdiffbeforeafterboth--- a/flake.nix
+++ b/flake.nix
@@ -25,14 +25,14 @@
lib = pkgs.lib;
rust =
(pkgs.rustChannelOf {
- date = "2023-10-28";
+ date = "2024-01-10";
channel = "nightly";
})
.default
.override {
extensions = ["rust-src" "miri" "rust-analyzer" "clippy"];
};
- in rec {
+ in {
packages = rec {
go-jsonnet = pkgs.callPackage ./nix/go-jsonnet.nix {};
sjsonnet = pkgs.callPackage ./nix/sjsonnet.nix {};
tests/suite/std_param_names.jsonnetdiffbeforeafterboth--- a/tests/suite/std_param_names.jsonnet
+++ b/tests/suite/std_param_names.jsonnet
@@ -103,6 +103,7 @@
asin: ['x'],
acos: ['x'],
atan: ['x'],
+ atan2: ['y', 'x'],
type: ['x'],
filter: ['func', 'arr'],
objectHasEx: ['obj', 'fname', 'hidden'],