Skip to content

Commit

Permalink
fix: lake: TOML key order bug in ppTable (#4104)
Browse files Browse the repository at this point in the history
Fixes a bug in `Lake.Toml.ppTable` where root table keys could be
printed after a subtable header.

Closes #4099.

(cherry picked from commit ca6437d)
  • Loading branch information
tydeu authored and kim-em committed May 10, 2024
1 parent d0b121c commit a4d7e81
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/lake/Lake/Toml/Data/Value.lean
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,28 @@ end
instance : ToString Value := ⟨Value.toString⟩

def ppTable (t : Table) : String :=
String.trimLeft <| t.items.foldl (init := "") fun s (k,v) =>
let (ts, fs) := t.items.foldl (init := ("", "")) fun (ts, fs) (k,v) =>
match v with
| .array _ vs =>
if vs.all (· matches .table ..) then
vs.foldl (init := s) fun s v =>
let fs := vs.foldl (init := fs) fun s v =>
match v with
| .table _ t =>
let s := s ++ s!"\n[[{ppKey k}]]\n"
t.items.foldl (fun s (k,v) => appendKeyval s k v) s
let s := s ++ s!"[[{ppKey k}]]\n"
let s := t.items.foldl (fun s (k,v) => appendKeyval s k v) s
s.push '\n'
| _ => unreachable!
(ts, fs)
else
s.append s!"{ppKey k} = {ppInlineArray vs}\n"
(ts.append s!"{ppKey k} = {ppInlineArray vs}\n", fs)
| .table _ t =>
let s := s ++ s!"\n[{ppKey k}]\n"
t.items.foldl (fun s (k,v) => appendKeyval s k v) s
| _ => appendKeyval s k v
let fs := fs ++ s!"[{ppKey k}]\n"
let fs := t.items.foldl (fun s (k,v) => appendKeyval s k v) fs
(ts, fs.push '\n')
| _ => (appendKeyval ts k v, fs)
-- Ensures root table keys come before subtables
-- See https://github.com/leanprover/lean4/issues/4099
(ts.push '\n' ++ fs).trimRight.push '\n'
where
appendKeyval s k v :=
s.append s!"{ppKey k} = {v}\n"
3 changes: 3 additions & 0 deletions src/lake/tests/translateConfig/out.expected.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name = "test"
testRunner = "b"
defaultTargets = ["A"]

[leanOptions]
pp.unicode.fun = true

[[require]]
name = "foo"
path = "dir"
Expand Down
2 changes: 1 addition & 1 deletion src/lake/tests/translateConfig/source.lean
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package test where
buildArchive? := none
preferReleaseBuild := false
packagesDir := defaultPackagesDir
toLeanConfig := testLeanConfig
leanOptions := #[⟨`pp.unicode.fun, true⟩]

require foo from "dir" with NameMap.empty.insert `foo "bar"
require bar from git "https://example.com" @ "abc" / "sub" / "dir"
Expand Down

0 comments on commit a4d7e81

Please sign in to comment.