difftreelog
test pointer-size invariant snapshots
in: master
10 files changed
crates/jrsonnet-evaluator/src/analyze.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/analyze.rs
+++ b/crates/jrsonnet-evaluator/src/analyze.rs
@@ -1959,48 +1959,6 @@
}
}
-#[cfg(test)]
-fn render_diagnostics(src: &str, diags: &[Diagnostic]) -> String {
- use std::fmt::Write;
-
- use hi_doc::{Formatting, SnippetBuilder, Text};
-
- let mut out = String::new();
- let mut unspanned = Vec::new();
- let mut spanned: Vec<&Diagnostic> = Vec::new();
- for d in diags {
- if d.span.is_some() {
- spanned.push(d);
- } else {
- unspanned.push(d);
- }
- }
- if !spanned.is_empty() {
- let mut builder = SnippetBuilder::new(src);
- for d in spanned {
- let span = d.span.as_ref().expect("spanned");
- let ab = match d.level {
- DiagLevel::Error => {
- builder.error(Text::fragment(d.message.clone(), Formatting::default()))
- }
- DiagLevel::Warning => {
- builder.warning(Text::fragment(d.message.clone(), Formatting::default()))
- }
- };
- ab.range(span.range()).build();
- }
- out.push_str(&hi_doc::source_to_ansi(&builder.build()));
- }
- for d in unspanned {
- let prefix = match d.level {
- DiagLevel::Error => "error",
- DiagLevel::Warning => "warning",
- };
- writeln!(out, "{prefix}: {}", d.message).expect("fmt");
- }
- out
-}
-
pub struct AnalysisReport {
pub lir: LExpr,
pub root_shape: ClosureShape,
@@ -2011,16 +1969,63 @@
#[cfg(test)]
mod tests {
- use std::fs;
+ #[test]
+ #[cfg(not(feature = "exp-null-coaelse"))]
+ fn snapshots() {
+ use std::fs;
- use insta::{assert_snapshot, glob};
- use jrsonnet_ir::Source;
+ use insta::{assert_snapshot, glob};
+ use jrsonnet_ir::Source;
- use super::*;
+ use super::*;
- #[test]
- #[cfg(not(feature = "exp-null-coaelse"))]
- fn snapshots() {
+ fn render_diagnostics(src: &str, diags: &[Diagnostic]) -> String {
+ use std::fmt::Write;
+
+ use hi_doc::{Formatting, SnippetBuilder, Text};
+
+ let mut out = String::new();
+ let mut unspanned = Vec::new();
+ let mut spanned: Vec<&Diagnostic> = Vec::new();
+ for d in diags {
+ if d.span.is_some() {
+ spanned.push(d);
+ } else {
+ unspanned.push(d);
+ }
+ }
+ if !spanned.is_empty() {
+ let mut builder = SnippetBuilder::new(src);
+ for d in spanned {
+ let span = d.span.as_ref().expect("spanned");
+ let ab = match d.level {
+ DiagLevel::Error => {
+ builder.error(Text::fragment(d.message.clone(), Formatting::default()))
+ }
+ DiagLevel::Warning => builder
+ .warning(Text::fragment(d.message.clone(), Formatting::default())),
+ };
+ ab.range(span.range()).build();
+ }
+ out.push_str(&hi_doc::source_to_ansi(&builder.build()));
+ }
+ for d in unspanned {
+ let prefix = match d.level {
+ DiagLevel::Error => "error",
+ DiagLevel::Warning => "warning",
+ };
+ writeln!(out, "{prefix}: {}", d.message).expect("fmt");
+ }
+ out
+ }
+ fn fmt_depth(d: u32) -> String {
+ if d == u32::MAX {
+ "none".into()
+ } else {
+ d.to_string()
+ }
+ }
+
glob!("analysis_tests/*.jsonnet", |path| {
let code = fs::read_to_string(path).expect("read test file");
let src = Source::new_virtual("<test>".into(), code.clone().into());
@@ -2041,13 +2046,5 @@
);
assert_snapshot!(rendered);
});
- }
-
- fn fmt_depth(d: u32) -> String {
- if d == u32::MAX {
- "none".into()
- } else {
- d.to_string()
- }
}
}
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -123,9 +123,9 @@
EagerCompspecCaptured,
#[error("array out of bounds: {0} is not within [0,{1})")]
- ArrayBoundsError(isize, u32),
+ ArrayBoundsError(f64, u32),
#[error("string out of bounds: {0} is not within [0,{1})")]
- StringBoundsError(usize, usize),
+ StringBoundsError(f64, u32),
#[error("assert failed: {}", format_empty_str(.0))]
AssertionFailed(IStr),
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -18,12 +18,12 @@
LIndexPart, LObjAsserts, LObjBody, LObjMembers, LSlot,
},
arr::ArrValue,
- bail, error,
+ bail,
error::{ErrorKind::*, suggest_object_fields},
evaluate::{destructure::fill_letrec_binds, operator::evaluate_unary_op},
function::{CallLocation, FuncDesc, FuncVal, prepared::PreparedFuncVal},
in_frame,
- typed::FromUntyped as _,
+ typed::{BoundedUsize, FromUntyped as _},
val::{CachedUnbound, Thunk},
with_state,
};
@@ -193,7 +193,6 @@
}
LExpr::ArrComp(comp) => evaluate_arr_comp(ctx, comp)?,
LExpr::Slice(slice) => {
- use crate::typed::BoundedUsize;
let val = evaluate(ctx.clone(), &slice.value)?;
let indexable = val.into_indexable()?;
let start = slice
@@ -201,26 +200,14 @@
.as_ref()
.map(|e| evaluate(ctx.clone(), e))
.transpose()?
- .map(|v| -> Result<i32> {
- v.as_num()
- .ok_or_else(|| {
- TypeMismatch("slice start", vec![ValType::Num], v.value_type()).into()
- })
- .map(|n| n as i32)
- })
+ .map(|v| -> Result<i32> { i32::from_untyped(v).description("slice start value") })
.transpose()?;
let end = slice
.end
.as_ref()
.map(|e| evaluate(ctx.clone(), e))
.transpose()?
- .map(|v| -> Result<i32> {
- v.as_num()
- .ok_or_else(|| {
- TypeMismatch("slice end", vec![ValType::Num], v.value_type()).into()
- })
- .map(|n| n as i32)
- })
+ .map(|v| -> Result<i32> { i32::from_untyped(v).description("slice end value") })
.transpose()?;
let step = slice
.step
@@ -228,10 +215,7 @@
.map(|e| evaluate(ctx, e))
.transpose()?
.map(|v| -> Result<BoundedUsize<1, { i32::MAX as usize }>> {
- let n = v.as_num().ok_or_else(|| -> crate::Error {
- TypeMismatch("slice step", vec![ValType::Num], v.value_type()).into()
- })?;
- BoundedUsize::new(n as usize).ok_or_else(|| error!("slice step must be >= 1"))
+ BoundedUsize::from_untyped(v).description("slice step value")
})
.transpose()?;
Val::from(indexable.slice(start, end, step)?)
@@ -410,11 +394,9 @@
if n.fract() > f64::EPSILON {
bail!(FractionalIndex)
}
- if n < 0.0 {
- bail!(ArrayBoundsError(
- n as isize, // truncation is fine for error display
- arr.len()
- ));
+ let len = arr.len();
+ if n < 0.0 || n > f64::from(len) {
+ bail!(ArrayBoundsError(n, len));
}
#[expect(
clippy::cast_possible_truncation,
@@ -424,30 +406,30 @@
let i = n as u32;
arr.get(i)
.with_description_src(loc, || format!("element <{i}> access"))?
- .ok_or_else(|| ArrayBoundsError(i as isize, arr.len()))?
+ .ok_or_else(|| ArrayBoundsError(n, len))?
}
(Val::Str(s), Val::Num(idx)) => {
let n = idx.get();
if n.fract() > f64::EPSILON {
bail!(FractionalIndex)
}
- let flat = s.clone().into_flat();
- if n < 0.0 {
- bail!(ArrayBoundsError(
- n as isize, // truncation is fine for error display
- flat.chars().count() as u32
- ));
- }
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "n is checked positive, overflow will truncate as expected"
)]
let i = n as usize;
- let Some(char) = flat.chars().nth(i) else {
- bail!(StringBoundsError(i, flat.chars().count()))
- };
- Val::string(char)
+ let flat = s.clone().into_flat();
+ #[allow(clippy::cast_possible_truncation, reason = "string is max 4g")]
+ if n >= 0.0
+ && n <= f64::from(u32::MAX)
+ && let Some(char) = flat.chars().nth(i)
+ {
+ Val::string(char)
+ } else {
+ let len = flat.chars().count();
+ bail!(StringBoundsError(n, len as u32))
+ }
}
#[cfg(feature = "exp-null-coaelse")]
(Val::Null, _) if part.null_coaelse => return Ok(Val::Null),
@@ -566,7 +548,7 @@
let a_ctx = ctx
.pack_captures_sup_this(&members.frame_shape)
.enter(|fill, ctx| {
- fill_letrec_binds(fill, &ctx, &members.locals);
+ fill_letrec_binds(fill, ctx, &members.locals);
});
for field in &members.fields {
evaluate_field_member_static(&mut builder, ctx.clone(), a_ctx.clone(), field)?;
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -157,9 +157,7 @@
Self::BoundedNumber(from, to) => {
if let Val::Num(n) = value {
let n = n.get();
- if from.map(|from| from > n).unwrap_or(false)
- || to.map(|to| to < n).unwrap_or(false)
- {
+ if from.is_some_and(|from| from > n) || to.is_some_and(|to| to < n) {
return Err(TypeError::BoundsFailed(n, *from, *to).into());
}
Ok(())
crates/jrsonnet-interner/src/inner.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/inner.rs
+++ b/crates/jrsonnet-interner/src/inner.rs
@@ -161,6 +161,12 @@
// SAFETY: header is initialized
unsafe { (*header).refcnt() }
}
+
+ pub fn len32(&self) -> u32 {
+ let header = Self::header(self);
+ // SAFETY: header is initialized
+ unsafe { (*header).size }
+ }
}
impl Clone for Inner {
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth1#![deny(2 unsafe_op_in_unsafe_fn,3 clippy::missing_safety_doc,4 clippy::undocumented_unsafe_blocks5)]6#![warn(clippy::pedantic, clippy::nursery)]7#![allow(clippy::missing_const_for_fn)]8use std::{9 borrow::Cow,10 cell::RefCell,11 fmt::{self, Display},12 hash::{Hash, Hasher},13 ops::Deref,14 str,15};1617use hashbrown::{HashMap, hash_map::RawEntryMut};18use jrsonnet_gcmodule::{Acyclic, Trace};19use rustc_hash::FxBuildHasher;2021mod inner;22use inner::Inner;2324mod names;2526/// Interned string27///28/// Provides O(1) comparsions and hashing, cheap copy, and cheap conversion to [`IBytes`]29#[derive(Clone, PartialOrd, Ord, Eq)]30pub struct IStr(Inner);31impl Trace for IStr {32 fn is_type_tracked() -> bool {33 false34 }35}3637/// SAFETY:38///39/// `IStr` is acyclic40unsafe impl Acyclic for IStr {}4142impl IStr {43 #[must_use]44 pub fn empty() -> Self {45 "".into()46 }47 #[must_use]48 pub fn as_str(&self) -> &str {49 self as &str50 }5152 #[must_use]53 pub fn cast_bytes(self) -> IBytes {54 IBytes(self.0.clone())55 }5657 pub fn len32(&self) -> u32 {58 self.0.len32()59 }60}6162impl Deref for IStr {63 type Target = str;6465 fn deref(&self) -> &Self::Target {66 // SAFETY: Inner::check_utf8 is called on IStr construction, data is utf-867 unsafe { self.0.as_str_unchecked() }68 }69}7071impl PartialEq for IStr {72 fn eq(&self, other: &Self) -> bool {73 // all IStr should be inlined into same pool74 Inner::ptr_eq(&self.0, &other.0)75 }76}7778impl PartialEq<str> for IStr {79 fn eq(&self, other: &str) -> bool {80 self as &str == other81 }82}8384impl Hash for IStr {85 fn hash<H: Hasher>(&self, state: &mut H) {86 // IStr is always obtained from pool, where no string have duplicate, thus every unique string has unique address87 state.write_usize(Inner::as_ptr(&self.0).cast::<()>() as usize);88 }89}9091impl Drop for IStr {92 fn drop(&mut self) {93 maybe_unpool(&self.0);94 }95}9697impl fmt::Debug for IStr {98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {99 fmt::Debug::fmt(self as &str, f)100 }101}102103impl Display for IStr {104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {105 fmt::Display::fmt(self as &str, f)106 }107}108109/// Interned byte array110#[derive(Clone, PartialOrd, Ord, Eq)]111pub struct IBytes(Inner);112impl Trace for IBytes {113 fn is_type_tracked() -> bool {114 false115 }116}117118impl IBytes {119 #[must_use]120 pub fn cast_str(self) -> Option<IStr> {121 if Inner::check_utf8(&self.0) {122 Some(IStr(self.0.clone()))123 } else {124 None125 }126 }127 /// # Safety128 /// data should be valid utf8129 unsafe fn cast_str_unchecked(self) -> IStr {130 // SAFETY: data is utf8131 unsafe { Inner::assume_utf8(&self.0) };132 IStr(self.0.clone())133 }134135 #[must_use]136 pub fn as_slice(&self) -> &[u8] {137 self.0.as_slice()138 }139}140141impl Deref for IBytes {142 type Target = [u8];143144 fn deref(&self) -> &Self::Target {145 self.0.as_slice()146 }147}148149impl PartialEq for IBytes {150 fn eq(&self, other: &Self) -> bool {151 // all IStr should be inlined into same pool152 Inner::ptr_eq(&self.0, &other.0)153 }154}155156impl Hash for IBytes {157 fn hash<H: Hasher>(&self, state: &mut H) {158 // IBytes is always obtained from pool, where no string have duplicate, thus every unique string has unique address159 state.write_usize(Inner::as_ptr(&self.0).cast::<()>() as usize);160 }161}162163impl Drop for IBytes {164 fn drop(&mut self) {165 maybe_unpool(&self.0);166 }167}168169fn maybe_unpool(inner: &Inner) {170 #[cold]171 #[inline(never)]172 fn unpool(inner: &Inner) {173 // May fail on program termination174 let _ = POOL.try_with(|pool| {175 let mut pool = pool.borrow_mut();176177 if pool.remove(inner).is_none() {178 // DOC(string-pooling)179 // On some platforms (i.e i686-windows), try_with will not fail after TLS180 // destructor is called, but instead re-initialize the TLS with the empty pool.181 // Allow non-pooled Drop in this case.182 // https://github.com/CertainLach/jrsonnet/issues/98#issuecomment-1591624016183 // Another cause might be that you have improperly used jrsonnet in multi-threaded environment:184 // https://github.com/CertainLach/jrsonnet/issues/113185 debug_assert!(pool.is_empty(), "if you have landed here - you most likely did something naughty with multi-threading. jrsonnet string pooling uses thread_local pool");186 }187 });188 }189 // First reference - current object, second - POOL190 if Inner::strong_count(inner) <= 2 {191 unpool(inner);192 }193}194195impl fmt::Debug for IBytes {196 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {197 fmt::Debug::fmt(self as &[u8], f)198 }199}200201impl<'c> From<Cow<'c, str>> for IStr {202 fn from(v: Cow<'c, str>) -> Self {203 intern_str(&v)204 }205}206impl From<&str> for IStr {207 fn from(v: &str) -> Self {208 intern_str(v)209 }210}211impl From<String> for IStr {212 fn from(s: String) -> Self {213 s.as_str().into()214 }215}216impl From<&String> for IStr {217 fn from(s: &String) -> Self {218 s.as_str().into()219 }220}221impl From<char> for IStr {222 fn from(value: char) -> Self {223 let mut buf = [0; 5];224 Self::from(&*value.encode_utf8(&mut buf))225 }226}227impl From<&[u8]> for IBytes {228 fn from(v: &[u8]) -> Self {229 intern_bytes(v)230 }231}232233type PoolMap = HashMap<Inner, (), FxBuildHasher>;234235thread_local! {236 static POOL: RefCell<PoolMap> = RefCell::new(HashMap::with_capacity_and_hasher(200, FxBuildHasher));237}238239/// Utils for embedding jrsonnet in non-rust.240///241/// Jrsonnet golang bindings require that it is possible to move jsonnet242/// VM between OS threads, and this is not possible due to usage of243/// `thread_local`. Instead, there is two methods added, one should be244/// called at the end of current thread work, and one that should be245/// used when using other thread.246pub mod interop {247 use std::mem;248249 use crate::{POOL, PoolMap};250251 /// Type-erased interned string pool252 pub enum PoolState {}253254 /// Dump current interned string pool, to be restored by255 /// `reenter_thread`256 pub fn exit_thread() -> *mut PoolState {257 Box::into_raw(Box::new(POOL.with_borrow_mut(mem::take))).cast()258 }259260 /// Reenter thread, using state dumped by `exit_thread`.261 ///262 /// # Safety263 ///264 /// `state` should be acquired from `exit_thread`, it is not allowed265 /// to reuse state to reenter multiple threads.266 pub unsafe fn reenter_thread(state: *mut PoolState) {267 let ptr: *mut PoolMap = state.cast();268 // SAFETY: ptr is an unique state per method safety requirements.269 let ptr: Box<PoolMap> = unsafe { Box::from_raw(ptr) };270 let ptr: PoolMap = *ptr;271 POOL.with_borrow_mut(|pool| {272 let _ = mem::replace(pool, ptr);273 });274 }275}276277#[must_use]278pub fn intern_bytes(bytes: &[u8]) -> IBytes {279 POOL.with(|pool| {280 let mut pool = pool.borrow_mut();281 let entry = pool.raw_entry_mut().from_key(bytes);282 match entry {283 RawEntryMut::Occupied(i) => IBytes(i.get_key_value().0.clone()),284 RawEntryMut::Vacant(e) => {285 let (k, ()) = e.insert(Inner::new_bytes(bytes), ());286 IBytes(k.clone())287 }288 }289 })290}291292#[must_use]293pub fn intern_str(str: &str) -> IStr {294 // SAFETY: Rust strings always utf8295 unsafe { intern_bytes(str.as_bytes()).cast_str_unchecked() }296}297298#[cfg(test)]299mod tests {300 use crate::IStr;301302 #[test]303 fn simple() {304 let a = IStr::from("a");305 let b = IStr::from("a");306307 assert_eq!(a.as_ptr(), b.as_ptr());308 }309}crates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-ir-parser/src/lib.rs
+++ b/crates/jrsonnet-ir-parser/src/lib.rs
@@ -1038,7 +1038,7 @@
}
let e = expr(&mut p)?;
if !p.at_eof() {
- return Err(p.error(format!("expected end of file, got {}", p.current_desc(),)));
+ return Err(p.error(format!("expected end of file, got {}", p.current_desc())));
}
Ok(e)
}
@@ -1051,10 +1051,7 @@
#[cfg(test)]
mod tests {
- use std::fs;
-
- use insta::{assert_snapshot, glob};
- use jrsonnet_ir::{IStr, Source};
+ use insta::assert_snapshot;
use super::*;
@@ -1159,6 +1156,11 @@
#[test]
#[cfg(not(feature = "exp-null-coaelse"))]
fn peg_snapshots() {
+ use std::fs;
+
+ use insta::glob;
+ use jrsonnet_ir::{IStr, Source};
+
glob!("../../jrsonnet-peg-parser/src", "tests/*.jsonnet", |path| {
let input = fs::read_to_string(path).expect("read test file");
let source = Source::new_virtual("<test>".into(), IStr::empty());
crates/jrsonnet-peg-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-peg-parser/src/lib.rs
+++ b/crates/jrsonnet-peg-parser/src/lib.rs
@@ -433,16 +433,16 @@
#[cfg(test)]
mod tests {
- use std::fs;
+ #[test]
+ #[cfg(not(feature = "exp-null-coaelse"))]
+ fn snapshots() {
+ use std::fs;
- use insta::{assert_snapshot, glob};
- use jrsonnet_ir::{IStr, Source};
+ use insta::{assert_snapshot, glob};
+ use jrsonnet_ir::{IStr, Source};
- use crate::{ParserSettings, parse};
+ use crate::{ParserSettings, parse};
- #[test]
- #[cfg(not(feature = "exp-null-coaelse"))]
- fn snapshots() {
glob!("tests/*.jsonnet", |path| {
let input = fs::read_to_string(path).expect("read test file");
let v = parse(
tests/cpp_test_suite_golden_override/error.array_large_index.jsonnet.goldendiffbeforeafterboth--- a/tests/cpp_test_suite_golden_override/error.array_large_index.jsonnet.golden
+++ b/tests/cpp_test_suite_golden_override/error.array_large_index.jsonnet.golden
@@ -1 +1 @@
-array out of bounds: 4294967295 is not within [0,3)
\ No newline at end of file
+array out of bounds: 18446744073709552000 is not within [0,3)
\ No newline at end of file
tests/go_testdata_golden_override/string_index_negative.jsonnet.goldendiffbeforeafterboth--- a/tests/go_testdata_golden_override/string_index_negative.jsonnet.golden
+++ b/tests/go_testdata_golden_override/string_index_negative.jsonnet.golden
@@ -1 +1 @@
-array out of bounds: -1 is not within [0,4)
\ No newline at end of file
+string out of bounds: -1 is not within [0,4)
\ No newline at end of file