tvm_tirx/sym/
analyzer.rs

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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
#![allow(dead_code, unused_imports)]

//! The analyzer, its bound and modular results, and the forms of its simplifiers.
//!
//! The objects `python/tvm_tirx/sym/analyzer.py` defines. An [`Analyzer`] keeps the facts the
//! native analyses share. A [`ConstIntBound`] is an inclusive range `[min_value, max_value]`, and a
//! [`ModularSet`] is the set `coeff * x + base_` for any integer `x`.

// Python has no class for `CanonicalExpr`, `SumExpr` and `SplitExpr`, the forms of the canonical
// simplifier, or for the `RewriteSimplifierStats` that `Analyzer.rewrite_simplify_stats()`
// returns; they are here, with the analyzer they belong to.

// Names the hand-written wrappers use: the import section keeps them even when no generated block
// does.
// tvm-ffi-stubgen(import-object): tvm_ffi::Error
// tvm-ffi-stubgen(import-object): tvm_ffi::Map
// tvm-ffi-stubgen(import-object): tvm_ffi::Result
// tvm-ffi-stubgen(import-object): tvm_ffi::String
// tvm-ffi-stubgen(import-object): tvm_ffi::VALUE_ERROR
// tvm-ffi-stubgen(import-object): crate::ExitOnDrop
// tvm-ffi-stubgen(import-object): crate::ir::expr::Range
// tvm-ffi-stubgen(import-object): crate::ir::expr::Var
// tvm-ffi-stubgen(import-object): crate::sym::int_set::IntSet
// tvm-ffi-stubgen(import-object): crate::ir::support
// tvm-ffi-stubgen(import-object): crate::ir::expr::IntoExpr

// tvm-ffi-stubgen(begin): import-section
use crate::ExitOnDrop;
use crate::ir::expr::Expr;
use crate::ir::expr::ExprObj;
use crate::ir::expr::IntoExpr;
use crate::ir::expr::Range;
use crate::ir::expr::Var;
use crate::ir::support;
use crate::sym::int_set::IntSet;
use std::ops::Deref;
use tvm_ffi::Error;
use tvm_ffi::Map;
use tvm_ffi::Object;
use tvm_ffi::ObjectArc;
use tvm_ffi::Result;
use tvm_ffi::String;
use tvm_ffi::VALUE_ERROR;
// tvm-ffi-stubgen(end)

// tvm-ffi-stubgen(begin): object/sym.Analyzer
/// Opaque: bytes [24, 160) of [24, 160) are not accounted for by reflected fields. Fields are read through the C ABI getters.
#[repr(C)]
#[derive(tvm_ffi::derive::Object)]
#[type_key = "sym.Analyzer"]
#[type_final]
pub struct AnalyzerObj {
    base: Object,
}

#[repr(C)]
#[derive(tvm_ffi::derive::ObjectRef, Clone)]
pub struct Analyzer {
    base: ObjectArc<AnalyzerObj>,
}

impl Deref for Analyzer {
    type Target = AnalyzerObj;
    fn deref(&self) -> &AnalyzerObj {
        &self.base
    }
}
// tvm-ffi-stubgen(end)

// tvm-ffi-stubgen(begin): object/sym.CanonicalExpr
/// Opaque: no metadata of its own: total_size is unknown. Fields are read through the C ABI getters.
#[repr(C)]
#[derive(tvm_ffi::derive::Object)]
#[type_key = "sym.CanonicalExpr"]
pub struct CanonicalExprObj {
    base: ExprObj,
}

#[repr(C)]
#[derive(tvm_ffi::derive::ObjectRef, Clone)]
pub struct CanonicalExpr {
    base: ObjectArc<CanonicalExprObj>,
}

impl Deref for CanonicalExpr {
    type Target = CanonicalExprObj;
    fn deref(&self) -> &CanonicalExprObj {
        &self.base
    }
}

impl Deref for CanonicalExprObj {
    type Target = ExprObj;
    fn deref(&self) -> &ExprObj {
        &self.base
    }
}

tvm_ffi::impl_object_upcast!(CanonicalExpr => Expr);
// tvm-ffi-stubgen(end)

// tvm-ffi-stubgen(begin): object/sym.ConstIntBound
/// Complete: reflected fields fill [24, 40) exactly.
#[repr(C)]
#[derive(tvm_ffi::derive::Object)]
#[type_key = "sym.ConstIntBound"]
#[type_final]
pub struct ConstIntBoundObj {
    base: Object,
    pub min_value: i64,
    pub max_value: i64,
}

