1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! All types related to inserting one [`Kind`] into another.
use crate::path::{BorrowedSegment, ValuePath};
use crate::value::Kind;
use crate::value::kind::Collection;
impl Kind {
/// Insert the `Kind` at the given `path` within `self`.
/// This has the same behavior as `Value::insert`.
#[allow(clippy::needless_pass_by_value)] // only reference types implement Path
pub fn insert<'a>(&mut self, path: impl ValuePath<'a>, kind: Self) {
self.insert_recursive(path.segment_iter(), kind.upgrade_undefined());
}
/// Set the `Kind` at the given `path` within `self`.
/// There is a subtle difference
/// between this and `Kind::insert` where this function does _not_ convert undefined to null.
#[allow(clippy::needless_pass_by_value)] // only reference types implement Path
pub fn set_at_path<'a>(&mut self, path: impl ValuePath<'a>, kind: Self) {
self.insert_recursive(path.segment_iter(), kind);
}
/// Insert the `Kind` at the given `path` within `self`.
/// This has the same behavior as `Value::insert`.
///
/// # Panics
/// Object/Array not present in `self`.
#[allow(clippy::too_many_lines)]
#[allow(clippy::needless_pass_by_value)] // only reference types implement Path
pub fn insert_recursive<'a, 'b>(
&'a mut self,
mut iter: impl Iterator<Item = BorrowedSegment<'b>> + Clone,
kind: Self,
) {
if kind.is_never() {
// If `kind` is `never`, the program would have already terminated
// so this assignment can't happen.
return;
}
if let Some(segment) = iter.next() {
match segment {
BorrowedSegment::Field(field) => {
// Field insertion converts the value to an object, so remove all other types.
*self = Self::object(self.object.clone().unwrap_or_else(Collection::empty));
let collection = self.object.as_mut().expect("object was just inserted");
let unknown_kind = collection.unknown_kind();
collection
.known_mut()
.entry(field.into_owned().into())
.or_insert(unknown_kind)
.insert_recursive(iter, kind);
}
BorrowedSegment::Index(mut index) => {
// Array insertion converts the value to an array, so remove all other types.
*self = Self::array(self.array.clone().unwrap_or_else(Collection::empty));
let collection = self.array.as_mut().expect("array was just inserted");
if index < 0 {
let largest_known_index = collection.largest_known_index();
// The minimum size of the resulting array.
let len_required = -index as usize;
let unknown_kind = collection.unknown_kind();
if unknown_kind.contains_any_defined() {
// The array may be larger, but this is the largest we can prove the array is from the type information.
let min_length = collection.min_length();
if len_required > min_length {
// We can't prove the array is large enough, so "holes" may be created
// which set the value to null.
// Holes are inserted to the front, which shifts everything to the right.
// We don't know the exact number of holes/shifts, but can determine an upper bound.
let max_shifts = len_required - min_length;
// The number of possible shifts is 0 ..= max_shifts.
// Each shift will be calculated independently and merged into the collection.
// A shift of 0 is the original collection, so that is skipped.
let zero_shifts = collection.clone();
for shift_count in 1..=max_shifts {
let mut shifted_collection = zero_shifts.clone();
// Clear all known values and replace with new ones. (in-place shift can overwrite).
shifted_collection.known_mut().clear();
// Add the "null" from holes.
for i in 1..shift_count {
shifted_collection
.known_mut()
.insert(i.into(), Self::null());
}
// Shift known values by the exact "shift_count".
for (i, i_kind) in zero_shifts.known() {
shifted_collection
.known_mut()
.insert(*i + shift_count, i_kind.clone());
}
// Add this shift count as another possible type definition.
collection.merge(shifted_collection, false);
}
}
// We can prove the positive index won't be less than "min_index".
let min_index = (min_length as isize + index).max(0) as usize;
// Sanity check: if holes are added to the type, min_index must be 0.
debug_assert!(min_index == 0 || min_length >= len_required);
// Apply the current "unknown" to indices that don't have an explicit known
// since the "unknown" is about to change.
for i in 0..len_required {
collection
.known_mut()
.entry(i.into())
.or_insert_with(|| unknown_kind.clone())
// These indices are guaranteed to exist, so they can't be undefined.
.remove_undefined();
}
for (i, i_kind) in collection.known_mut() {
// This index might be set by the insertion. Add the insertion type to the existing type.
if i.to_usize() >= min_index {
let mut kind_with_insertion = i_kind.clone();
let remaining_path_segments = iter.clone().collect::<Vec<_>>();
kind_with_insertion
.insert(&remaining_path_segments, kind.clone());
*i_kind = i_kind.union(kind_with_insertion);
}
}
let mut unknown_kind_with_insertion = unknown_kind.clone();
let remaining_path_segments = iter.clone().collect::<Vec<_>>();
unknown_kind_with_insertion.insert(&remaining_path_segments, kind);
let mut new_unknown_kind = unknown_kind;
new_unknown_kind.merge_keep(unknown_kind_with_insertion, false);
collection.set_unknown(new_unknown_kind);
return;
}
debug_assert!(
collection.unknown_kind().is_undefined(),
"all cases with an unknown have been handled"
);
// If there is no unknown, the exact position of the negative index can be determined.
let exact_array_len =
largest_known_index.map_or(0, |max_index| max_index + 1);
if len_required > exact_array_len {
// Fill in holes from extending to fit a negative index.
for i in exact_array_len..len_required {
// There is no unknown, so the exact type "null" can be inserted.
collection.known_mut().insert(i.into(), Self::null());
}
}
index += (len_required as isize).max(exact_array_len as isize);
}
debug_assert!(index >= 0, "all negative cases have been handled");
let index = index as usize;
let index_exists = collection.known().contains_key(&index.into());
if !index_exists {
// Add "null" to all holes, adding it to the "unknown" if it exists.
// Holes can never be undefined.
let hole_type = collection.unknown_kind().without_undefined().or_null();
for i in 0..index {
collection
.known_mut()
.entry(i.into())
.or_insert_with(|| hole_type.clone());
}
}
let unknown_kind = collection.unknown_kind();
collection
.known_mut()
.entry(index.into())
.or_insert(unknown_kind)
.insert_recursive(iter, kind);
}
BorrowedSegment::Invalid => { /* An invalid path does nothing. */ }
}
} else {
*self = kind;
}
}
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use crate::owned_value_path;
use crate::path::{OwnedValuePath, parse_value_path};
use crate::value::kind::Collection;
use super::*;
#[test]
#[allow(clippy::too_many_lines)]
fn test_insert() {
struct TestCase {
this: Kind,
path: OwnedValuePath,
kind: Kind,
expected: Kind,
}
for (
title,
TestCase {
mut this,
path,
kind,
expected,
},
) in [
(
"root insert",
TestCase {
this: Kind::bytes(),
path: owned_value_path!(),
kind: Kind::integer(),
expected: Kind::integer(),
},
),
(
"root insert object",
TestCase {
this: Kind::bytes(),
path: owned_value_path!(),
kind: Kind::object(BTreeMap::from([("a".into(), Kind::integer())])),
expected: Kind::object(BTreeMap::from([("a".into(), Kind::integer())])),
},
),
(
"empty object insert field",
TestCase {
this: Kind::object(Collection::empty()),
path: owned_value_path!("a"),
kind: Kind::integer(),
expected: Kind::object(BTreeMap::from([("a".into(), Kind::integer())])),
},
),
(
"non-empty object insert field",
TestCase {
this: Kind::object(BTreeMap::from([("b".into(), Kind::bytes())])),
path: owned_value_path!("a"),
kind: Kind::integer(),
expected: Kind::object(BTreeMap::from([
("a".into(), Kind::integer()),
("b".into(), Kind::bytes()),
])),
},
),
(
"object overwrite field",
TestCase {
this: Kind::object(BTreeMap::from([("a".into(), Kind::bytes())])),
path: owned_value_path!("a"),
kind: Kind::integer(),
expected: Kind::object(BTreeMap::from([("a".into(), Kind::integer())])),
},
),
(
"set array index on empty array",
TestCase {
this: Kind::array(Collection::empty()),
path: owned_value_path!(0),
kind: Kind::integer(),
expected: Kind::array(BTreeMap::from([(0.into(), Kind::integer())])),
},
),
(
"set array index past the end without unknown",
TestCase {
this: Kind::array(Collection::empty()),
path: owned_value_path!(1),
kind: Kind::integer(),
expected: Kind::array(BTreeMap::from([
(0.into(), Kind::null()),
(1.into(), Kind::integer()),
])),
},
),
(
"set array index past the end with unknown",
TestCase {
this: Kind::array(Collection::empty().with_unknown(Kind::integer())),
path: owned_value_path!(1),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([
(0.into(), Kind::integer().or_null()),
(1.into(), Kind::bytes()),
]))
.with_unknown(Kind::integer()),
),
},
),
(
"set array index past the end with unknown, nested",
TestCase {
this: Kind::array(Collection::empty().with_unknown(Kind::integer())),
path: owned_value_path!(1, "foo"),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([
(0.into(), Kind::integer().or_null()),
(
1.into(),
Kind::object(BTreeMap::from([("foo".into(), Kind::bytes())])),
),
]))
.with_unknown(Kind::integer()),
),
},
),
(
"set array index past the end with null unknown",
TestCase {
this: Kind::array(Collection::empty().with_unknown(Kind::null())),
path: owned_value_path!(1),
kind: Kind::integer(),
expected: Kind::array(
Collection::from(BTreeMap::from([
(0.into(), Kind::null()),
(1.into(), Kind::integer()),
]))
.with_unknown(Kind::null()),
),
},
),
(
"set field on non-object",
TestCase {
this: Kind::integer(),
path: owned_value_path!("a"),
kind: Kind::integer(),
expected: Kind::object(BTreeMap::from([("a".into(), Kind::integer())])),
},
),
(
"set array index on non-array",
TestCase {
this: Kind::integer(),
path: owned_value_path!(0),
kind: Kind::integer(),
expected: Kind::array(BTreeMap::from([(0.into(), Kind::integer())])),
},
),
(
"set negative array index (no unknown)",
TestCase {
this: Kind::array(BTreeMap::from([
(0.into(), Kind::integer()),
(1.into(), Kind::integer()),
])),
path: owned_value_path!(-1),
kind: Kind::bytes(),
expected: Kind::array(BTreeMap::from([
(0.into(), Kind::integer()),
(1.into(), Kind::bytes()),
])),
},
),
(
"set negative array index past the end (no unknown)",
TestCase {
this: Kind::array(BTreeMap::from([(0.into(), Kind::integer())])),
path: owned_value_path!(-2),
kind: Kind::bytes(),
expected: Kind::array(BTreeMap::from([
(0.into(), Kind::bytes()),
(1.into(), Kind::null()),
])),
},
),
(
"set negative array index size 1 unknown array",
TestCase {
this: Kind::array(
Collection::from(BTreeMap::from([(0.into(), Kind::integer())]))
.with_unknown(Kind::integer()),
),
path: owned_value_path!(-1),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([(0.into(), Kind::bytes().or_integer())]))
.with_unknown(Kind::integer().or_bytes().or_undefined()),
),
},
),
(
"set negative array index empty unknown array",
TestCase {
this: Kind::array(Collection::empty().with_unknown(Kind::integer())),
path: owned_value_path!(-1),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([
// we can prove the first index will not be undefined
(0.into(), Kind::bytes().or_integer()),
]))
.with_unknown(Kind::integer().or_bytes().or_undefined()),
),
},
),
(
"set negative array index empty unknown array (2)",
TestCase {
this: Kind::array(Collection::empty().with_unknown(Kind::integer())),
path: owned_value_path!(-2),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([
(0.into(), Kind::integer().or_bytes()),
// This is the only location a hole could potentially be inserted, so it
// is the only index that gets "null", rather than adding it to the
// entire unknown type.
(1.into(), Kind::integer().or_bytes().or_null()),
]))
.with_unknown(Kind::integer().or_bytes().or_undefined()),
),
},
),
(
"set negative array index unknown array",
TestCase {
this: Kind::array(
Collection::from(BTreeMap::from([
// This would be an invalid type without index 0 (it can't be undefined).
(0.into(), Kind::integer()),
(1.into(), Kind::float()),
]))
.with_unknown(Kind::integer()),
),
path: owned_value_path!(-3),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([
// Either the unknown (integer) or the inserted value, depending on the actual length.
(0.into(), Kind::integer().or_bytes()),
// The original float if it wasn't shifted, or bytes/integer if it was shifted.
// Can't be a hole.
(1.into(), Kind::float().or_bytes().or_integer()),
(2.into(), Kind::float().or_bytes().or_integer()),
]))
.with_unknown(Kind::integer().or_bytes().or_undefined()),
),
},
),
(
"set negative array index unknown array no holes",
TestCase {
this: Kind::array(
Collection::from(BTreeMap::from([
(0.into(), Kind::float()),
(1.into(), Kind::float()),
(2.into(), Kind::float()),
]))
.with_unknown(Kind::integer()),
),
path: owned_value_path!(-3),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([
(0.into(), Kind::float().or_bytes()),
(1.into(), Kind::float().or_bytes()),
(2.into(), Kind::float().or_bytes()),
]))
.with_unknown(Kind::integer().or_bytes().or_undefined()),
),
},
),
(
"set negative array index on non-array",
TestCase {
this: Kind::integer(),
path: owned_value_path!(-3),
kind: Kind::bytes(),
expected: Kind::array(Collection::from(BTreeMap::from([
(0.into(), Kind::bytes()),
(1.into(), Kind::null()),
(2.into(), Kind::null()),
]))),
},
),
(
"set nested negative array index on unknown array",
TestCase {
this: Kind::array(Collection::empty().with_unknown(Kind::integer())),
path: owned_value_path!(-3, "foo"),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([
(
0.into(),
Kind::integer()
.or_object(BTreeMap::from([("foo".into(), Kind::bytes())])),
),
(
1.into(),
Kind::integer()
.or_null()
.or_object(BTreeMap::from([("foo".into(), Kind::bytes())])),
),
(
2.into(),
Kind::integer()
.or_null()
.or_object(BTreeMap::from([("foo".into(), Kind::bytes())])),
),
]))
.with_unknown(
Kind::integer()
.or_undefined()
.or_object(BTreeMap::from([("foo".into(), Kind::bytes())])),
),
),
},
),
(
"set nested negative array index on unknown array (no holes)",
TestCase {
this: Kind::array(
Collection::from(BTreeMap::from([(0.into(), Kind::integer())]))
.with_unknown(Kind::integer()),
),
path: owned_value_path!(-1, "foo"),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([(
0.into(),
Kind::integer()
.or_object(BTreeMap::from([("foo".into(), Kind::bytes())])),
)]))
.with_unknown(
Kind::integer()
.or_undefined()
.or_object(BTreeMap::from([("foo".into(), Kind::bytes())])),
),
),
},
),
(
"insert into never",
TestCase {
this: Kind::never(),
path: parse_value_path(".").unwrap(),
kind: Kind::bytes(),
expected: Kind::bytes(),
},
),
(
"insert never",
TestCase {
this: Kind::object(Collection::empty()),
path: parse_value_path(".x").unwrap(),
kind: Kind::never(),
expected: Kind::object(Collection::empty()),
},
),
(
"insert undefined",
TestCase {
this: Kind::object(Collection::empty()),
path: parse_value_path(".x").unwrap(),
kind: Kind::undefined(),
expected: Kind::object(BTreeMap::from([("x".into(), Kind::null())])),
},
),
(
"array insert into any",
TestCase {
this: Kind::any(),
path: owned_value_path!(2),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([
(0.into(), Kind::any().without_undefined()),
(1.into(), Kind::any().without_undefined()),
(2.into(), Kind::bytes()),
]))
.with_unknown(Kind::any()),
),
},
),
(
"object insert into any",
TestCase {
this: Kind::any(),
path: owned_value_path!("b"),
kind: Kind::bytes(),
expected: Kind::object(
Collection::from(BTreeMap::from([("b".into(), Kind::bytes())]))
.with_unknown(Kind::any()),
),
},
),
(
"nested object/array insert into any",
TestCase {
this: Kind::any(),
path: owned_value_path!("x", 2),
kind: Kind::bytes(),
expected: Kind::object(
Collection::from(BTreeMap::from([(
"x".into(),
Kind::array(
Collection::from(BTreeMap::from([
(0.into(), Kind::any().without_undefined()),
(1.into(), Kind::any().without_undefined()),
(2.into(), Kind::bytes()),
]))
.with_unknown(Kind::any()),
),
)]))
.with_unknown(Kind::any()),
),
},
),
(
"nested array/array insert into any",
TestCase {
this: Kind::any(),
path: owned_value_path!(0, 0),
kind: Kind::bytes(),
expected: Kind::array(
Collection::from(BTreeMap::from([(
0.into(),
Kind::array(
Collection::from(BTreeMap::from([(0.into(), Kind::bytes())]))
.with_unknown(Kind::any()),
),
)]))
.with_unknown(Kind::any()),
),
},
),
] {
this.insert(&path, kind);
assert_eq!(this, expected, "{title}");
}
}
}