difftreelog
style fix formatting
in: master
31 files changed
crates/jrsonnet-evaluator/src/analyze.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/analyze.rs
+++ b/crates/jrsonnet-evaluator/src/analyze.rs
@@ -21,10 +21,10 @@
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_interner::IStr;
use jrsonnet_ir::{
- function::FunctionSignature, ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BinaryOpType,
- BindSpec, CompSpec, Destruct, Expr, ExprParams, FieldName, ForSpecData, IfElse, IfSpecData,
- ImportKind, LiteralType, NumValue, ObjBody, ObjComp, ObjMembers, Slice, SliceDesc, Span,
- Spanned, UnaryOpType, Visibility,
+ ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BinaryOpType, BindSpec, CompSpec, Destruct, Expr,
+ ExprParams, FieldName, ForSpecData, IfElse, IfSpecData, ImportKind, LiteralType, NumValue,
+ ObjBody, ObjComp, ObjMembers, Slice, SliceDesc, Span, Spanned, UnaryOpType, Visibility,
+ function::FunctionSignature,
};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -5,9 +5,9 @@
rc::Rc,
};
-use jrsonnet_gcmodule::{cc_dyn, Cc};
+use jrsonnet_gcmodule::{Cc, cc_dyn};
-use crate::{analyze::LExpr, function::NativeFn, typed::IntoUntyped, Context, Result, Thunk, Val};
+use crate::{Context, Result, Thunk, Val, analyze::LExpr, function::NativeFn, typed::IntoUntyped};
mod spec;
pub use spec::{ArrayLike, *};
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -11,13 +11,13 @@
use super::ArrValue;
use crate::{
+ Context, Error, ObjValue, Result, Thunk, Val,
analyze::LExpr,
error::ErrorKind::InfiniteRecursionDetected,
evaluate::evaluate,
function::NativeFn,
typed::{IntoUntyped, Typed},
val::ThunkValue,
- Context, Error, ObjValue, Result, Thunk, Val,
};
pub trait ArrayLike: Any + Trace + Debug {
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -4,7 +4,7 @@
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
-use crate::{analyze::LocalId, error, error::ErrorKind::*, Pending, Result, SupThis, Thunk, Val};
+use crate::{Pending, Result, SupThis, Thunk, Val, analyze::LocalId, error, error::ErrorKind::*};
#[derive(Debug, Trace, Clone, Educe)]
#[educe(PartialEq)]
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -9,10 +9,10 @@
use thiserror::Error;
use crate::{
+ ObjValue, ResolvePathOwned,
function::{CallLocation, FunctionSignature, ParamName},
stdlib::format::FormatError,
typed::TypeLocError,
- ObjValue, ResolvePathOwned,
};
#[derive(Debug, Clone, Acyclic)]
@@ -286,11 +286,11 @@
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- writeln!(f, "{}", self.0 .0)?;
- for el in &self.0 .1 .0 {
+ writeln!(f, "{}", self.0.0)?;
+ for el in &self.0.1.0 {
write!(f, "\t{}", el.desc)?;
if let Some(loc) = &el.location {
- write!(f, "at {}", loc.0 .0 .0)?;
+ write!(f, "at {}", loc.0.0.0)?;
loc.0.map_source_locations(&[loc.1, loc.2]);
}
writeln!(f)?;
crates/jrsonnet-evaluator/src/evaluate/compspec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/compspec.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/compspec.rs
@@ -7,12 +7,12 @@
evaluate_field_member_static, evaluate_field_member_unbound,
};
use crate::{
+ Context, ContextBuilder, ObjValue, ObjValueBuilder, Pending, Result, Thunk, Val,
analyze::{LArrComp, LBind, LCompSpec, LDestruct, LExpr, LFieldMember, LObjComp, LocalId},
arr::ArrValue,
bail,
error::ErrorKind::*,
evaluate::evaluate,
- Context, ContextBuilder, ObjValue, ObjValueBuilder, Pending, Result, Thunk, Val,
};
trait CompCollector {
crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -3,10 +3,10 @@
use jrsonnet_gcmodule::Trace;
use crate::{
+ Context, ContextBuilder, Pending, Result, SupThis, Thunk, Unbound, Val,
analyze::{LBind, LDestruct, LDestructField, LDestructRest, LExpr, LocalId},
bail,
evaluate::evaluate,
- Context, ContextBuilder, Pending, Result, SupThis, Thunk, Unbound, Val,
};
#[allow(dead_code, reason = "not dead in exp-destruct")]
@@ -97,7 +97,7 @@
use jrsonnet_interner::IStr;
use rustc_hash::FxHashSet;
- use crate::{bail, ObjValueBuilder};
+ use crate::{ObjValueBuilder, bail};
let captured_fields: FxHashSet<IStr> = fields.iter().map(|f| f.name.clone()).collect();
let field_names: Vec<(IStr, bool)> = fields
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -11,19 +11,20 @@
operator::evaluate_binary_op_special,
};
use crate::{
+ Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Result, ResultExt as _, SupThis,
+ Unbound, Val,
analyze::{
LArgsDesc, LAssertStmt, LExpr, LFieldMember, LFieldName, LFunction, LIndexPart, LObjBody,
LObjMembers,
},
bail,
- error::{suggest_object_fields, ErrorKind::*},
+ error::{ErrorKind::*, suggest_object_fields},
evaluate::operator::evaluate_unary_op,
- function::{prepared::PreparedFuncVal, CallLocation, FuncDesc, FuncVal},
+ function::{CallLocation, FuncDesc, FuncVal, prepared::PreparedFuncVal},
in_frame, runtime_error,
typed::FromUntyped as _,
val::{CachedUnbound, Thunk},
- with_state, Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Result, ResultExt as _,
- SupThis, Unbound, Val,
+ with_state,
};
pub mod compspec;
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -3,6 +3,7 @@
use jrsonnet_ir::{BinaryOpType, UnaryOpType};
use crate::{
+ Context, Result, Val,
analyze::LExpr,
arr::ArrValue,
bail, error,
@@ -10,8 +11,7 @@
evaluate::evaluate,
stdlib::std_format,
typed::IntoUntyped as _,
- val::{equals, StrValue},
- Context, Result, Val,
+ val::{StrValue, equals},
};
pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -8,13 +8,13 @@
use self::{
builtin::Builtin,
- prepared::{parse_prepared_builtin_call, PreparedCall},
+ prepared::{PreparedCall, parse_prepared_builtin_call},
};
use crate::{
+ Context, ContextBuilder, Result, Thunk, Val,
analyze::{LDestruct, LExpr, LFunction},
evaluate::{destructure::destruct, ensure_sufficient_stack, evaluate, evaluate_trivial},
function::builtin::BuiltinFunc,
- Context, ContextBuilder, Result, Thunk, Val,
};
pub mod builtin;
@@ -210,8 +210,7 @@
return false;
}
#[allow(irrefutable_let_patterns, reason = "refutable with exp-destruct")]
- let LDestruct::Full(id) = ¶m.destruct
- else {
+ let LDestruct::Full(id) = ¶m.destruct else {
return false;
};
matches!(&*desc.func.body, LExpr::Local(v) if v == id)
crates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -1,9 +1,9 @@
use std::rc::Rc;
use crate::{
+ Context, ContextBuilder, Result, Thunk,
analyze::LFunction,
evaluate::{destructure::destruct, evaluate},
- Context, ContextBuilder, Result, Thunk,
};
/// Creates Context with all argument default values applied
crates/jrsonnet-evaluator/src/function/prepared.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/prepared.rs
+++ b/crates/jrsonnet-evaluator/src/function/prepared.rs
@@ -5,10 +5,7 @@
use rustc_hash::FxHashSet;
use super::{CallLocation, FuncVal};
-use crate::{
- Result, Thunk, Val, bail,
- error::ErrorKind::*,
-};
+use crate::{Result, Thunk, Val, bail, error::ErrorKind::*};
#[derive(Debug, Trace, Clone)]
pub struct PreparedFuncVal {
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -3,16 +3,16 @@
use jrsonnet_interner::{IBytes, IStr};
use jrsonnet_ir::NumValue;
use serde::{
+ Deserialize, Serialize, Serializer,
de::{self, Visitor},
ser::{
Error, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
SerializeTupleStruct, SerializeTupleVariant,
},
- Deserialize, Serialize, Serializer,
};
use crate::{
- in_description_frame, runtime_error, Error as JrError, ObjValue, ObjValueBuilder, Result, Val,
+ Error as JrError, ObjValue, ObjValueBuilder, Result, Val, in_description_frame, runtime_error,
};
impl<'de> Deserialize<'de> for Val {
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8pub mod async_import;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13pub mod function;14pub mod gc;15mod import;16mod integrations;17pub mod manifest;18mod obj;19pub mod stack;20pub mod stdlib;21pub mod tla;22pub mod trace;23pub mod typed;24pub mod val;2526use std::{27 any::Any,28 cell::{RefCell, RefMut},29 clone::Clone,30 collections::hash_map::Entry,31 fmt::{self, Debug},32 marker::PhantomData,33 rc::Rc,34};3536pub use ctx::*;37pub use dynamic::*;38pub use error::{Error, ErrorKind::*, Result, ResultExt};39pub use evaluate::ensure_sufficient_stack;40use function::CallLocation;41pub use import::*;42use jrsonnet_gcmodule::{cc_dyn, Cc, Trace};43pub use jrsonnet_interner::{IBytes, IStr};44use jrsonnet_ir::Expr;45pub use jrsonnet_ir::{NumValue, Source, SourcePath, Span};46#[doc(hidden)]47pub use jrsonnet_macros;4849#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]50compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");5152pub use error::SyntaxError;53pub use obj::*;54pub use rustc_hash;55use rustc_hash::FxHashMap;56use stack::check_depth;57pub use tla::apply_tla;58pub use val::{Thunk, Val};5960pub mod analyze;61use crate::gc::WithCapacityExt as _;6263#[allow(clippy::needless_return)]64pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {65 #[cfg(feature = "peg-parser")]66 {67 use std::sync::LazyLock;68 static USE_LEGACY_PARSER: LazyLock<bool> =69 LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());7071 if *USE_LEGACY_PARSER {72 return parse_peg(code, source);73 }74 }75 #[cfg(feature = "ir-parser")]76 {77 return parse_ir(code, source);78 }79 #[cfg(feature = "peg-parser")]80 {81 return parse_peg(code, source);82 }83}8485#[cfg(feature = "ir-parser")]86fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {87 jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {88 SyntaxError {89 message: e.message,90 location: e.location,91 }92 })93}9495#[cfg(feature = "peg-parser")]96fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {97 jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {98 let message = e99 .expected100 .tokens()101 .find(|t| t.starts_with("!!!"))102 .map_or_else(103 || {104 format!(105 "expected {}, got {:?}",106 e.expected,107 code.chars()108 .nth(e.location.0)109 .map_or_else(|| "EOF".into(), |c: char| c.to_string())110 )111 },112 |v| v[3..].into(),113 );114 SyntaxError {115 message,116 location: e.location,117 }118 })119}120121cc_dyn!(122 #[derive(Clone)]123 CcUnbound<V>,124 Unbound<Bound = V>125);126127/// Thunk without bound `super`/`this`128/// object inheritance may be overriden multiple times, and will be fixed only on field read129pub trait Unbound: Trace {130 /// Type of value after object context is bound131 type Bound;132 /// Create value bound to specified object context133 fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;134}135136/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code137/// Standard jsonnet fields are always unbound138#[derive(Clone, Trace)]139pub enum MaybeUnbound {140 /// Value needs to be bound to `this`/`super`141 Unbound(CcUnbound<Val>),142 /// Value is object-independent143 Bound(Thunk<Val>),144}145146impl Debug for MaybeUnbound {147 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {148 write!(f, "MaybeUnbound")149 }150}151impl MaybeUnbound {152 /// Attach object context to value, if required153 pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {154 match self {155 Self::Unbound(v) => v.0.bind(sup_this),156 Self::Bound(v) => Ok(v.evaluate()?),157 }158 }159}160161cc_dyn!(CcContextInitializer, ContextInitializer);162163/// During import, this trait will be called to create initial context for file.164/// It may initialize global variables, stdlib for example.165pub trait ContextInitializer {166 /// For composability: extend builder. May panic if this initialization is not supported,167 /// and the context may only be created via `initialize`.168 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder);169 /// Allows upcasting from abstract to concrete context initializer.170 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.171 fn as_any(&self) -> &dyn Any;172}173impl<T> ContextInitializer for &T174where175 T: ContextInitializer,176{177 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {178 (*self).populate(for_file, builder);179 }180181 fn as_any(&self) -> &dyn Any {182 (*self).as_any()183 }184}185186/// Context initializer which adds nothing.187impl ContextInitializer for () {188 fn populate(&self, _for_file: Source, _builder: &mut InitialContextBuilder) {}189 fn as_any(&self) -> &dyn Any {190 self191 }192}193194impl<T> ContextInitializer for Option<T>195where196 T: ContextInitializer + 'static,197{198 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {199 if let Some(ctx) = self {200 ctx.populate(for_file, builder);201 }202 }203204 fn as_any(&self) -> &dyn Any {205 self206 }207}208209macro_rules! impl_context_initializer {210 ($($gen:ident)*) => {211 #[allow(non_snake_case)]212 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {213 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {214 let ($($gen,)*) = self;215 $($gen.populate(for_file.clone(), builder);)*216 }217 fn as_any(&self) -> &dyn Any {218 self219 }220 }221 };222 ($($cur:ident)* @ $c:ident $($rest:ident)*) => {223 impl_context_initializer!($($cur)*);224 impl_context_initializer!($($cur)* $c @ $($rest)*);225 };226 ($($cur:ident)* @) => {227 impl_context_initializer!($($cur)*);228 }229}230impl_context_initializer! {231 A @ B C D E F G232}233234#[derive(Trace)]235struct FileData {236 string: Option<IStr>,237 bytes: Option<IBytes>,238 parsed: Option<Rc<Expr>>,239 evaluated: Option<Val>,240241 evaluating: bool,242}243impl FileData {244 fn new_string(data: IStr) -> Self {245 Self {246 string: Some(data),247 bytes: None,248 parsed: None,249 evaluated: None,250 evaluating: false,251 }252 }253 fn new_bytes(data: IBytes) -> Self {254 Self {255 string: None,256 bytes: Some(data),257 parsed: None,258 evaluated: None,259 evaluating: false,260 }261 }262 pub(crate) fn get_string(&mut self) -> Option<IStr> {263 if self.string.is_none() {264 self.string = Some(265 self.bytes266 .as_ref()267 .expect("either string or bytes should be set")268 .clone()269 .cast_str()?,270 );271 }272 Some(self.string.clone().expect("just set"))273 }274}275276#[derive(Trace)]277pub struct EvaluationStateInternals {278 /// Internal state279 file_cache: RefCell<FxHashMap<SourcePath, FileData>>,280 /// Context initializer, which will be used for imports and everything281 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`282 context_initializer: CcContextInitializer,283 /// Used to resolve file locations/contents284 import_resolver: Rc<dyn ImportResolver>,285}286287/// Maintains stack trace and import resolution288#[derive(Clone, Trace)]289pub struct State(Cc<EvaluationStateInternals>);290291thread_local! {292 pub static DEFAULT_STATE: State = State::builder().build();293 pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};294}295pub struct StateEnterGuard(PhantomData<()>);296impl Drop for StateEnterGuard {297 fn drop(&mut self) {298 STATE.with_borrow_mut(|v| *v = None);299 }300}301302pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {303 if let Some(state) = STATE.with_borrow(Clone::clone) {304 v(state)305 } else {306 let s = DEFAULT_STATE.with(Clone::clone);307 v(s)308 }309}310311impl State {312 pub fn enter(&self) -> StateEnterGuard {313 self.try_enter().expect("entered state already exists")314 }315 pub fn try_enter(&self) -> Option<StateEnterGuard> {316 STATE.with_borrow_mut(|v| {317 if v.is_none() {318 *v = Some(self.clone());319 Some(StateEnterGuard(PhantomData))320 } else {321 None322 }323 })324 }325 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise326 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {327 let mut file_cache = self.file_cache();328 let mut file = file_cache.entry(path.clone());329330 let file = match file {331 Entry::Occupied(ref mut d) => d.get_mut(),332 Entry::Vacant(v) => {333 let data = self.import_resolver().load_file_contents(&path)?;334 v.insert(FileData::new_string(335 std::str::from_utf8(&data)336 .map_err(|_| ImportBadFileUtf8(path.clone()))?337 .into(),338 ))339 }340 };341 Ok(file342 .get_string()343 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?)344 }345 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise346 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {347 let mut file_cache = self.file_cache();348 let mut file = file_cache.entry(path.clone());349350 let file = match file {351 Entry::Occupied(ref mut d) => d.get_mut(),352 Entry::Vacant(v) => {353 let data = self.import_resolver().load_file_contents(&path)?;354 v.insert(FileData::new_bytes(data.as_slice().into()))355 }356 };357 if let Some(str) = &file.bytes {358 return Ok(str.clone());359 }360 if file.bytes.is_none() {361 file.bytes = Some(362 file.string363 .as_ref()364 .expect("either string or bytes should be set")365 .clone()366 .cast_bytes(),367 );368 }369 Ok(file.bytes.as_ref().expect("just set").clone())370 }371 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise372 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {373 let mut file_cache = self.file_cache();374 let mut file = file_cache.entry(path.clone());375376 let file = match file {377 Entry::Occupied(ref mut d) => d.get_mut(),378 Entry::Vacant(v) => {379 let data = self.import_resolver().load_file_contents(&path)?;380 v.insert(FileData::new_string(381 std::str::from_utf8(&data)382 .map_err(|_| ImportBadFileUtf8(path.clone()))?383 .into(),384 ))385 }386 };387 if let Some(val) = &file.evaluated {388 return Ok(val.clone());389 }390 let code = file391 .get_string()392 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?;393 let file_name = Source::new(path.clone(), code.clone());394 if file.parsed.is_none() {395 file.parsed = Some(396 parse_jsonnet(&code, file_name.clone())397 .map(Rc::new)398 .map_err(|e| ImportSyntaxError {399 path: file_name.clone(),400 error: Box::new(e),401 })?,402 );403 }404 let parsed = file.parsed.as_ref().expect("just set").clone();405 if file.evaluating {406 bail!(InfiniteRecursionDetected)407 }408 file.evaluating = true;409 // Dropping file cache guard here, as evaluation may use this map too410 drop(file_cache);411 let (ctx, externals) = self.create_default_context(file_name.clone()).build();412 let report = analyze::analyze_root(&parsed, externals);413 if report.errored {414 return Err(StaticAnalysisError(report.diagnostics_list).into());415 }416 let res = evaluate::evaluate(ctx.build(), &report.lir);417418 let mut file_cache = self.file_cache();419 let mut file = file_cache.entry(path);420421 let Entry::Occupied(file) = &mut file else {422 unreachable!("this file was just here")423 };424 let file = file.get_mut();425 file.evaluating = false;426 match res {427 Ok(v) => {428 file.evaluated = Some(v.clone());429 Ok(v)430 }431 Err(e) => Err(e),432 }433 }434435 /// Has same semantics as `import 'path'` called from `from` file436 pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {437 let resolved = self.resolve_from(from, &path)?;438 self.import_resolved(resolved)439 }440 pub fn import(&self, path: impl AsPathLike) -> Result<Val> {441 let resolved = self.resolve_from_default(&path)?;442 self.import_resolved(resolved)443 }444445 /// Creates context with all passed global variables446 pub fn create_default_context(&self, source: Source) -> InitialContextBuilder {447 self.create_default_context_with(source, &())448 }449450 /// Creates context with all passed global variables, calling custom modifier451 pub fn create_default_context_with(452 &self,453 source: Source,454 context_initializer: &dyn ContextInitializer,455 ) -> InitialContextBuilder {456 let default_initializer = self.context_initializer();457 let mut builder = InitialContextBuilder::new();458 default_initializer.populate(source.clone(), &mut builder);459 context_initializer.populate(source, &mut builder);460461 builder462 }463}464465/// Internals466impl State {467 fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {468 self.0.file_cache.borrow_mut()469 }470}471/// Executes code creating a new stack frame, to be replaced with try{}472pub fn in_frame<T>(473 e: CallLocation<'_>,474 frame_desc: impl FnOnce() -> String,475 f: impl FnOnce() -> Result<T>,476) -> Result<T> {477 let _guard = check_depth()?;478479 f().with_description_src(e, frame_desc)480}481482/// Executes code creating a new stack frame, to be replaced with try{}483pub fn in_description_frame<T>(484 frame_desc: impl FnOnce() -> String,485 f: impl FnOnce() -> Result<T>,486) -> Result<T> {487 let _guard = check_depth()?;488489 f().with_description(frame_desc)490}491492#[derive(Trace)]493pub struct InitialUnderscore(pub Thunk<Val>);494impl ContextInitializer for InitialUnderscore {495 fn populate(&self, _for_file: Source, builder: &mut InitialContextBuilder) {496 builder.bind("_", self.0.clone());497 }498499 fn as_any(&self) -> &dyn Any {500 self501 }502}503504/// Raw methods evaluate passed values but don't perform TLA execution505impl State {506 /// Parses and evaluates the given snippet507 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {508 self.evaluate_snippet_with(name, code, &())509 }510 /// Parses and evaluates the given snippet with custom context modifier511 pub fn evaluate_snippet_with(512 &self,513 name: impl Into<IStr>,514 code: impl Into<IStr>,515 context_initializer: &dyn ContextInitializer,516 ) -> Result<Val> {517 let code = code.into();518 let source = Source::new_virtual(name.into(), code.clone());519 let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {520 path: source.clone(),521 error: Box::new(e),522 })?;523 let (ctx, externals) = self524 .create_default_context_with(source.clone(), context_initializer)525 .build();526 let report = analyze::analyze_root(&parsed, externals);527 if report.errored {528 return Err(StaticAnalysisError(report.diagnostics_list).into());529 }530 evaluate::evaluate(ctx.build(), &report.lir)531 }532}533534/// Settings utilities535impl State {536 // Only panics in case of [`ImportResolver`] contract violation537 #[allow(clippy::missing_panics_doc)]538 pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {539 self.import_resolver().resolve_from(from, path)540 }541 #[allow(clippy::missing_panics_doc)]542 pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {543 self.import_resolver().resolve_from_default(path)544 }545 pub fn import_resolver(&self) -> &dyn ImportResolver {546 &*self.0.import_resolver547 }548 pub fn context_initializer(&self) -> &dyn ContextInitializer {549 &*self.0.context_initializer.0550 }551}552553impl State {554 pub fn builder() -> StateBuilder {555 StateBuilder::default()556 }557}558559impl Default for State {560 fn default() -> Self {561 Self::builder().build()562 }563}564565#[derive(Default)]566pub struct StateBuilder {567 import_resolver: Option<Rc<dyn ImportResolver>>,568 context_initializer: Option<CcContextInitializer>,569}570impl StateBuilder {571 pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {572 let _ = self.import_resolver.insert(Rc::new(import_resolver));573 self574 }575 pub fn context_initializer(576 &mut self,577 context_initializer: impl ContextInitializer + Trace,578 ) -> &mut Self {579 let _ = self580 .context_initializer581 .insert(CcContextInitializer::new(context_initializer));582 self583 }584 pub fn build(mut self) -> State {585 State(Cc::new(EvaluationStateInternals {586 file_cache: RefCell::new(FxHashMap::new()),587 context_initializer: self588 .context_initializer589 .take()590 .unwrap_or_else(|| CcContextInitializer::new(())),591 import_resolver: self592 .import_resolver593 .take()594 .unwrap_or_else(|| Rc::new(DummyImportResolver)),595 }))596 }597}1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8pub mod async_import;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13pub mod function;14pub mod gc;15mod import;16mod integrations;17pub mod manifest;18mod obj;19pub mod stack;20pub mod stdlib;21pub mod tla;22pub mod trace;23pub mod typed;24pub mod val;2526use std::{27 any::Any,28 cell::{RefCell, RefMut},29 clone::Clone,30 collections::hash_map::Entry,31 fmt::{self, Debug},32 marker::PhantomData,33 rc::Rc,34};3536pub use ctx::*;37pub use dynamic::*;38pub use error::{Error, ErrorKind::*, Result, ResultExt};39pub use evaluate::ensure_sufficient_stack;40use function::CallLocation;41pub use import::*;42use jrsonnet_gcmodule::{Cc, Trace, cc_dyn};43pub use jrsonnet_interner::{IBytes, IStr};44use jrsonnet_ir::Expr;45pub use jrsonnet_ir::{NumValue, Source, SourcePath, Span};46#[doc(hidden)]47pub use jrsonnet_macros;4849#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]50compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");5152pub use error::SyntaxError;53pub use obj::*;54pub use rustc_hash;55use rustc_hash::FxHashMap;56use stack::check_depth;57pub use tla::apply_tla;58pub use val::{Thunk, Val};5960pub mod analyze;61use crate::gc::WithCapacityExt as _;6263#[allow(clippy::needless_return)]64pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {65 #[cfg(feature = "peg-parser")]66 {67 use std::sync::LazyLock;68 static USE_LEGACY_PARSER: LazyLock<bool> =69 LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());7071 if *USE_LEGACY_PARSER {72 return parse_peg(code, source);73 }74 }75 #[cfg(feature = "ir-parser")]76 {77 return parse_ir(code, source);78 }79 #[cfg(feature = "peg-parser")]80 {81 return parse_peg(code, source);82 }83}8485#[cfg(feature = "ir-parser")]86fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {87 jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {88 SyntaxError {89 message: e.message,90 location: e.location,91 }92 })93}9495#[cfg(feature = "peg-parser")]96fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {97 jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {98 let message = e99 .expected100 .tokens()101 .find(|t| t.starts_with("!!!"))102 .map_or_else(103 || {104 format!(105 "expected {}, got {:?}",106 e.expected,107 code.chars()108 .nth(e.location.0)109 .map_or_else(|| "EOF".into(), |c: char| c.to_string())110 )111 },112 |v| v[3..].into(),113 );114 SyntaxError {115 message,116 location: e.location,117 }118 })119}120121cc_dyn!(122 #[derive(Clone)]123 CcUnbound<V>,124 Unbound<Bound = V>125);126127/// Thunk without bound `super`/`this`128/// object inheritance may be overriden multiple times, and will be fixed only on field read129pub trait Unbound: Trace {130 /// Type of value after object context is bound131 type Bound;132 /// Create value bound to specified object context133 fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;134}135136/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code137/// Standard jsonnet fields are always unbound138#[derive(Clone, Trace)]139pub enum MaybeUnbound {140 /// Value needs to be bound to `this`/`super`141 Unbound(CcUnbound<Val>),142 /// Value is object-independent143 Bound(Thunk<Val>),144}145146impl Debug for MaybeUnbound {147 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {148 write!(f, "MaybeUnbound")149 }150}151impl MaybeUnbound {152 /// Attach object context to value, if required153 pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {154 match self {155 Self::Unbound(v) => v.0.bind(sup_this),156 Self::Bound(v) => Ok(v.evaluate()?),157 }158 }159}160161cc_dyn!(CcContextInitializer, ContextInitializer);162163/// During import, this trait will be called to create initial context for file.164/// It may initialize global variables, stdlib for example.165pub trait ContextInitializer {166 /// For composability: extend builder. May panic if this initialization is not supported,167 /// and the context may only be created via `initialize`.168 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder);169 /// Allows upcasting from abstract to concrete context initializer.170 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.171 fn as_any(&self) -> &dyn Any;172}173impl<T> ContextInitializer for &T174where175 T: ContextInitializer,176{177 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {178 (*self).populate(for_file, builder);179 }180181 fn as_any(&self) -> &dyn Any {182 (*self).as_any()183 }184}185186/// Context initializer which adds nothing.187impl ContextInitializer for () {188 fn populate(&self, _for_file: Source, _builder: &mut InitialContextBuilder) {}189 fn as_any(&self) -> &dyn Any {190 self191 }192}193194impl<T> ContextInitializer for Option<T>195where196 T: ContextInitializer + 'static,197{198 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {199 if let Some(ctx) = self {200 ctx.populate(for_file, builder);201 }202 }203204 fn as_any(&self) -> &dyn Any {205 self206 }207}208209macro_rules! impl_context_initializer {210 ($($gen:ident)*) => {211 #[allow(non_snake_case)]212 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {213 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {214 let ($($gen,)*) = self;215 $($gen.populate(for_file.clone(), builder);)*216 }217 fn as_any(&self) -> &dyn Any {218 self219 }220 }221 };222 ($($cur:ident)* @ $c:ident $($rest:ident)*) => {223 impl_context_initializer!($($cur)*);224 impl_context_initializer!($($cur)* $c @ $($rest)*);225 };226 ($($cur:ident)* @) => {227 impl_context_initializer!($($cur)*);228 }229}230impl_context_initializer! {231 A @ B C D E F G232}233234#[derive(Trace)]235struct FileData {236 string: Option<IStr>,237 bytes: Option<IBytes>,238 parsed: Option<Rc<Expr>>,239 evaluated: Option<Val>,240241 evaluating: bool,242}243impl FileData {244 fn new_string(data: IStr) -> Self {245 Self {246 string: Some(data),247 bytes: None,248 parsed: None,249 evaluated: None,250 evaluating: false,251 }252 }253 fn new_bytes(data: IBytes) -> Self {254 Self {255 string: None,256 bytes: Some(data),257 parsed: None,258 evaluated: None,259 evaluating: false,260 }261 }262 pub(crate) fn get_string(&mut self) -> Option<IStr> {263 if self.string.is_none() {264 self.string = Some(265 self.bytes266 .as_ref()267 .expect("either string or bytes should be set")268 .clone()269 .cast_str()?,270 );271 }272 Some(self.string.clone().expect("just set"))273 }274}275276#[derive(Trace)]277pub struct EvaluationStateInternals {278 /// Internal state279 file_cache: RefCell<FxHashMap<SourcePath, FileData>>,280 /// Context initializer, which will be used for imports and everything281 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`282 context_initializer: CcContextInitializer,283 /// Used to resolve file locations/contents284 import_resolver: Rc<dyn ImportResolver>,285}286287/// Maintains stack trace and import resolution288#[derive(Clone, Trace)]289pub struct State(Cc<EvaluationStateInternals>);290291thread_local! {292 pub static DEFAULT_STATE: State = State::builder().build();293 pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};294}295pub struct StateEnterGuard(PhantomData<()>);296impl Drop for StateEnterGuard {297 fn drop(&mut self) {298 STATE.with_borrow_mut(|v| *v = None);299 }300}301302pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {303 if let Some(state) = STATE.with_borrow(Clone::clone) {304 v(state)305 } else {306 let s = DEFAULT_STATE.with(Clone::clone);307 v(s)308 }309}310311impl State {312 pub fn enter(&self) -> StateEnterGuard {313 self.try_enter().expect("entered state already exists")314 }315 pub fn try_enter(&self) -> Option<StateEnterGuard> {316 STATE.with_borrow_mut(|v| {317 if v.is_none() {318 *v = Some(self.clone());319 Some(StateEnterGuard(PhantomData))320 } else {321 None322 }323 })324 }325 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise326 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {327 let mut file_cache = self.file_cache();328 let mut file = file_cache.entry(path.clone());329330 let file = match file {331 Entry::Occupied(ref mut d) => d.get_mut(),332 Entry::Vacant(v) => {333 let data = self.import_resolver().load_file_contents(&path)?;334 v.insert(FileData::new_string(335 std::str::from_utf8(&data)336 .map_err(|_| ImportBadFileUtf8(path.clone()))?337 .into(),338 ))339 }340 };341 Ok(file342 .get_string()343 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?)344 }345 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise346 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {347 let mut file_cache = self.file_cache();348 let mut file = file_cache.entry(path.clone());349350 let file = match file {351 Entry::Occupied(ref mut d) => d.get_mut(),352 Entry::Vacant(v) => {353 let data = self.import_resolver().load_file_contents(&path)?;354 v.insert(FileData::new_bytes(data.as_slice().into()))355 }356 };357 if let Some(str) = &file.bytes {358 return Ok(str.clone());359 }360 if file.bytes.is_none() {361 file.bytes = Some(362 file.string363 .as_ref()364 .expect("either string or bytes should be set")365 .clone()366 .cast_bytes(),367 );368 }369 Ok(file.bytes.as_ref().expect("just set").clone())370 }371 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise372 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {373 let mut file_cache = self.file_cache();374 let mut file = file_cache.entry(path.clone());375376 let file = match file {377 Entry::Occupied(ref mut d) => d.get_mut(),378 Entry::Vacant(v) => {379 let data = self.import_resolver().load_file_contents(&path)?;380 v.insert(FileData::new_string(381 std::str::from_utf8(&data)382 .map_err(|_| ImportBadFileUtf8(path.clone()))?383 .into(),384 ))385 }386 };387 if let Some(val) = &file.evaluated {388 return Ok(val.clone());389 }390 let code = file391 .get_string()392 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?;393 let file_name = Source::new(path.clone(), code.clone());394 if file.parsed.is_none() {395 file.parsed = Some(396 parse_jsonnet(&code, file_name.clone())397 .map(Rc::new)398 .map_err(|e| ImportSyntaxError {399 path: file_name.clone(),400 error: Box::new(e),401 })?,402 );403 }404 let parsed = file.parsed.as_ref().expect("just set").clone();405 if file.evaluating {406 bail!(InfiniteRecursionDetected)407 }408 file.evaluating = true;409 // Dropping file cache guard here, as evaluation may use this map too410 drop(file_cache);411 let (ctx, externals) = self.create_default_context(file_name.clone()).build();412 let report = analyze::analyze_root(&parsed, externals);413 if report.errored {414 return Err(StaticAnalysisError(report.diagnostics_list).into());415 }416 let res = evaluate::evaluate(ctx.build(), &report.lir);417418 let mut file_cache = self.file_cache();419 let mut file = file_cache.entry(path);420421 let Entry::Occupied(file) = &mut file else {422 unreachable!("this file was just here")423 };424 let file = file.get_mut();425 file.evaluating = false;426 match res {427 Ok(v) => {428 file.evaluated = Some(v.clone());429 Ok(v)430 }431 Err(e) => Err(e),432 }433 }434435 /// Has same semantics as `import 'path'` called from `from` file436 pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {437 let resolved = self.resolve_from(from, &path)?;438 self.import_resolved(resolved)439 }440 pub fn import(&self, path: impl AsPathLike) -> Result<Val> {441 let resolved = self.resolve_from_default(&path)?;442 self.import_resolved(resolved)443 }444445 /// Creates context with all passed global variables446 pub fn create_default_context(&self, source: Source) -> InitialContextBuilder {447 self.create_default_context_with(source, &())448 }449450 /// Creates context with all passed global variables, calling custom modifier451 pub fn create_default_context_with(452 &self,453 source: Source,454 context_initializer: &dyn ContextInitializer,455 ) -> InitialContextBuilder {456 let default_initializer = self.context_initializer();457 let mut builder = InitialContextBuilder::new();458 default_initializer.populate(source.clone(), &mut builder);459 context_initializer.populate(source, &mut builder);460461 builder462 }463}464465/// Internals466impl State {467 fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {468 self.0.file_cache.borrow_mut()469 }470}471/// Executes code creating a new stack frame, to be replaced with try{}472pub fn in_frame<T>(473 e: CallLocation<'_>,474 frame_desc: impl FnOnce() -> String,475 f: impl FnOnce() -> Result<T>,476) -> Result<T> {477 let _guard = check_depth()?;478479 f().with_description_src(e, frame_desc)480}481482/// Executes code creating a new stack frame, to be replaced with try{}483pub fn in_description_frame<T>(484 frame_desc: impl FnOnce() -> String,485 f: impl FnOnce() -> Result<T>,486) -> Result<T> {487 let _guard = check_depth()?;488489 f().with_description(frame_desc)490}491492#[derive(Trace)]493pub struct InitialUnderscore(pub Thunk<Val>);494impl ContextInitializer for InitialUnderscore {495 fn populate(&self, _for_file: Source, builder: &mut InitialContextBuilder) {496 builder.bind("_", self.0.clone());497 }498499 fn as_any(&self) -> &dyn Any {500 self501 }502}503504/// Raw methods evaluate passed values but don't perform TLA execution505impl State {506 /// Parses and evaluates the given snippet507 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {508 self.evaluate_snippet_with(name, code, &())509 }510 /// Parses and evaluates the given snippet with custom context modifier511 pub fn evaluate_snippet_with(512 &self,513 name: impl Into<IStr>,514 code: impl Into<IStr>,515 context_initializer: &dyn ContextInitializer,516 ) -> Result<Val> {517 let code = code.into();518 let source = Source::new_virtual(name.into(), code.clone());519 let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {520 path: source.clone(),521 error: Box::new(e),522 })?;523 let (ctx, externals) = self524 .create_default_context_with(source.clone(), context_initializer)525 .build();526 let report = analyze::analyze_root(&parsed, externals);527 if report.errored {528 return Err(StaticAnalysisError(report.diagnostics_list).into());529 }530 evaluate::evaluate(ctx.build(), &report.lir)531 }532}533534/// Settings utilities535impl State {536 // Only panics in case of [`ImportResolver`] contract violation537 #[allow(clippy::missing_panics_doc)]538 pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {539 self.import_resolver().resolve_from(from, path)540 }541 #[allow(clippy::missing_panics_doc)]542 pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {543 self.import_resolver().resolve_from_default(path)544 }545 pub fn import_resolver(&self) -> &dyn ImportResolver {546 &*self.0.import_resolver547 }548 pub fn context_initializer(&self) -> &dyn ContextInitializer {549 &*self.0.context_initializer.0550 }551}552553impl State {554 pub fn builder() -> StateBuilder {555 StateBuilder::default()556 }557}558559impl Default for State {560 fn default() -> Self {561 Self::builder().build()562 }563}564565#[derive(Default)]566pub struct StateBuilder {567 import_resolver: Option<Rc<dyn ImportResolver>>,568 context_initializer: Option<CcContextInitializer>,569}570impl StateBuilder {571 pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {572 let _ = self.import_resolver.insert(Rc::new(import_resolver));573 self574 }575 pub fn context_initializer(576 &mut self,577 context_initializer: impl ContextInitializer + Trace,578 ) -> &mut Self {579 let _ = self580 .context_initializer581 .insert(CcContextInitializer::new(context_initializer));582 self583 }584 pub fn build(mut self) -> State {585 State(Cc::new(EvaluationStateInternals {586 file_cache: RefCell::new(FxHashMap::new()),587 context_initializer: self588 .context_initializer589 .take()590 .unwrap_or_else(|| CcContextInitializer::new(())),591 import_resolver: self592 .import_resolver593 .take()594 .unwrap_or_else(|| Rc::new(DummyImportResolver)),595 }))596 }597}crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -1,8 +1,7 @@
use std::{borrow::Cow, fmt::Write, hint::black_box, ptr};
use crate::{
- bail, evaluate::ensure_sufficient_stack, in_description_frame, Error,
- Result, ResultExt, Val,
+ Error, Result, ResultExt, Val, bail, evaluate::ensure_sufficient_stack, in_description_frame,
};
pub trait ManifestFormat {
crates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj/mod.rs
+++ b/crates/jrsonnet-evaluator/src/obj/mod.rs
@@ -11,8 +11,8 @@
};
use educe::Educe;
-use im_rc::{vector, Vector};
-use jrsonnet_gcmodule::{cc_dyn, Acyclic, Cc, Trace, Weak};
+use im_rc::{Vector, vector};
+use jrsonnet_gcmodule::{Acyclic, Cc, Trace, Weak, cc_dyn};
use jrsonnet_interner::IStr;
use jrsonnet_ir::Span;
use rustc_hash::{FxHashMap, FxHashSet};
@@ -23,13 +23,13 @@
pub use oop::ObjValueBuilder;
use crate::{
+ CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,
arr::{PickObjectKeyValues, PickObjectValues},
bail,
- error::{suggest_object_fields, ErrorKind::*},
+ error::{ErrorKind::*, suggest_object_fields},
evaluate::operator::evaluate_add_op,
identity_hash,
val::{ArrValue, ThunkValue},
- CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,
};
#[cfg(not(feature = "exp-preserve-order"))]
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -10,7 +10,7 @@
#[cfg(feature = "explaining-traces")]
use jrsonnet_ir::Span;
-use crate::{error::ErrorKind, Error};
+use crate::{Error, error::ErrorKind};
/// The way paths should be displayed
#[derive(Clone, Trace)]
@@ -259,7 +259,7 @@
struct ResetData {
loc: Span,
}
- use hi_doc::{source_to_ansi, Formatting, SnippetBuilder, Text};
+ use hi_doc::{Formatting, SnippetBuilder, Text, source_to_ansi};
write!(out, "{}", error.error())?;
if let ErrorKind::ImportSyntaxError { path, error } = error.error() {
@@ -277,14 +277,15 @@
use crate::analyze::DiagLevel;
let mut builder: Option<SnippetBuilder> = None;
let mut current_src: Option<&str> = None;
- let flush =
- |builder: Option<SnippetBuilder>, out: &mut dyn std::fmt::Write| -> Result<(), std::fmt::Error> {
- if let Some(b) = builder {
- let ansi = source_to_ansi(&b.build());
- write!(out, "\n{}", ansi.trim_end())?;
- }
- Ok(())
- };
+ let flush = |builder: Option<SnippetBuilder>,
+ out: &mut dyn std::fmt::Write|
+ -> Result<(), std::fmt::Error> {
+ if let Some(b) = builder {
+ let ansi = source_to_ansi(&b.build());
+ write!(out, "\n{}", ansi.trim_end())?;
+ }
+ Ok(())
+ };
for diag in diagnostics {
if let Some(span) = &diag.span {
let src = span.0.code();
@@ -295,14 +296,12 @@
}
let b = builder.as_mut().unwrap();
let ab = match diag.level {
- DiagLevel::Error => b.error(Text::fragment(
- diag.message.clone(),
- Formatting::default(),
- )),
- DiagLevel::Warning => b.warning(Text::fragment(
- diag.message.clone(),
- Formatting::default(),
- )),
+ DiagLevel::Error => {
+ b.error(Text::fragment(diag.message.clone(), Formatting::default()))
+ }
+ DiagLevel::Warning => {
+ b.warning(Text::fragment(diag.message.clone(), Formatting::default()))
+ }
};
ab.range(span.range()).build();
} else {
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -17,7 +17,13 @@
pub use crate::arr::{ArrValue, ArrayLike};
use crate::{
- NumValue, ObjValue, Result, SupThis, Unbound, WeakSupThis, bail, error::{Error, ErrorKind::*}, evaluate::operator::{evaluate_compare_op, evaluate_mod_op}, function::FuncVal, gc::WithCapacityExt as _, manifest::{ManifestFormat, ToStringFormat}, typed::BoundedUsize
+ NumValue, ObjValue, Result, SupThis, Unbound, WeakSupThis, bail,
+ error::{Error, ErrorKind::*},
+ evaluate::operator::{evaluate_compare_op, evaluate_mod_op},
+ function::FuncVal,
+ gc::WithCapacityExt as _,
+ manifest::{ManifestFormat, ToStringFormat},
+ typed::BoundedUsize,
};
pub trait ThunkValue: Trace {
crates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-ir-parser/src/lib.rs
+++ b/crates/jrsonnet-ir-parser/src/lib.rs
@@ -1,11 +1,11 @@
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_ir::{
- unescape, ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BinaryOpType, BindSpec, CompSpec,
- Destruct, Expr, ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse,
- IfSpecData, ImportKind, IndexPart, LiteralType, Member, NumValue, ObjBody, ObjComp, ObjMembers,
- Slice, SliceDesc, Source, Span, Spanned, UnaryOpType, Visibility,
+ ArgsDesc, AssertExpr, AssertStmt, BinaryOp, BinaryOpType, BindSpec, CompSpec, Destruct, Expr,
+ ExprParam, ExprParams, FieldMember, FieldName, ForSpecData, IStr, IfElse, IfSpecData,
+ ImportKind, IndexPart, LiteralType, Member, NumValue, ObjBody, ObjComp, ObjMembers, Slice,
+ SliceDesc, Source, Span, Spanned, UnaryOpType, Visibility, unescape,
};
-use jrsonnet_lexer::{collect_lexed_str_block, Lexeme, Lexer, Span as LexSpan, SyntaxKind, T};
+use jrsonnet_lexer::{Lexeme, Lexer, Span as LexSpan, SyntaxKind, T, collect_lexed_str_block};
pub struct ParserSettings {
pub source: Source,
crates/jrsonnet-ir/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-ir/src/expr.rs
+++ b/crates/jrsonnet-ir/src/expr.rs
@@ -7,9 +7,9 @@
use jrsonnet_interner::IStr;
use crate::{
+ NumValue,
function::{FunctionSignature, ParamDefault, ParamName, ParamParse},
source::Source,
- NumValue,
};
#[derive(Debug, PartialEq, Acyclic)]
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -3,14 +3,13 @@
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::{
- parenthesized,
+ Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn,
+ LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type, parenthesized,
parse::{Parse, ParseStream},
parse_macro_input,
punctuated::Punctuated,
spanned::Spanned,
token::Comma,
- Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn,
- LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type,
};
use self::typed::{derive_from_untyped_inner, derive_into_untyped_inner, derive_typed_inner};
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -1,12 +1,11 @@
#![allow(non_snake_case)]
use jrsonnet_evaluator::{
- bail, error,
- function::{builtin, NativeFn},
+ Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val, bail, error,
+ function::{NativeFn, builtin},
runtime_error,
typed::{BoundedUsize, Either2, FromUntyped},
- val::{equals, ArrValue, IndexableVal},
- Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
+ val::{ArrValue, IndexableVal, equals},
};
pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {
crates/jrsonnet-stdlib/src/compat.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/compat.rs
+++ b/crates/jrsonnet-stdlib/src/compat.rs
@@ -1,6 +1,6 @@
use std::cmp::Ordering;
-use jrsonnet_evaluator::{function::builtin, val::ArrValue, Result, Val};
+use jrsonnet_evaluator::{Result, Val, function::builtin, val::ArrValue};
#[builtin]
#[allow(non_snake_case)]
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -12,7 +12,12 @@
pub use encoding::*;
pub use hash::*;
use jrsonnet_evaluator::{
- IStr, InitialContextBuilder, NumValue, ObjValue, ObjValueBuilder, Source, Thunk, Val, error::Result, function::{CallLocation, FuncVal, builtin_id}, tla::TlaArg, trace::PathResolver, typed::SerializeTypedObj as _
+ IStr, InitialContextBuilder, NumValue, ObjValue, ObjValueBuilder, Source, Thunk, Val,
+ error::Result,
+ function::{CallLocation, FuncVal, builtin_id},
+ tla::TlaArg,
+ trace::PathResolver,
+ typed::SerializeTypedObj as _,
};
use jrsonnet_gcmodule::{Acyclic, Cc, Trace};
use jrsonnet_macros::{IntoUntyped, Typed};
crates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/toml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/toml.rs
@@ -1,7 +1,10 @@
use std::borrow::Cow;
use jrsonnet_evaluator::{
- Error, IStr, ObjValue, Result, ResultExt, Val, bail, ensure_sufficient_stack, in_description_frame, manifest::{ManifestFormat, escape_string_json_buf}, val::ArrValue
+ Error, IStr, ObjValue, Result, ResultExt, Val, bail, ensure_sufficient_stack,
+ in_description_frame,
+ manifest::{ManifestFormat, escape_string_json_buf},
+ val::ArrValue,
};
pub struct TomlFormat<'s> {
@@ -218,14 +221,16 @@
}
first = false;
path.push(k.clone());
- ensure_sufficient_stack(|| in_description_frame(
- || format!("section <{k}> manifestification"),
- || match v {
- Val::Obj(obj) => manifest_table(&obj, path, buf, cur_padding, options),
- Val::Arr(arr) => manifest_table_array(&arr, path, buf, cur_padding, options),
- _ => unreachable!("iterating over sections"),
- },
- ))?;
+ ensure_sufficient_stack(|| {
+ in_description_frame(
+ || format!("section <{k}> manifestification"),
+ || match v {
+ Val::Obj(obj) => manifest_table(&obj, path, buf, cur_padding, options),
+ Val::Arr(arr) => manifest_table_array(&arr, path, buf, cur_padding, options),
+ _ => unreachable!("iterating over sections"),
+ },
+ )
+ })?;
path.pop();
}
Ok(())
crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -1,13 +1,12 @@
use std::{cell::RefCell, collections::BTreeSet};
use jrsonnet_evaluator::{
- bail,
+ Either, IStr, ObjValue, ObjValueBuilder, ResultExt, Thunk, Val, bail,
error::{ErrorKind::*, Result},
- function::{builtin, CallLocation, FuncVal},
+ function::{CallLocation, FuncVal, builtin},
manifest::JsonFormat,
typed::{Either2, Either4},
- val::{equals, ArrValue},
- Either, IStr, ObjValue, ObjValueBuilder, ResultExt, Thunk, Val,
+ val::{ArrValue, equals},
};
use jrsonnet_gcmodule::Cc;
crates/jrsonnet-stdlib/src/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/operator.rs
+++ b/crates/jrsonnet-stdlib/src/operator.rs
@@ -2,11 +2,11 @@
//! However, in our case we instead implement them in native, and implement native functions on top of core for backwards compatibility
use jrsonnet_evaluator::{
+ IStr, NumValue, Result, Val,
function::builtin,
stdlib::std_format,
typed::{Either, Either2},
val::{equals, primitive_equals},
- IStr, NumValue, Result, Val,
};
#[builtin]
crates/jrsonnet-stdlib/src/sets.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/sets.rs
+++ b/crates/jrsonnet-stdlib/src/sets.rs
@@ -1,6 +1,6 @@
use std::cmp::Ordering;
-use jrsonnet_evaluator::{function::builtin, val::ArrValue, Result, Thunk, Val};
+use jrsonnet_evaluator::{Result, Thunk, Val, function::builtin, val::ArrValue};
use crate::keyf::KeyF;
crates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/sort.rs
+++ b/crates/jrsonnet-stdlib/src/sort.rs
@@ -3,10 +3,9 @@
use std::cmp::Ordering;
use jrsonnet_evaluator::{
- bail,
+ Result, Thunk, Val, bail,
function::builtin,
- val::{equals, ArrValue},
- Result, Thunk, Val,
+ val::{ArrValue, equals},
};
use crate::{eval_on_empty, keyf::KeyF};
tests/tests/builtin.rsdiffbeforeafterboth--- a/tests/tests/builtin.rs
+++ b/tests/tests/builtin.rs
@@ -1,7 +1,11 @@
mod common;
use jrsonnet_evaluator::{
- ContextInitializer, FileImportResolver, InitialContextBuilder, Result, Source, State, Thunk, Val, function::{CallLocation, FuncVal, builtin, builtin::{Builtin}}, trace::PathResolver, typed::FromUntyped
+ ContextInitializer, FileImportResolver, InitialContextBuilder, Result, Source, State, Thunk,
+ Val,
+ function::{CallLocation, FuncVal, builtin, builtin::Builtin},
+ trace::PathResolver,
+ typed::FromUntyped,
};
use jrsonnet_gcmodule::Trace;
use jrsonnet_stdlib::ContextInitializer as StdContextInitializer;
tests/tests/common.rsdiffbeforeafterboth--- a/tests/tests/common.rs
+++ b/tests/tests/common.rs
@@ -1,5 +1,7 @@
use jrsonnet_evaluator::{
- ContextBuilder, ContextInitializer as ContextInitializerT, InitialContextBuilder, ObjValueBuilder, Result, Thunk, Val, bail, function::{FuncVal, builtin}, Source
+ ContextBuilder, ContextInitializer as ContextInitializerT, InitialContextBuilder,
+ ObjValueBuilder, Result, Source, Thunk, Val, bail,
+ function::{FuncVal, builtin},
};
use jrsonnet_gcmodule::Trace;