const _: () = {
    assert!(::core::mem::size_of::<ConstIntBoundObj>() == 40);
    assert!(::core::mem::align_of::<ConstIntBoundObj>() == 8);
};

#[repr(C)]
#[derive(tvm_ffi::derive::ObjectRef, Clone)]
pub struct ConstIntBound {
    base: ObjectArc<ConstIntBoundObj>,
}

impl Deref for ConstIntBound {
    type Target = ConstIntBoundObj;
    fn deref(&self) -> &ConstIntBoundObj {
        &self.base
    }
}

impl ConstIntBoundObj {
    pub(crate) fn new(min_value: i64, max_value: i64) -> Self {
        let base = Object::new();
        Self { base, min_value, max_value }
    }
}

impl ConstIntBound {
    /// Lossless complete-field allocation.
    pub fn new(min_value: i64, max_value: i64) -> Self {
        let obj = ConstIntBoundObj::new(min_value, max_value);
        Self { base: ObjectArc::new(obj) }
    }
}
// tvm-ffi-stubgen(end)

// tvm-ffi-stubgen(begin): object/sym.ModularSet
/// Complete: reflected fields fill [24, 40) exactly.
#[repr(C)]
#[derive(tvm_ffi::derive::Object)]
#[type_key = "sym.ModularSet"]
#[type_final]
pub struct ModularSetObj {
    base: Object,
    pub coeff: i64,
    pub base_: i64,
}

const _: () = {
    assert!(::core::mem::size_of::<ModularSetObj>() == 40);
    assert!(::core::mem::align_of::<ModularSetObj>() == 8);
};

#[repr(C)]
#[derive(tvm_ffi::derive::ObjectRef, Clone)]
pub struct ModularSet {
    base: ObjectArc<ModularSetObj>,
}

impl Deref for ModularSet {
    type Target = ModularSetObj;
    fn deref(&self) -> &ModularSetObj {
        &self.base
    }
}

impl ModularSetObj {
    pub(crate) fn new(coeff: i64, base_: i64) -> Self {
        let base = Object::new();
        Self { base, coeff, base_ }
    }
}

impl ModularSet {
    /// Lossless complete-field allocation.
    pub fn new(coeff: i64, base_: i64) -> Self {
        let obj = ModularSetObj::new(coeff, base_);
        Self { base: ObjectArc::new(obj) }
    }
}
// tvm-ffi-stubgen(end)

// tvm-ffi-stubgen(begin): object/sym.RewriteSimplifierStats
/// Complete: reflected fields fill [24, 72) exactly.
#[repr(C)]
#[derive(tvm_ffi::derive::Object)]
#[type_key = "sym.RewriteSimplifierStats"]
#[type_final]
pub struct RewriteSimplifierStatsObj {
    base: Object,
    pub nodes_visited: i64,
    pub constraints_entered: i64,
    pub rewrites_attempted: i64,
    pub rewrites_performed: i64,
    pub max_recursive_depth: i64,
    pub num_recursive_rewrites: i64,
}

const _: () = {
    assert!(::core::mem::size_of::<RewriteSimplifierStatsObj>() == 72);
    assert!(::core::mem::align_of::<RewriteSimplifierStatsObj>() == 8);
};

#[repr(C)]
#[derive(tvm_ffi::derive::ObjectRef, Clone)]
pub struct RewriteSimplifierStats {
    base: ObjectArc<RewriteSimplifierStatsObj>,
}

impl Deref for RewriteSimplifierStats {
    type Target = RewriteSimplifierStatsObj;
    fn deref(&self) -> &RewriteSimplifierStatsObj {
        &self.base
    }
}

impl RewriteSimplifierStatsObj {
    pub(crate) fn new(
        nodes_visited: i64,
        constraints_entered: i64,
        rewrites_attempted: i64,
        rewrites_performed: i64,
        max_recursive_depth: i64,
        num_recursive_rewrites: i64,
    ) -> Self {
        let base = Object::new();
        Self {
            base,
            nodes_visited,
            constraints_entered,
            rewrites_attempted,
            rewrites_performed,
            max_recursive_depth,
            num_recursive_rewrites,
        }
    }
}

