difftreelog
fix disallow field duplicates
in: master
5 files changed
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth1use crate::{2 builtin::{format::FormatError, sort::SortError},3 typed::TypeLocError,4};5use gcmodule::Trace;6use jrsonnet_interner::IStr;7use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};8use jrsonnet_types::ValType;9use std::{10 path::{Path, PathBuf},11 rc::Rc,12};13use thiserror::Error;1415#[derive(Error, Debug, Clone, Trace)]16pub enum Error {17 #[error("intrinsic not found: {0}")]18 IntrinsicNotFound(IStr),1920 #[error("operator {0} does not operate on type {1}")]21 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),22 #[error("binary operation {1} {0} {2} is not implemented")]23 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),2425 #[error("no top level object in this context")]26 NoTopLevelObjectFound,27 #[error("self is only usable inside objects")]28 CantUseSelfOutsideOfObject,29 #[error("no super found")]30 NoSuperFound,3132 #[error("for loop can only iterate over arrays")]33 InComprehensionCanOnlyIterateOverArray,3435 #[error("array out of bounds: {0} is not within [0,{1})")]36 ArrayBoundsError(usize, usize),3738 #[error("assert failed: {0}")]39 AssertionFailed(IStr),4041 #[error("variable is not defined: {0}")]42 VariableIsNotDefined(IStr),43 #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]44 TypeMismatch(&'static str, Vec<ValType>, ValType),45 #[error("no such field: {0}")]46 NoSuchField(IStr),4748 #[error("only functions can be called, got {0}")]49 OnlyFunctionsCanBeCalledGot(ValType),50 #[error("parameter {0} is not defined")]51 UnknownFunctionParameter(String),52 #[error("argument {0} is already bound")]53 BindingParameterASecondTime(IStr),54 #[error("too many args, function has {0}")]55 TooManyArgsFunctionHas(usize),56 #[error("function argument is not passed: {0}")]57 FunctionParameterNotBoundInCall(IStr),5859 #[error("external variable is not defined: {0}")]60 UndefinedExternalVariable(IStr),61 #[error("native is not defined: {0}")]62 UndefinedExternalFunction(IStr),6364 #[error("field name should be string, got {0}")]65 FieldMustBeStringGot(ValType),6667 #[error("attempted to index array with string {0}")]68 AttemptedIndexAnArrayWithString(IStr),69 #[error("{0} index type should be {1}, got {2}")]70 ValueIndexMustBeTypeGot(ValType, ValType, ValType),71 #[error("cant index into {0}")]72 CantIndexInto(ValType),73 #[error("{0} is not indexable")]74 ValueIsNotIndexable(ValType),7576 #[error("super can't be used standalone")]77 StandaloneSuper,7879 #[error("can't resolve {1} from {0}")]80 ImportFileNotFound(PathBuf, PathBuf),81 #[error("resolved file not found: {0}")]82 ResolvedFileNotFound(PathBuf),83 #[error("imported file is not valid utf-8: {0:?}")]84 ImportBadFileUtf8(PathBuf),85 #[error("import io error: {0}")]86 ImportIo(String),87 #[error("tried to import {1} from {0}, but imports is not supported")]88 ImportNotSupported(PathBuf, PathBuf),89 #[error(90 "syntax error: expected {}, got {:?}",91 .error.expected,92 .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())93 )]94 ImportSyntaxError {95 #[skip_trace]96 path: Rc<Path>,97 source_code: IStr,98 #[skip_trace]99 error: Box<jrsonnet_parser::ParseError>,100 },101102 #[error("runtime error: {0}")]103 RuntimeError(IStr),104 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]105 StackOverflow,106 #[error("infinite recursion detected")]107 RecursiveLazyValueEvaluation,108 #[error("tried to index by fractional value")]109 FractionalIndex,110 #[error("attempted to divide by zero")]111 DivisionByZero,112113 #[error("string manifest output is not an string")]114 StringManifestOutputIsNotAString,115 #[error("stream manifest output is not an array")]116 StreamManifestOutputIsNotAArray,117 #[error("multi manifest output is not an object")]118 MultiManifestOutputIsNotAObject,119120 #[error("cant recurse stream manifest")]121 StreamManifestOutputCannotBeRecursed,122 #[error("stream manifest output cannot consist of raw strings")]123 StreamManifestCannotNestString,124125 #[error("{0}")]126 ImportCallbackError(String),127 #[error("invalid unicode codepoint: {0}")]128 InvalidUnicodeCodepointGot(u32),129130 #[error("format error: {0}")]131 Format(#[from] FormatError),132 #[error("type error: {0}")]133 TypeError(TypeLocError),134 #[error("sort error: {0}")]135 Sort(#[from] SortError),136137 #[cfg(feature = "anyhow-error")]138 #[error(transparent)]139 Other(Rc<anyhow::Error>),140}141142#[cfg(feature = "anyhow-error")]143impl From<anyhow::Error> for LocError {144 fn from(e: anyhow::Error) -> Self {145 Self::new(Error::Other(Rc::new(e)))146 }147}148149impl From<Error> for LocError {150 fn from(e: Error) -> Self {151 Self::new(e)152 }153}154155#[derive(Clone, Debug, Trace)]156pub struct StackTraceElement {157 pub location: Option<ExprLocation>,158 pub desc: String,159}160#[derive(Debug, Clone, Trace)]161pub struct StackTrace(pub Vec<StackTraceElement>);162163#[derive(Debug, Clone, Trace)]164pub struct LocError(Box<(Error, StackTrace)>);165impl LocError {166 pub fn new(e: Error) -> Self {167 Self(Box::new((e, StackTrace(vec![]))))168 }169170 pub const fn error(&self) -> &Error {171 &(self.0).0172 }173 pub fn error_mut(&mut self) -> &mut Error {174 &mut (self.0).0175 }176 pub const fn trace(&self) -> &StackTrace {177 &(self.0).1178 }179 pub fn trace_mut(&mut self) -> &mut StackTrace {180 &mut (self.0).1181 }182}183184pub type Result<V, E = LocError> = std::result::Result<V, E>;185186#[macro_export]187macro_rules! throw {188 ($e: expr) => {189 return Err($e.into())190 };191}crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -298,7 +298,7 @@
context_creator: context_creator.clone(),
value: value.clone(),
name,
- })));
+ })))?;
}
Member::Field(FieldMember {
name,
@@ -341,7 +341,7 @@
value: value.clone(),
params: params.clone(),
name,
- })));
+ })))?;
}
Member::BindStmt(_) => {}
Member::AssertStmt(stmt) => {
@@ -424,7 +424,7 @@
.bindable(TraceBox(Box::new(ObjCompBinding {
context: ctx,
value: obj.value.clone(),
- })));
+ })))?;
}
v => throw!(FieldMustBeStringGot(v.value_type())),
}
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -68,7 +68,7 @@
Value::Object(o) => {
let mut builder = ObjValueBuilder::with_capacity(o.len());
for (k, v) in o {
- builder.member((k as &str).into()).value(v.try_into()?);
+ builder.member((k as &str).into()).value(v.try_into()?)?;
}
Self::Obj(builder.build())
}
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,6 +1,11 @@
+use crate::function::CallLocation;
use crate::gc::{GcHashMap, GcHashSet, TraceBox};
use crate::operator::evaluate_add_op;
-use crate::{cc_ptr_eq, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal, Result, Val};
+use crate::push_frame;
+use crate::{
+ cc_ptr_eq, error::Error::*, throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal,
+ Result, Val,
+};
use gcmodule::{Cc, Trace, Weak};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ExprLocation, Visibility};
@@ -373,22 +378,29 @@
self.location = Some(location);
self
}
- pub fn value(self, value: Val) -> &'v mut ObjValueBuilder {
+ pub fn value(self, value: Val) -> Result<()> {
self.binding(LazyBinding::Bound(LazyVal::new_resolved(value)))
}
- pub fn bindable(self, bindable: TraceBox<dyn Bindable>) -> &'v mut ObjValueBuilder {
+ pub fn bindable(self, bindable: TraceBox<dyn Bindable>) -> Result<()> {
self.binding(LazyBinding::Bindable(Cc::new(bindable)))
}
- pub fn binding(self, binding: LazyBinding) -> &'v mut ObjValueBuilder {
- self.value.map.insert(
- self.name,
+ pub fn binding(self, binding: LazyBinding) -> Result<()> {
+ let old = self.value.map.insert(
+ self.name.clone(),
ObjMember {
add: self.add,
visibility: self.visibility,
invoke: binding,
- location: self.location,
+ location: self.location.clone(),
},
);
- self.value
+ if old.is_some() {
+ push_frame(
+ CallLocation(self.location.as_ref()),
+ || format!("field <{}> initializtion", self.name.clone()),
+ || throw!(DuplicateFieldName(self.name.clone())),
+ )?
+ }
+ Ok(())
}
}
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -459,12 +459,12 @@
if self.is_option() {
quote! {
if let Some(value) = self.#ident {
- out.member(#name.into()).value(value.try_into()?);
+ out.member(#name.into()).value(value.try_into()?)?;
}
}
} else {
quote! {
- out.member(#name.into()).value(self.#ident.try_into()?);
+ out.member(#name.into()).value(self.#ident.try_into()?)?;
}
}
} else if self.is_option() {