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.rsdiffbeforeafterboth1use std::{2 alloc::{self, Layout},3 borrow::Borrow,4 cell::UnsafeCell,5 cmp,6 hash::{Hash, Hasher},7 mem,8 ptr::{self, NonNull},9 slice, str,10};1112const UTF8_MASK: u32 = 1 << 31;13const REFCNT_MASK: u32 = !UTF8_MASK;1415#[repr(C)]16struct InnerHeader {17 size: u32,18 // MSB is checked utf8 flag, rest - refcnt19 utf8_refcnt: u32,20}21impl InnerHeader {22 const fn new(size: u32, is_utf8: bool) -> Self {23 Self {24 size,25 utf8_refcnt: 1 | (if is_utf8 { UTF8_MASK } else { 0 }),26 }27 }2829 const fn refcnt(&self) -> u32 {30 self.utf8_refcnt & REFCNT_MASK31 }32 const fn is_utf8(&self) -> bool {33 self.utf8_refcnt & UTF8_MASK != 034 }3536 fn set_refcnt(&mut self, cnt: u32) {37 assert_eq!(cnt & UTF8_MASK, 0);38 // Reset all bits expect last39 self.utf8_refcnt &= UTF8_MASK;40 // Store refcnt41 self.utf8_refcnt |= cnt;42 }43 fn set_is_utf8(&mut self) {44 self.utf8_refcnt |= UTF8_MASK;45 }46}4748/// Similar to Rc<[u8]>, but stores all data (refcnt, size) inline, instead of being DST49pub struct Inner(UnsafeCell<NonNull<InnerHeader>>);50impl Inner {51 /// # Safety52 /// `is_utf8` should only be set if data is really checked to be utf853 /// # Panics54 /// If data is larger than 4GB55 // we allocate with correct alignment56 #[allow(clippy::cast_ptr_alignment)]57 unsafe fn new_raw(bytes: &[u8], is_utf8: bool) -> Self {58 // SAFETY:59 // - layout has non-zero size, and correct align60 // - data is written right after allocation61 // - new allocation can't overlap with passed slice62 unsafe {63 let data: *mut InnerHeader = alloc::alloc(Layout::from_size_align_unchecked(64 mem::size_of::<InnerHeader>() + bytes.len(),65 mem::align_of::<InnerHeader>(),66 ))67 .cast();68 assert!(!data.is_null());69 *data = InnerHeader::new(bytes.len().try_into().expect("bytes > 4GB"), is_utf8);70 ptr::copy_nonoverlapping(bytes.as_ptr(), data.add(1).cast::<u8>(), bytes.len());71 Self(UnsafeCell::new(NonNull::new_unchecked(data)))72 }73 }74 pub fn new_bytes(bytes: &[u8]) -> Self {75 // SAFETY: is_utf8 is not set76 unsafe { Self::new_raw(bytes, false) }77 }78 #[allow(dead_code)]79 pub fn new_str(str: &str) -> Self {80 // SAFETY: strings always utf881 unsafe { Self::new_raw(str.as_bytes(), true) }82 }8384 // `slice::from_raw_parts` is not yet stabilized85 #[allow(clippy::missing_const_for_fn)]86 pub fn as_slice(&self) -> &[u8] {87 let header = Self::header(self);88 // SAFETY: data is not null, and it is correctly initialized89 let size = unsafe { (*header).size };90 // SAFETY: bytes after data is allocated to be exactly data.size in length91 unsafe {92 slice::from_raw_parts((*self.0.get()).as_ptr().add(1).cast::<u8>(), size as usize)93 }94 }9596 /// # Safety97 /// Data should be checked to be utf8 via [`check_utf8`] first98 pub unsafe fn as_str_unchecked(&self) -> &str {99 // SAFETY: data is checked100 unsafe { str::from_utf8_unchecked(self.as_slice()) }101 }102103 /// Check data to be utf-8104 ///105 /// Positive results are cached106 pub fn check_utf8(this: &Self) -> bool {107 let header = Self::header_mut(this);108 // SAFETY: header is initialized109 if unsafe { (*header).is_utf8() } {110 return true;111 }112113 if str::from_utf8(this.as_slice()).is_ok() {114 // SAFETY: header is initialized115 unsafe { (*header).set_is_utf8() };116 true117 } else {118 false119 }120 }121122 /// Marks data as utf-8123 ///124 /// # Safety125 /// data should be really utf-8126 pub unsafe fn assume_utf8(this: &Self) {127 let header = Self::header_mut(this);128 // SAFETY: header is correct129 unsafe { (*header).set_is_utf8() }130 }131132 fn header(this: &Self) -> *const InnerHeader {133 // Safety: in `new`, we allocate with correct alignment134 unsafe { (*this.0.get()).as_ptr() }135 }136 fn header_mut(this: &Self) -> *mut InnerHeader {137 // Safety: in `new`, we allocate with correct alignment138 unsafe { (*this.0.get()).as_ptr() }139 }140141 fn clone(this: &Self) -> Self {142 let header = Self::header_mut(this);143 // SAFETY: header is initialized144 unsafe {145 let refcnt = (*header).refcnt() + 1;146 (*header).set_refcnt(refcnt);147 Self(UnsafeCell::new(*this.0.get()))148 }149 }150151 pub fn ptr_eq(a: &Self, b: &Self) -> bool {152 Self::as_ptr(a) == Self::as_ptr(b)153 }154 pub fn as_ptr(this: &Self) -> *const u8 {155 // SAFETY: data is initialized156 unsafe { (*this.0.get()).as_ptr().add(1).cast() }157 }158159 pub fn strong_count(this: &Self) -> u32 {160 let header = Self::header(this);161 // SAFETY: header is initialized162 unsafe { (*header).refcnt() }163 }164165 pub fn len32(&self) -> u32 {166 let header = Self::header(self);167 // SAFETY: header is initialized168 unsafe { (*header).size }169 }170}171172impl Clone for Inner {173 fn clone(&self) -> Self {174 Self::clone(self)175 }176}177178impl Drop for Inner {179 fn drop(&mut self) {180 #[cold]181 #[inline(never)]182 fn dealloc(val: &Inner) {183 let header = Inner::header_mut(val);184 // Safety: Data is valid yet185 let size = unsafe { (*header).size as usize };186 // SAFETY: size is correct, layout is valid, data will not be used after this, as refcn == 0187 unsafe {188 alloc::dealloc(189 header.cast(),190 Layout::from_size_align_unchecked(191 mem::size_of::<InnerHeader>() + size,192 mem::align_of::<InnerHeader>(),193 ),194 );195 }196 }197 let header = Self::header_mut(self);198 // SAFETY: header is initialized199 let refcnt = unsafe {200 let refcnt = (*header).refcnt() - 1;201 (*header).set_refcnt(refcnt);202 refcnt203 };204 if refcnt == 0 {205 dealloc(self);206 }207 }208}209210impl PartialEq for Inner {211 fn eq(&self, other: &Self) -> bool {212 Self::as_ptr(self) == Self::as_ptr(other) || self.as_slice().eq(other.as_slice())213 }214}215impl Hash for Inner {216 fn hash<H: Hasher>(&self, state: &mut H) {217 self.as_slice().hash(state);218 }219}220impl Eq for Inner {}221impl PartialOrd for Inner {222 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {223 Some(self.cmp(other))224 }225}226impl Ord for Inner {227 fn cmp(&self, other: &Self) -> cmp::Ordering {228 self.as_slice().cmp(other.as_slice())229 }230}231232impl Borrow<[u8]> for Inner {233 fn borrow(&self) -> &[u8] {234 self.as_slice()235 }236}crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -53,6 +53,10 @@
pub fn cast_bytes(self) -> IBytes {
IBytes(self.0.clone())
}
+
+ pub fn len32(&self) -> u32 {
+ self.0.len32()
+ }
}
impl Deref for IStr {
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