Skip to content

Commit e5c93a1

Browse files
tydeuclaude
andcommitted
test: postpone compile
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 81c5655 commit e5c93a1

9 files changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module
2+
3+
import Test.A
4+
5+
#eval twice 21
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module
2+
3+
import Test.C
4+
5+
public def main : IO Unit := IO.println (viaImportAll 20)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module
2+
3+
public import Test.C
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module
2+
3+
private def offset : Nat := 1
4+
5+
@[inline] public def addOffset (n : Nat) : Nat := n + offset
6+
7+
public def twice (n : Nat) : Nat := n + n
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module
2+
3+
import Test.A
4+
5+
public def viaImport (n : Nat) : Nat := addOffset (twice n)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module
2+
3+
import all Test.B
4+
5+
public def viaImportAll (n : Nat) : Nat := viaImport n + 1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
rm -rf work
3+
rm -f produced.out
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name = "codegen"
2+
defaultTargets = ["codegen"]
3+
4+
[leanOptions]
5+
experimental.module = true
6+
compiler.postponeCompile = true
7+
8+
[[lean_lib]]
9+
name = "Test"
10+
11+
[[lean_exe]]
12+
name = "codegen"
13+
root = "Main"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bash
2+
source ../common.sh
3+
4+
./clean.sh
5+
6+
# ---
7+
# This test covers `compiler.postponeCompile`, under which Lake defers a module system module's
8+
# code generation to a separate `leanir` step producing its `.ir.sig`, `.ir`, and `.c`.
9+
# ---
10+
11+
# The rebuild tests below edit the sources, so work on a copy
12+
copy_to_work lakefile.toml Main.lean Test.lean Test Eval.lean
13+
14+
# Elaboration alone does not generate code
15+
test_run build Test.A
16+
test_cmd_fails test -f .lake/build/ir/Test/A.c
17+
18+
# Each module's code generation is a job of its own
19+
echo "# TEST: code generation"
20+
test_out "Built Test.A:irArts" build Test.A:c -v
21+
test_cmd test -f .lake/build/lib/lean/Test/A.ir.sig
22+
test_cmd test -f .lake/build/lib/lean/Test/A.ir
23+
test_out "Built Test.B:irArts" build Test.B:c -v
24+
test_run build Test.C:c
25+
26+
# An import's IR must be provided even for a plain `import`, as the language server loads it
27+
test_cmd grep -F 'Test/A.ir"' .lake/build/ir/Test/B.setup.json
28+
test_cmd grep -F 'Test/A.ir"' .lake/build/ir/Test/B.irsetup.json
29+
30+
# The server allows `#eval` on a plainly imported definition, so it must be able to run it
31+
echo "# TEST: server eval across a plain import"
32+
echo '$' lake setup-file Eval.lean
33+
"$LAKE" setup-file Eval.lean > eval.setup.json
34+
test_cmd_eq 42 lean --setup eval.setup.json -DElab.inServer=true Eval.lean
35+
36+
# The generated code links and runs
37+
echo "# TEST: link and run"
38+
test_run build codegen
39+
test_cmd_eq 42 ./.lake/build/bin/codegen
40+
41+
# ---
42+
# Tests that `leanir` is only rerun when needed
43+
# ---
44+
45+
test_run build Test.A:c Test.B:c Test.C:c --no-build
46+
47+
# A non-inlinable definition's body is part of the module's IR, but not of its `.ir.sig`
48+
echo "# TEST: irArts on a value edit"
49+
test_cmd sed_i 's/n + n/n + n + 0/' Test/A.lean
50+
test_out "Built Test.A:irArts" build Test.A:c -v
51+
# importers read only the `.ir.sig`, so their own IR is unaffected
52+
test_run build Test.B:c Test.C:c --no-build
53+
54+
# A new public definition changes the `.ir.sig` as well
55+
echo "# TEST: irArts on an interface edit"
56+
test_run build Test.A:c Test.B:c Test.C:c
57+
test_cmd sed_i 's/^private def offset/public def extra : Nat := 7\nprivate def offset/' Test/A.lean
58+
test_out "Built Test.A:irArts" build Test.A:c -v
59+
test_out "Built Test.B:irArts" build Test.B:c -v
60+

0 commit comments

Comments
 (0)