From acc13867bfa404e57d6e322fe1e1abb32d4cdb58 Mon Sep 17 00:00:00 2001 From: Louis Gesbert Date: Tue, 6 Aug 2024 12:41:57 +0200 Subject: [PATCH] Fix: return module items in file order This affects their UIDs, and the order in which they later get traversed. In particular, it was affecting the struct names renaming. --- compiler/shared_ast/program.ml | 4 ++++ compiler/surface/parser_driver.ml | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/shared_ast/program.ml b/compiler/shared_ast/program.ml index 2dd81cd3..dc807647 100644 --- a/compiler/shared_ast/program.ml +++ b/compiler/shared_ast/program.ml @@ -122,6 +122,10 @@ let rename_ids let module PathMap = Map.Make (Uid.Path) in let pctxmap = PathMap.singleton [] ctx in let pctxmap, structs_map, fields_map, ctx_structs = + (* Warning: the folding order matters here, if a module contains e.g. two + fields with the same name. This fold relies on UIDs, and is thus + dependent on the definition order. Another possibility would be to fold + lexicographically, but the result would be "less intuitive" *) StructName.Map.fold (fun name fields (pctxmap, structs_map, fields_map, ctx_structs) -> let path = StructName.path name in diff --git a/compiler/surface/parser_driver.ml b/compiler/surface/parser_driver.ml index c317db7b..bdd0540a 100644 --- a/compiler/surface/parser_driver.ml +++ b/compiler/surface/parser_driver.ml @@ -476,7 +476,8 @@ let get_interface program = (* Non-metadata blocks are ignored *) req, acc in - List.fold_left filter ([], []) program.Ast.program_items + let req, acc = List.fold_left filter ([], []) program.Ast.program_items in + List.rev req, List.rev acc (** {1 API} *)