git.delta.rocks / jrsonnet / refs/commits / 36af281ff0ca

difftreelog

test pointer-size invariant snapshots

vqqnoupnYaroslav Bolyukin2026-05-06parent: #752087c.patch.diff
in: master

10 files changed

modifiedcrates/jrsonnet-evaluator/src/analyze.rsdiffbeforeafterboth
1959 }1959 }
1960}1960}
1961
1962#[cfg(test)]
1963fn render_diagnostics(src: &str, diags: &[Diagnostic]) -> String {
1964 use std::fmt::Write;
1965
1966 use hi_doc::{Formatting, SnippetBuilder, Text};
1967
1968 let mut out = String::new();
1969 let mut unspanned = Vec::new();
1970 let mut spanned: Vec<&Diagnostic> = Vec::new();
1971 for d in diags {
1972 if d.span.is_some() {
1973 spanned.push(d);
1974 } else {
1975 unspanned.push(d);
1976 }
1977 }
1978 if !spanned.is_empty() {
1979 let mut builder = SnippetBuilder::new(src);
1980 for d in spanned {
1981 let span = d.span.as_ref().expect("spanned");
1982 let ab = match d.level {
1983 DiagLevel::Error => {
1984 builder.error(Text::fragment(d.message.clone(), Formatting::default()))
1985 }
1986 DiagLevel::Warning => {
1987 builder.warning(Text::fragment(d.message.clone(), Formatting::default()))
1988 }
1989 };
1990 ab.range(span.range()).build();
1991 }
1992 out.push_str(&hi_doc::source_to_ansi(&builder.build()));
1993 }
1994 for d in unspanned {
1995 let prefix = match d.level {
1996 DiagLevel::Error => "error",
1997 DiagLevel::Warning => "warning",
1998 };
1999 writeln!(out, "{prefix}: {}", d.message).expect("fmt");
2000 }
2001 out
2002}
20031961
2004pub struct AnalysisReport {1962pub struct AnalysisReport {
2005 pub lir: LExpr,1963 pub lir: LExpr,
20111969
2012#[cfg(test)]1970#[cfg(test)]
2013mod tests {1971mod tests {
1972 #[test]
1973 #[cfg(not(feature = "exp-null-coaelse"))]
1974 fn snapshots() {
2014 use std::fs;1975 use std::fs;
20151976
2016 use insta::{assert_snapshot, glob};1977 use insta::{assert_snapshot, glob};
2017 use jrsonnet_ir::Source;1978 use jrsonnet_ir::Source;
20181979
2019 use super::*;1980 use super::*;
20201981
2021 #[test]1982 fn render_diagnostics(src: &str, diags: &[Diagnostic]) -> String {
2022 #[cfg(not(feature = "exp-null-coaelse"))]1983 use std::fmt::Write;
1984
1985 use hi_doc::{Formatting, SnippetBuilder, Text};
1986
1987 let mut out = String::new();
1988 let mut unspanned = Vec::new();
1989 let mut spanned: Vec<&Diagnostic> = Vec::new();
1990 for d in diags {
1991 if d.span.is_some() {
1992 spanned.push(d);
1993 } else {
1994 unspanned.push(d);
1995 }
1996 }
1997 if !spanned.is_empty() {
1998 let mut builder = SnippetBuilder::new(src);
1999 for d in spanned {
2000 let span = d.span.as_ref().expect("spanned");
2001 let ab = match d.level {
2002 DiagLevel::Error => {
2003 builder.error(Text::fragment(d.message.clone(), Formatting::default()))
2004 }
2005 DiagLevel::Warning => builder
2006 .warning(Text::fragment(d.message.clone(), Formatting::default())),
2007 };
2008 ab.range(span.range()).build();
2009 }
2010 out.push_str(&hi_doc::source_to_ansi(&builder.build()));
2011 }
2012 for d in unspanned {
2013 let prefix = match d.level {
2014 DiagLevel::Error => "error",
2015 DiagLevel::Warning => "warning",
2016 };
2017 writeln!(out, "{prefix}: {}", d.message).expect("fmt");
2018 }
2019 out
2020 }
2023 fn snapshots() {2021 fn fmt_depth(d: u32) -> String {
2022 if d == u32::MAX {
2023 "none".into()
2024 } else {
2025 d.to_string()
2026 }
2027 }
2028
2024 glob!("analysis_tests/*.jsonnet", |path| {2029 glob!("analysis_tests/*.jsonnet", |path| {
2025 let code = fs::read_to_string(path).expect("read test file");2030 let code = fs::read_to_string(path).expect("read test file");
2041 );2046 );
2042 assert_snapshot!(rendered);2047 assert_snapshot!(rendered);
2043 });2048 });
2044 }2049 }
2045
2046 fn fmt_depth(d: u32) -> String {
2047 if d == u32::MAX {
2048 "none".into()
2049 } else {
2050 d.to_string()
2051 }
2052 }
2053}2050}
20542051
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
123 EagerCompspecCaptured,123 EagerCompspecCaptured,
124124
125 #[error("array out of bounds: {0} is not within [0,{1})")]125 #[error("array out of bounds: {0} is not within [0,{1})")]
126 ArrayBoundsError(isize, u32),126 ArrayBoundsError(f64, u32),
127 #[error("string out of bounds: {0} is not within [0,{1})")]127 #[error("string out of bounds: {0} is not within [0,{1})")]
128 StringBoundsError(usize, usize),128 StringBoundsError(f64, u32),
129129
130 #[error("assert failed: {}", format_empty_str(.0))]130 #[error("assert failed: {}", format_empty_str(.0))]
131 AssertionFailed(IStr),131 AssertionFailed(IStr),
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
18 LIndexPart, LObjAsserts, LObjBody, LObjMembers, LSlot,18 LIndexPart, LObjAsserts, LObjBody, LObjMembers, LSlot,
19 },19 },
20 arr::ArrValue,20 arr::ArrValue,
21 bail, error,21 bail,
22 error::{ErrorKind::*, suggest_object_fields},22 error::{ErrorKind::*, suggest_object_fields},
23 evaluate::{destructure::fill_letrec_binds, operator::evaluate_unary_op},23 evaluate::{destructure::fill_letrec_binds, operator::evaluate_unary_op},
24 function::{CallLocation, FuncDesc, FuncVal, prepared::PreparedFuncVal},24 function::{CallLocation, FuncDesc, FuncVal, prepared::PreparedFuncVal},
25 in_frame,25 in_frame,
26 typed::FromUntyped as _,26 typed::{BoundedUsize, FromUntyped as _},
27 val::{CachedUnbound, Thunk},27 val::{CachedUnbound, Thunk},
28 with_state,28 with_state,
29};29};
193 }193 }
194 LExpr::ArrComp(comp) => evaluate_arr_comp(ctx, comp)?,194 LExpr::ArrComp(comp) => evaluate_arr_comp(ctx, comp)?,
195 LExpr::Slice(slice) => {195 LExpr::Slice(slice) => {
196 use crate::typed::BoundedUsize;
197 let val = evaluate(ctx.clone(), &slice.value)?;196 let val = evaluate(ctx.clone(), &slice.value)?;
198 let indexable = val.into_indexable()?;197 let indexable = val.into_indexable()?;
199 let start = slice198 let start = slice
200 .start199 .start
201 .as_ref()200 .as_ref()
202 .map(|e| evaluate(ctx.clone(), e))201 .map(|e| evaluate(ctx.clone(), e))
203 .transpose()?202 .transpose()?
204 .map(|v| -> Result<i32> {203 .map(|v| -> Result<i32> { i32::from_untyped(v).description("slice start value") })
205 v.as_num()
206 .ok_or_else(|| {
207 TypeMismatch("slice start", vec![ValType::Num], v.value_type()).into()
208 })
209 .map(|n| n as i32)
210 })
211 .transpose()?;204 .transpose()?;
212 let end = slice205 let end = slice
213 .end206 .end
214 .as_ref()207 .as_ref()
215 .map(|e| evaluate(ctx.clone(), e))208 .map(|e| evaluate(ctx.clone(), e))
216 .transpose()?209 .transpose()?
217 .map(|v| -> Result<i32> {210 .map(|v| -> Result<i32> { i32::from_untyped(v).description("slice end value") })
218 v.as_num()
219 .ok_or_else(|| {
220 TypeMismatch("slice end", vec![ValType::Num], v.value_type()).into()
221 })
222 .map(|n| n as i32)
223 })
224 .transpose()?;211 .transpose()?;
225 let step = slice212 let step = slice
228 .map(|e| evaluate(ctx, e))215 .map(|e| evaluate(ctx, e))
229 .transpose()?216 .transpose()?
230 .map(|v| -> Result<BoundedUsize<1, { i32::MAX as usize }>> {217 .map(|v| -> Result<BoundedUsize<1, { i32::MAX as usize }>> {
231 let n = v.as_num().ok_or_else(|| -> crate::Error {
232 TypeMismatch("slice step", vec![ValType::Num], v.value_type()).into()
233 })?;
234 BoundedUsize::new(n as usize).ok_or_else(|| error!("slice step must be >= 1"))218 BoundedUsize::from_untyped(v).description("slice step value")
235 })219 })
236 .transpose()?;220 .transpose()?;
237 Val::from(indexable.slice(start, end, step)?)221 Val::from(indexable.slice(start, end, step)?)
410 if n.fract() > f64::EPSILON {394 if n.fract() > f64::EPSILON {
411 bail!(FractionalIndex)395 bail!(FractionalIndex)
412 }396 }
397 let len = arr.len();
413 if n < 0.0 {398 if n < 0.0 || n > f64::from(len) {
414 bail!(ArrayBoundsError(399 bail!(ArrayBoundsError(n, len));
415 n as isize, // truncation is fine for error display
416 arr.len()
417 ));
418 }400 }
419 #[expect(401 #[expect(
424 let i = n as u32;406 let i = n as u32;
425 arr.get(i)407 arr.get(i)
426 .with_description_src(loc, || format!("element <{i}> access"))?408 .with_description_src(loc, || format!("element <{i}> access"))?
427 .ok_or_else(|| ArrayBoundsError(i as isize, arr.len()))?409 .ok_or_else(|| ArrayBoundsError(n, len))?
428 }410 }
429 (Val::Str(s), Val::Num(idx)) => {411 (Val::Str(s), Val::Num(idx)) => {
430 let n = idx.get();412 let n = idx.get();
431 if n.fract() > f64::EPSILON {413 if n.fract() > f64::EPSILON {
432 bail!(FractionalIndex)414 bail!(FractionalIndex)
433 }415 }
434 let flat = s.clone().into_flat();
435 if n < 0.0 {
436 bail!(ArrayBoundsError(
437 n as isize, // truncation is fine for error display
438 flat.chars().count() as u32
439 ));
440 }
441 #[expect(416 #[expect(
442 clippy::cast_possible_truncation,417 clippy::cast_possible_truncation,
443 clippy::cast_sign_loss,418 clippy::cast_sign_loss,
444 reason = "n is checked positive, overflow will truncate as expected"419 reason = "n is checked positive, overflow will truncate as expected"
445 )]420 )]
446 let i = n as usize;421 let i = n as usize;
422 let flat = s.clone().into_flat();
423 #[allow(clippy::cast_possible_truncation, reason = "string is max 4g")]
424 if n >= 0.0
425 && n <= f64::from(u32::MAX)
447 let Some(char) = flat.chars().nth(i) else {426 && let Some(char) = flat.chars().nth(i)
427 {
428 Val::string(char)
429 } else {
448 bail!(StringBoundsError(i, flat.chars().count()))430 let len = flat.chars().count();
431 bail!(StringBoundsError(n, len as u32))
449 };432 }
450 Val::string(char)
451 }433 }
452 #[cfg(feature = "exp-null-coaelse")]434 #[cfg(feature = "exp-null-coaelse")]
453 (Val::Null, _) if part.null_coaelse => return Ok(Val::Null),435 (Val::Null, _) if part.null_coaelse => return Ok(Val::Null),
566 let a_ctx = ctx548 let a_ctx = ctx
567 .pack_captures_sup_this(&members.frame_shape)549 .pack_captures_sup_this(&members.frame_shape)
568 .enter(|fill, ctx| {550 .enter(|fill, ctx| {
569 fill_letrec_binds(fill, &ctx, &members.locals);551 fill_letrec_binds(fill, ctx, &members.locals);
570 });552 });
571 for field in &members.fields {553 for field in &members.fields {
572 evaluate_field_member_static(&mut builder, ctx.clone(), a_ctx.clone(), field)?;554 evaluate_field_member_static(&mut builder, ctx.clone(), a_ctx.clone(), field)?;
modifiedcrates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth
157 Self::BoundedNumber(from, to) => {157 Self::BoundedNumber(from, to) => {
158 if let Val::Num(n) = value {158 if let Val::Num(n) = value {
159 let n = n.get();159 let n = n.get();
160 if from.map(|from| from > n).unwrap_or(false)160 if from.is_some_and(|from| from > n) || to.is_some_and(|to| to < n) {
161 || to.map(|to| to < n).unwrap_or(false)
162 {
163 return Err(TypeError::BoundsFailed(n, *from, *to).into());161 return Err(TypeError::BoundsFailed(n, *from, *to).into());
164 }162 }
modifiedcrates/jrsonnet-interner/src/inner.rsdiffbeforeafterboth
162 unsafe { (*header).refcnt() }162 unsafe { (*header).refcnt() }
163 }163 }
164
165 pub fn len32(&self) -> u32 {
166 let header = Self::header(self);
167 // SAFETY: header is initialized
168 unsafe { (*header).size }
169 }
164}170}
165171
166impl Clone for Inner {172impl Clone for Inner {
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
54 IBytes(self.0.clone())54 IBytes(self.0.clone())
55 }55 }
56
57 pub fn len32(&self) -> u32 {
58 self.0.len32()
59 }
56}60}
5761
58impl Deref for IStr {62impl Deref for IStr {
modifiedcrates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth
1038 }1038 }
1039 let e = expr(&mut p)?;1039 let e = expr(&mut p)?;
1040 if !p.at_eof() {1040 if !p.at_eof() {
1041 return Err(p.error(format!("expected end of file, got {}", p.current_desc(),)));1041 return Err(p.error(format!("expected end of file, got {}", p.current_desc())));
1042 }1042 }
1043 Ok(e)1043 Ok(e)
1044}1044}
10511051
1052#[cfg(test)]1052#[cfg(test)]
1053mod tests {1053mod tests {
1054 use std::fs;1054 use insta::assert_snapshot;
10551055
1056 use insta::{assert_snapshot, glob};
1057 use jrsonnet_ir::{IStr, Source};
1058
1059 use super::*;1056 use super::*;
10601057
1061 fn parse_str(input: &str) -> Expr {1058 fn parse_str(input: &str) -> Expr {
1159 #[test]1156 #[test]
1160 #[cfg(not(feature = "exp-null-coaelse"))]1157 #[cfg(not(feature = "exp-null-coaelse"))]
1161 fn peg_snapshots() {1158 fn peg_snapshots() {
1159 use std::fs;
1160
1161 use insta::glob;
1162 use jrsonnet_ir::{IStr, Source};
1163
1162 glob!("../../jrsonnet-peg-parser/src", "tests/*.jsonnet", |path| {1164 glob!("../../jrsonnet-peg-parser/src", "tests/*.jsonnet", |path| {
1163 let input = fs::read_to_string(path).expect("read test file");1165 let input = fs::read_to_string(path).expect("read test file");
1164 let source = Source::new_virtual("<test>".into(), IStr::empty());1166 let source = Source::new_virtual("<test>".into(), IStr::empty());
modifiedcrates/jrsonnet-peg-parser/src/lib.rsdiffbeforeafterboth
433433
434#[cfg(test)]434#[cfg(test)]
435mod tests {435mod tests {
436 #[test]
437 #[cfg(not(feature = "exp-null-coaelse"))]
438 fn snapshots() {
436 use std::fs;439 use std::fs;
437440
438 use insta::{assert_snapshot, glob};441 use insta::{assert_snapshot, glob};
439 use jrsonnet_ir::{IStr, Source};442 use jrsonnet_ir::{IStr, Source};
440443
441 use crate::{ParserSettings, parse};444 use crate::{ParserSettings, parse};
442445
443 #[test]
444 #[cfg(not(feature = "exp-null-coaelse"))]
445 fn snapshots() {
446 glob!("tests/*.jsonnet", |path| {446 glob!("tests/*.jsonnet", |path| {
447 let input = fs::read_to_string(path).expect("read test file");447 let input = fs::read_to_string(path).expect("read test file");
448 let v = parse(448 let v = parse(
455 let v = format!("{v:#?}");455 let v = format!("{v:#?}");
456 assert_snapshot!(v);456 assert_snapshot!(v);
457 });457 });
458 }458 }
459}459}
460460
modifiedtests/cpp_test_suite_golden_override/error.array_large_index.jsonnet.goldendiffbeforeafterboth
1array out of bounds: 4294967295 is not within [0,3)1array out of bounds: 18446744073709552000 is not within [0,3)
modifiedtests/go_testdata_golden_override/string_index_negative.jsonnet.goldendiffbeforeafterboth
1array out of bounds: -1 is not within [0,4)1string out of bounds: -1 is not within [0,4)