difftreelog
refactor add proper getters for LocExpr
in: master
10 files changed
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -53,8 +53,6 @@
serde.workspace = true
anyhow = { workspace = true, optional = true }
-# Serialized stdlib
-bincode = { workspace = true, optional = true }
# Explaining traces
annotate-snippets = { workspace = true, optional = true }
# Better explaining traces
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -7,7 +7,7 @@
use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, LocExpr, Source, SourcePath, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, LocExpr, Source, SourcePath, Span, UnaryOpType};
use jrsonnet_types::ValType;
use thiserror::Error;
@@ -275,7 +275,7 @@
pub struct StackTraceElement {
/// Source of this frame
/// Some frames only act as description, without attached source
- pub location: Option<ExprLocation>,
+ pub location: Option<Span>,
/// Frame description
pub desc: String,
}
@@ -324,20 +324,20 @@
impl std::error::Error for Error {}
pub trait ErrorSource {
- fn to_location(self) -> Option<ExprLocation>;
+ fn to_location(self) -> Option<Span>;
}
impl ErrorSource for &LocExpr {
- fn to_location(self) -> Option<ExprLocation> {
- Some(self.1.clone())
+ fn to_location(self) -> Option<Span> {
+ Some(self.span())
}
}
-impl ErrorSource for &ExprLocation {
- fn to_location(self) -> Option<ExprLocation> {
+impl ErrorSource for &Span {
+ fn to_location(self) -> Option<Span> {
Some(self.clone())
}
}
impl ErrorSource for CallLocation<'_> {
- fn to_location(self) -> Option<ExprLocation> {
+ fn to_location(self) -> Option<Span> {
self.0.cloned()
}
}
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth262627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {27pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28 fn is_trivial(expr: &LocExpr) -> bool {28 fn is_trivial(expr: &LocExpr) -> bool {29 match &*expr.0 {29 match expr.expr() {30 Expr::Str(_)30 Expr::Str(_)31 | Expr::Num(_)31 | Expr::Num(_)32 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,32 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,35 _ => false,35 _ => false,36 }36 }37 }37 }38 Some(match &*expr.0 {38 Some(match expr.expr() {39 Expr::Str(s) => Val::string(s.clone()),39 Expr::Str(s) => Val::string(s.clone()),40 Expr::Num(n) => {40 Expr::Num(n) => {41 Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))41 Val::Num(NumValue::new(*n).expect("parser will not allow non-finite values"))72 Ok(match field_name {72 Ok(match field_name {73 FieldName::Fixed(n) => Some(n.clone()),73 FieldName::Fixed(n) => Some(n.clone()),74 FieldName::Dyn(expr) => State::push(74 FieldName::Dyn(expr) => State::push(75 CallLocation::new(&expr.1),75 CallLocation::new(&expr.span()),76 || "evaluating field name".to_string(),76 || "evaluating field name".to_string(),77 || {77 || {78 let value = evaluate(ctx, expr)?;78 let value = evaluate(ctx, expr)?;231 .field(name.clone())231 .field(name.clone())232 .with_add(*plus)232 .with_add(*plus)233 .with_visibility(*visibility)233 .with_visibility(*visibility)234 .with_location(value.1.clone())234 .with_location(value.span())235 .bindable(UnboundValue {235 .bindable(UnboundValue {236 uctx,236 uctx,237 value: value.clone(),237 value: value.clone(),266 builder266 builder267 .field(name.clone())267 .field(name.clone())268 .with_visibility(*visibility)268 .with_visibility(*visibility)269 .with_location(value.1.clone())269 .with_location(value.span())270 .bindable(UnboundMethod {270 .bindable(UnboundMethod {271 uctx,271 uctx,272 value: value.clone(),272 value: value.clone(),385 let value = &assertion.0;385 let value = &assertion.0;386 let msg = &assertion.1;386 let msg = &assertion.1;387 let assertion_result = State::push(387 let assertion_result = State::push(388 CallLocation::new(&value.1),388 CallLocation::new(&value.span()),389 || "assertion condition".to_owned(),389 || "assertion condition".to_owned(),390 || bool::from_untyped(evaluate(ctx.clone(), value)?),390 || bool::from_untyped(evaluate(ctx.clone(), value)?),391 )?;391 )?;392 if !assertion_result {392 if !assertion_result {393 State::push(393 State::push(394 CallLocation::new(&value.1),394 CallLocation::new(&value.span()),395 || "assertion failure".to_owned(),395 || "assertion failure".to_owned(),396 || {396 || {397 if let Some(msg) = msg {397 if let Some(msg) = msg {406406407pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {407pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {408 use Expr::*;408 use Expr::*;409 let LocExpr(raw_expr, _loc) = expr;410 Ok(match &**raw_expr {409 Ok(match expr.expr() {411 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),412 _ => evaluate(ctx, expr)?,411 _ => evaluate(ctx, expr)?,413 })412 })420 if let Some(trivial) = evaluate_trivial(expr) {419 if let Some(trivial) = evaluate_trivial(expr) {421 return Ok(trivial);420 return Ok(trivial);422 }421 }423 let LocExpr(expr, loc) = expr;422 let loc = expr.span();424 Ok(match &**expr {423 Ok(match expr.expr() {425 Literal(LiteralType::This) => {424 Literal(LiteralType::This) => {426 Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())425 Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())427 }426 }448 // because the standalone super literal is not supported, that is because in other447 // because the standalone super literal is not supported, that is because in other449 // implementations `in super` treated differently from in `smth_else`.448 // implementations `in super` treated differently from in `smth_else`.450 BinaryOp(field, BinaryOpType::In, e)449 BinaryOp(field, BinaryOpType::In, e)451 if matches!(&*e.0, Expr::Literal(LiteralType::Super)) =>450 if matches!(e.expr(), Expr::Literal(LiteralType::Super)) =>452 {451 {453 let Some(super_obj) = ctx.super_obj() else {452 let Some(super_obj) = ctx.super_obj() else {454 return Ok(Val::Bool(false));453 return Ok(Val::Bool(false));459 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,458 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,460 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,459 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,461 Var(name) => State::push(460 Var(name) => State::push(462 CallLocation::new(loc),461 CallLocation::new(&loc),463 || format!("variable <{name}> access"),462 || format!("variable <{name}> access"),464 || ctx.binding(name.clone())?.evaluate(),463 || ctx.binding(name.clone())?.evaluate(),465 )?,464 )?,466 Index { indexable, parts } => {465 Index { indexable, parts } => {467 let mut parts = parts.iter();466 let mut parts = parts.iter();468 let mut indexable = match &indexable {467 let mut indexable = if matches!(indexable.expr(), Expr::Literal(LiteralType::Super)) {469 // Cheaper to execute than creating object with overriden `this`470 LocExpr(v, _) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {471 let part = parts.next().expect("at least part should exist");468 let part = parts.next().expect("at least part should exist");472 let Some(super_obj) = ctx.super_obj() else {469 let Some(super_obj) = ctx.super_obj() else {473 #[cfg(feature = "exp-null-coaelse")]470 #[cfg(feature = "exp-null-coaelse")]503 bail!(NoSuchField(name, suggestions))500 bail!(NoSuchField(name, suggestions))504 }501 }505 }502 }506 }503 } else {507 e => evaluate(ctx.clone(), e)?,504 evaluate(ctx.clone(), indexable)?508 };505 };509506510 for part in parts {507 for part in parts {511 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {508 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {639 &Val::Obj(evaluate_object(ctx, b)?),636 &Val::Obj(evaluate_object(ctx, b)?),640 )?,637 )?,641 Apply(value, args, tailstrict) => {638 Apply(value, args, tailstrict) => {642 evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?639 evaluate_apply(ctx, value, args, CallLocation::new(&loc), *tailstrict)?643 }640 }644 Function(params, body) => {641 Function(params, body) => {645 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())642 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())649 evaluate(ctx, returned)?646 evaluate(ctx, returned)?650 }647 }651 ErrorStmt(e) => State::push(648 ErrorStmt(e) => State::push(652 CallLocation::new(loc),649 CallLocation::new(&loc),653 || "error statement".to_owned(),650 || "error statement".to_owned(),654 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),651 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),655 )?,652 )?,659 cond_else,656 cond_else,660 } => {657 } => {661 if State::push(658 if State::push(662 CallLocation::new(loc),659 CallLocation::new(&loc),663 || "if condition".to_owned(),660 || "if condition".to_owned(),664 || bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),661 || bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),665 )? {662 )? {690 }687 }691688692 let indexable = evaluate(ctx.clone(), value)?;689 let indexable = evaluate(ctx.clone(), value)?;693 let loc = CallLocation::new(loc);690 let loc = CallLocation::new(&loc);694691695 let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;692 let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;696 let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;693 let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;699 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?696 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?700 }697 }701 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {698 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {702 let Expr::Str(path) = &*path.0 else {699 let Expr::Str(path) = &path.expr() else {703 bail!("computed imports are not supported")700 bail!("computed imports are not supported")704 };701 };705 let tmp = loc.clone().0;702 let tmp = loc.clone().0;706 let s = ctx.state();703 let s = ctx.state();707 let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;704 let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;708 match i {705 match i {709 Import(_) => State::push(706 Import(_) => State::push(710 CallLocation::new(loc),707 CallLocation::new(&loc),711 || format!("import {:?}", path.clone()),708 || format!("import {:?}", path.clone()),712 || s.import_resolved(resolved_path),709 || s.import_resolved(resolved_path),713 )?,710 )?,crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -4,7 +4,7 @@
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
pub use jrsonnet_macros::builtin;
-use jrsonnet_parser::{Destruct, Expr, ExprLocation, LocExpr, ParamsDesc};
+use jrsonnet_parser::{Destruct, Expr, LocExpr, ParamsDesc, Span};
use self::{
arglike::OptionalContext,
@@ -22,10 +22,10 @@
/// Function callsite location.
/// Either from other jsonnet code, specified by expression location, or from native (without location).
#[derive(Clone, Copy)]
-pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);
+pub struct CallLocation<'l>(pub Option<&'l Span>);
impl<'l> CallLocation<'l> {
/// Construct new location for calls coming from specified jsonnet expression location.
- pub const fn new(loc: &'l ExprLocation) -> Self {
+ pub const fn new(loc: &'l Span) -> Self {
Self(Some(loc))
}
}
@@ -225,7 +225,7 @@
#[cfg(feature = "exp-destruct")]
_ => return false,
};
- &desc.body.0 as &Expr == &Expr::Var(id.clone())
+ desc.body.expr() == &Expr::Var(id.clone())
}
_ => false,
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -45,7 +45,7 @@
#[doc(hidden)]
pub use jrsonnet_macros;
pub use jrsonnet_parser as parser;
-use jrsonnet_parser::{ExprLocation, LocExpr, ParserSettings, Source, SourcePath};
+use jrsonnet_parser::{LocExpr, ParserSettings, Source, SourcePath, Span};
pub use obj::*;
use stack::check_depth;
pub use tla::apply_tla;
@@ -369,7 +369,7 @@
/// Executes code creating a new stack frame
pub fn push_val(
&self,
- e: &ExprLocation,
+ e: &Span,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<Val>,
) -> Result<Val> {
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -8,7 +8,7 @@
use jrsonnet_gcmodule::{Cc, Trace, Weak};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ExprLocation, Visibility};
+use jrsonnet_parser::{Span, Visibility};
use rustc_hash::FxHashMap;
use crate::{
@@ -135,7 +135,7 @@
flags: ObjFieldFlags,
original_index: FieldIndex,
pub invoke: MaybeUnbound,
- pub location: Option<ExprLocation>,
+ pub location: Option<Span>,
}
pub trait ObjectAssertion: Trace {
@@ -896,7 +896,7 @@
add: bool,
visibility: Visibility,
original_index: FieldIndex,
- location: Option<ExprLocation>,
+ location: Option<Span>,
}
#[allow(clippy::missing_const_for_fn)]
@@ -926,7 +926,7 @@
pub fn hide(self) -> Self {
self.with_visibility(Visibility::Hidden)
}
- pub fn with_location(mut self, location: ExprLocation) -> Self {
+ pub fn with_location(mut self, location: Span) -> Self {
self.location = Some(location);
self
}
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -5,7 +5,7 @@
};
use jrsonnet_gcmodule::Trace;
-use jrsonnet_parser::{CodeLocation, ExprLocation, Source};
+use jrsonnet_parser::{CodeLocation, Source, Span};
use crate::{error::ErrorKind, Error};
@@ -380,7 +380,7 @@
error: &Error,
) -> Result<(), std::fmt::Error> {
struct ResetData {
- loc: ExprLocation,
+ loc: Span,
}
use hi_doc::{source_to_ansi, Formatting, SnippetBuilder, Text};
@@ -399,7 +399,7 @@
}
let trace = &error.trace();
let snippet_builder: RefCell<Option<SnippetBuilder>> = RefCell::new(None);
- let mut last_location: Option<ExprLocation> = None;
+ let mut last_location: Option<Span> = None;
let mut flush_builder = |data: Option<ResetData>| {
use std::fmt::Write;
let mut out = String::new();
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -376,7 +376,7 @@
State, Val,
function::{builtin::{Builtin, StaticBuiltin, BuiltinParam, ParamName, ParamDefault}, CallLocation, ArgsLike, parse::parse_builtin_call},
Result, Context, typed::Typed,
- parser::ExprLocation,
+ parser::Span,
};
const PARAMS: &'static [BuiltinParam] = &[
#(#params_desc)*
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -385,17 +385,16 @@
#[derive(Clone, PartialEq, Eq, Trace)]
#[trace(skip)]
#[repr(C)]
-pub struct ExprLocation(pub Source, pub u32, pub u32);
-impl ExprLocation {
- pub fn belongs_to(&self, other: &ExprLocation) -> bool {
+pub struct Span(pub Source, pub u32, pub u32);
+impl Span {
+ pub fn belongs_to(&self, other: &Span) -> bool {
other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2
}
}
-#[cfg(target_pointer_width = "64")]
-static_assertions::assert_eq_size!(ExprLocation, [u8; 16]);
+static_assertions::assert_eq_size!(Span, (usize, usize));
-impl Debug for ExprLocation {
+impl Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}:{:?}-{:?}", self.0, self.1, self.2)
}
@@ -403,19 +402,32 @@
/// Holds AST expression and its location in source file
#[derive(Clone, PartialEq, Trace)]
-pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);
+pub struct LocExpr(Rc<(Expr, Span)>);
+impl LocExpr {
+ pub fn new(expr: Expr, span: Span) -> Self {
+ Self(Rc::new((expr, span)))
+ }
+ #[inline]
+ pub fn span(&self) -> Span {
+ self.0 .1.clone()
+ }
+ #[inline]
+ pub fn expr(&self) -> &Expr {
+ &self.0 .0
+ }
+}
-#[cfg(target_pointer_width = "64")]
-static_assertions::assert_eq_size!(LocExpr, [u8; 24]);
+static_assertions::assert_eq_size!(LocExpr, usize);
impl Debug for LocExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ let expr = self.expr();
if f.alternate() {
- write!(f, "{:#?}", self.0)?;
+ write!(f, "{:#?}", expr)?;
} else {
- write!(f, "{:?}", self.0)?;
+ write!(f, "{:?}", expr)?;
}
- write!(f, " from {:?}", self.1)?;
+ write!(f, " from {:?}", self.span())?;
Ok(())
}
}
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -232,7 +232,7 @@
pub rule var_expr(s: &ParserSettings) -> Expr
= n:id() { expr::Expr::Var(n) }
pub rule id_loc(s: &ParserSettings) -> LocExpr
- = a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.source.clone(), a as u32,b as u32)) }
+ = a:position!() n:id() b:position!() { LocExpr::new(expr::Expr::Str(n), Span(s.source.clone(), a as u32,b as u32)) }
pub rule if_then_else_expr(s: &ParserSettings) -> Expr
= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{
cond,
@@ -299,7 +299,7 @@
use UnaryOpType::*;
rule expr(s: &ParserSettings) -> LocExpr
= precedence! {
- start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.source.clone(), start as u32, end as u32)) }
+ start:position!() v:@ end:position!() { LocExpr::new(v, Span(s.source.clone(), start as u32, end as u32)) }
--
a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}
a:(@) _ binop(<"??">) _ ensure_null_coaelse() b:@ {
@@ -370,10 +370,7 @@
/// Used for importstr values
pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {
let len = str.len();
- LocExpr(
- Rc::new(Expr::Str(str)),
- ExprLocation(settings.source.clone(), 0, len as u32),
- )
+ LocExpr::new(Expr::Str(str), Span(settings.source.clone(), 0, len as u32))
}
#[cfg(test)]
@@ -398,9 +395,9 @@
macro_rules! el {
($expr:expr, $from:expr, $to:expr$(,)?) => {
- LocExpr(
- std::rc::Rc::new($expr),
- ExprLocation(
+ LocExpr::new(
+ $expr,
+ Span(
Source::new_virtual("<test>".into(), IStr::empty()),
$from,
$to,