impl RewriteSimplifierStats {
    /// Lossless complete-field allocation.
    pub fn new(
        nodes_visited: i64,
        constraints_entered: i64,
        rewrites_attempted: i64,
        rewrites_performed: i64,
        max_recursive_depth: i64,
        num_recursive_rewrites: i64,
    ) -> Self {
        let obj = RewriteSimplifierStatsObj::new(
            nodes_visited,
            constraints_entered,
            rewrites_attempted,
            rewrites_performed,
            max_recursive_depth,
            num_recursive_rewrites,
        );
        Self { base: ObjectArc::new(obj) }
    }
}
// tvm-ffi-stubgen(end)

// tvm-ffi-stubgen(begin): object/sym.SplitExpr
/// Opaque: no metadata of its own: total_size is unknown. Fields are read through the C ABI getters.
#[repr(C)]
#[derive(tvm_ffi::derive::Object)]
#[type_key = "sym.SplitExpr"]
pub struct SplitExprObj {
    base: CanonicalExprObj,
}

#[repr(C)]
#[derive(tvm_ffi::derive::ObjectRef, Clone)]
pub struct SplitExpr {
    base: ObjectArc<SplitExprObj>,
}

impl Deref for SplitExpr {
    type Target = SplitExprObj;
    fn deref(&self) -> &SplitExprObj {
        &self.base
    }
}

impl Deref for SplitExprObj {
    type Target = CanonicalExprObj;
    fn deref(&self) -> &CanonicalExprObj {
        &self.base
    }
}

tvm_ffi::impl_object_upcast!(SplitExpr => Expr, SplitExpr => CanonicalExpr);
// tvm-ffi-stubgen(end)

// tvm-ffi-stubgen(begin): object/sym.SumExpr
/// Opaque: no metadata of its own: total_size is unknown. Fields are read through the C ABI getters.
#[repr(C)]
#[derive(tvm_ffi::derive::Object)]
#[type_key = "sym.SumExpr"]
pub struct SumExprObj {
    base: CanonicalExprObj,
}

#[repr(C)]
#[derive(tvm_ffi::derive::ObjectRef, Clone)]
pub struct SumExpr {
    base: ObjectArc<SumExprObj>,
}

impl Deref for SumExpr {
    type Target = SumExprObj;
    fn deref(&self) -> &SumExprObj {
        &self.base
    }
}

impl Deref for SumExprObj {
    type Target = CanonicalExprObj;
    fn deref(&self) -> &CanonicalExprObj {
        &self.base
    }
}

tvm_ffi::impl_object_upcast!(SumExpr => Expr, SumExpr => CanonicalExpr);
// tvm-ffi-stubgen(end)

/// How hard [`Analyzer::can_prove`] tries: `sym::ProofStrength`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum ProofStrength {
    /// The default, inexpensive analyses.
    #[default]
    Default = 0,
    /// Also prove through symbolic bounds, which is more expensive.
    SymbolicBound = 1,
}

/// The result of [`Analyzer::try_compare`]: `sym::CompareResult`.
///
/// The relations other than `Inconsistent` and `Unknown` say what is known of `lhs` against `rhs`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum CompareResult {
    /// The known facts contradict each other.
    Inconsistent = 0,
    Eq = 1,
    Lt = 2,
    Le = 3,
    Gt = 4,
    Ge = 5,
    Ne = 6,
    /// Nothing is known of the relation.
    Unknown = 7,
}

impl TryFrom<i64> for CompareResult {
    type Error = Error;
    fn try_from(value: i64) -> Result<Self> {
        Ok(match value {
            0 => Self::Inconsistent,
            1 => Self::Eq,
            2 => Self::Lt,
            3 => Self::Le,
            4 => Self::Gt,
            5 => Self::Ge,
            6 => Self::Ne,
            7 => Self::Unknown,
            _ => {
                let message = format!("{value} is not a sym::CompareResult");
                return Err(Error::new(VALUE_ERROR, &message, ""));
            }
        })
    }
}

/// The uint-as-index assumption, in effect while it is alive: [`allow_uint_as_index`].
///
/// The assumption is a depth counter of the thread, so a scope stays on the thread that opened
/// it.
///
/// ```compile_fail
/// fn send<T: Send>() {}
/// send::<tvm_tirx::sym::AllowUintAsIndexScope>();
/// ```
#[must_use = "the assumption ends when the scope is dropped"]
pub struct AllowUintAsIndexScope(ExitOnDrop);

impl Analyzer {
    /// A fresh analyzer that knows no facts (`sym.Analyzer`).
    pub fn new() -> Self {
        support::infallible(
            tvm_ffi::cached_global_func!("sym.Analyzer")
                .call_tuple(())
                .and_then(|any| any.try_into()),
        )
    }

