lean4-htt/tests/elab/compile_recursive_array_access.lean
Henrik Böving d0aa7d2faa
perf: mark inhabited arguments to extern as borrowed (#13094)
This PR marks the `Inhabited` arguments of all functions in core marked
as `extern` as borrowed
(panicking array accessors and `panic!` itself). This in turn causes a
transitive effect throughout
the codebase and promotes most, if not all, `Inhabited` arguments to
functions to borrowed.
2026-03-24 13:54:06 +00:00

52 lines
1.5 KiB
Text

/-!
This test asserts that the compiler is able to handle compilation of functions that recurse through
nested arrays in a way that does not unnecessarily remove borrow annotations.
-/
inductive NAryTree where
| tip (x : String)
| node (ys : Array NAryTree)
deriving Inhabited
/--
trace: [Compiler.explicitRc] size: 20
def followPath @&tree @&path : obj :=
cases tree : obj
| NAryTree.tip =>
cases path : obj
| List.nil =>
let x.1 := oproj[0] tree;
inc[ref] x.1;
return x.1
| _ =>
let _x.2 := instInhabitedNAryTree.default._closed_0;
return _x.2
| NAryTree.node =>
cases path : obj
| List.cons =>
let ys.3 := oproj[0] tree;
let head.4 := oproj[0] path;
let tail.5 := oproj[1] path;
let _x.6 := instInhabitedNAryTree.default;
let _x.7 := Array.get!InternalBorrowed ◾ _x.6 ys.3 head.4;
dec[persistent][ref] _x.6;
let _x.8 := followPath _x.7 tail.5;
return _x.8
| _ =>
let _x.9 := instInhabitedNAryTree.default._closed_0;
return _x.9
[Compiler.explicitRc] size: 3
def followPath._boxed tree path : obj :=
let res := followPath tree path;
dec path;
dec[ref] tree;
return res
-/
#guard_msgs in
set_option trace.Compiler.explicitRc true in
def followPath (tree : NAryTree) (path : List Nat) : String :=
match tree, path with
| .tip x, [] => x
| .node ys, idx :: path => followPath ys[idx]! path
| _, _ => default