    /// Set whether default-strength queries try Z3 after native analysis fails.
    ///
    /// Enabled by default and preserved by `deep_clone`. Stronger queries and direct Z3
    /// APIs are unaffected. This does not clear accepted premises or solver state.
    pub fn set_z3_default_fallback(&self, enabled: bool) -> Result<()> {
        tvm_ffi::cached_global_func!("sym.AnalyzerSetZ3DefaultFallback")
            .call_tuple((self, enabled))?;
        Ok(())
    }

    /// A copy of the analyzer and the facts it knows (`sym.AnalyzerClone`).
    pub fn deep_clone(&self) -> Result<Self> {
        tvm_ffi::cached_global_func!("sym.AnalyzerClone").call_tuple((self,))?.try_into()
    }

    /// Synchronize with a `sym.ConstraintContext` captured at its current position.
    ///
    /// The context is an opaque FFI object until typed constraint bindings are available.
    /// Switching contexts removes facts that are no longer present. Mutating the context
    /// after this call takes effect only after another synchronization.
    pub fn sync_constraint_context(&self, context: &tvm_ffi::object::ObjectRef) -> Result<()> {
        tvm_ffi::cached_global_func!("sym.AnalyzerSyncConstraintContext")
            .call_packed(&[tvm_ffi::AnyView::from(self), tvm_ffi::AnyView::from(context)])?;
        Ok(())
    }

    /// The constant bounds of `expr` (`sym.AnalyzerConstIntBound`).
    pub fn const_int_bound(&self, expr: impl IntoExpr) -> Result<ConstIntBound> {
        tvm_ffi::cached_global_func!("sym.AnalyzerConstIntBound")
            .call_tuple((self, &expr.into_expr()?))?
            .try_into()
    }

    /// Whether `var` has constant bounds recorded (`sym.AnalyzerConstIntBoundIsBound`).
    pub fn const_int_bound_is_bound(&self, var: impl Into<Var>) -> Result<bool> {
        tvm_ffi::cached_global_func!("sym.AnalyzerConstIntBoundIsBound")
            .call_tuple((self, &var.into()))?
            .try_into()
    }

    /// The modular set `coeff * x + base` of `expr` (`sym.AnalyzerModularSet`).
    pub fn modular_set(&self, expr: impl IntoExpr) -> Result<ModularSet> {
        tvm_ffi::cached_global_func!("sym.AnalyzerModularSet")
            .call_tuple((self, &expr.into_expr()?))?
            .try_into()
    }

    /// The integer set `expr` covers, with the variables of `dom_map` over their sets
    /// (`sym.AnalyzerIntSet`).
    pub fn int_set(
        &self,
        expr: impl IntoExpr,
        dom_map: Option<&Map<Var, IntSet>>,
    ) -> Result<IntSet> {
        tvm_ffi::cached_global_func!("sym.AnalyzerIntSet")
            .call_tuple((self, &expr.into_expr()?, dom_map.cloned()))?
            .try_into()
    }

    /// Simplify `expr`, alternating the rewrite and canonical simplifiers `steps` times
    /// (`sym.AnalyzerSimplify`).
    pub fn simplify(&self, expr: impl IntoExpr, steps: i32) -> Result<Expr> {
        tvm_ffi::cached_global_func!("sym.AnalyzerSimplify")
            .call_tuple((self, &expr.into_expr()?, steps))?
            .try_into()
    }

    /// Simplify `expr` by rewrite rules (`sym.AnalyzerRewriteSimplify`).
    pub fn rewrite_simplify(&self, expr: impl IntoExpr) -> Result<Expr> {
        tvm_ffi::cached_global_func!("sym.AnalyzerRewriteSimplify")
            .call_tuple((self, &expr.into_expr()?))?
            .try_into()
    }

    /// Simplify `expr` into canonical form (`sym.AnalyzerCanonicalSimplify`).
    pub fn canonical_simplify(&self, expr: impl IntoExpr) -> Result<Expr> {
        tvm_ffi::cached_global_func!("sym.AnalyzerCanonicalSimplify")
            .call_tuple((self, &expr.into_expr()?))?
            .try_into()
    }

    /// The counters of the rewrite simplifier (`sym.AnalyzerGetRewriteSimplifyStats`).
    pub fn rewrite_simplify_stats(&self) -> Result<RewriteSimplifierStats> {
        tvm_ffi::cached_global_func!("sym.AnalyzerGetRewriteSimplifyStats")
            .call_tuple((self,))?
            .try_into()
    }

    /// Zero the counters of the rewrite simplifier (`sym.AnalyzerResetRewriteSimplifyStats`).
    pub fn reset_rewrite_simplify_stats(&self) -> Result<()> {
        tvm_ffi::cached_global_func!("sym.AnalyzerResetRewriteSimplifyStats")
            .call_tuple((self,))?;
        Ok(())
    }

    /// Cap the rewrites of one rewrite simplification; 0 is no cap
    /// (`sym.AnalyzerSetMaximumRewriteSteps`).
    pub fn set_maximum_rewrite_steps(&self, maximum: i64) -> Result<()> {
        tvm_ffi::cached_global_func!("sym.AnalyzerSetMaximumRewriteSteps")
            .call_tuple((self, maximum))?;
        Ok(())
    }

    /// Whether `expr` is provably true (`sym.AnalyzerCanProve`).
    pub fn can_prove(&self, expr: impl IntoExpr, strength: ProofStrength) -> Result<bool> {
        tvm_ffi::cached_global_func!("sym.AnalyzerCanProve")
            .call_tuple((self, &expr.into_expr()?, strength as i32))?
            .try_into()
    }

    /// Whether `lhs == rhs` is provably true (`sym.AnalyzerCanProveEqual`).
    pub fn can_prove_equal(&self, lhs: impl IntoExpr, rhs: impl IntoExpr) -> Result<bool> {
        tvm_ffi::cached_global_func!("sym.AnalyzerCanProveEqual")
            .call_tuple((self, &lhs.into_expr()?, &rhs.into_expr()?))?
            .try_into()
    }

    /// The relation of `lhs` to `rhs` the known comparisons imply (`sym.AnalyzerTryCompare`).
    pub fn try_compare(&self, lhs: impl IntoExpr, rhs: impl IntoExpr) -> Result<CompareResult> {
        let result: i64 = tvm_ffi::cached_global_func!("sym.AnalyzerTryCompare")
            .call_tuple((self, &lhs.into_expr()?, &rhs.into_expr()?))?
            .try_into()?;
        result.try_into()
    }

    /// Whether the analyzer was built with the Z3 prover (`sym.AnalyzerIsZ3Enabled`).
    pub fn is_z3_enabled(&self) -> Result<bool> {
        tvm_ffi::cached_global_func!("sym.AnalyzerIsZ3Enabled").call_tuple((self,))?.try_into()
    }

    /// The facts the Z3 prover knows, and `expr` to prove if given, as an SMT-LIB2 script
    /// (`sym.AnalyzerGetSMTLIB2`).
    pub fn smtlib2(&self, expr: Option<&Expr>) -> Result<String> {
        tvm_ffi::cached_global_func!("sym.AnalyzerGetSMTLIB2")
            .call_tuple((self, expr.cloned()))?
            .try_into()
    }

    /// Bound each Z3 proof by `timeout_ms` milliseconds (`sym.AnalyzerSetZ3TimeoutMs`).
    pub fn set_z3_timeout_ms(&self, timeout_ms: u32) -> Result<()> {
        tvm_ffi::cached_global_func!("sym.AnalyzerSetZ3TimeoutMs")
            .call_tuple((self, timeout_ms))?;
        Ok(())
    }

    /// Bound each Z3 proof by the resource limit `rlimit` (`sym.AnalyzerSetZ3RLimit`).
    pub fn set_z3_rlimit(&self, rlimit: u32) -> Result<()> {
        tvm_ffi::cached_global_func!("sym.AnalyzerSetZ3RLimit").call_tuple((self, rlimit))?;
        Ok(())
    }

    /// The statistics of the Z3 prover (`sym.AnalyzerGetZ3Stats`).
    pub fn z3_stats(&self) -> Result<String> {
        tvm_ffi::cached_global_func!("sym.AnalyzerGetZ3Stats").call_tuple((self,))?.try_into()
    }
}

/// Treat `uint32` and `uint64` scalars as non-wrapping index types until the returned scope is
/// dropped (`sym.AnalyzerEnterAllowUintAsIndex`).
///
/// For compile-time layout proofs, not for code whose unsigned arithmetic may wrap around.
pub fn allow_uint_as_index() -> Result<AllowUintAsIndexScope> {
    let exit: tvm_ffi::Function = tvm_ffi::cached_global_func!("sym.AnalyzerEnterAllowUintAsIndex")
        .call_tuple(())?
        .try_into()?;
    Ok(AllowUintAsIndexScope(ExitOnDrop::new(exit)))
}