v / vlib / v2 / ssa / register_fn_signatures_from_flat_test.v
125 lines · 113 sloc · 3.77 KB · d358576851079d4716834cc86627307d28acd1ae
Raw
1// Copyright (c) 2026 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4// vtest build: macos
5//
6// Bit-equality pin for s172: `register_fn_signatures_from_flat` (flat-cursor
7// port) must register the same fn signatures (fn_index, mod.funcs) as the
8// legacy `register_fn_signatures`. The flat port walks one file's top-level
9// stmts via FileCursor and only reads `.stmt_fn_decl` signatures via
10// `Cursor.fn_decl_signature()` which returns FnDecl with `stmts = []`. Fn
11// bodies are never decoded — the largest per-stmt savings of any SSA
12// phase port.
13module ssa
14
15import v2.ast
16import v2.types
17
18fn make_fn_sigs_fixture() []ast.File {
19 return [
20 ast.File{
21 name: 'main.v'
22 mod: 'main'
23 stmts: [
24 ast.Stmt(ast.ModuleStmt{
25 name: 'main'
26 }),
27 ast.Stmt(ast.FnDecl{
28 name: 'add'
29 typ: ast.FnType{
30 return_type: ast.Expr(ast.Ident{
31 name: 'int'
32 })
33 params: [
34 ast.Parameter{
35 name: 'a'
36 typ: ast.Expr(ast.Ident{
37 name: 'int'
38 })
39 },
40 ast.Parameter{
41 name: 'b'
42 typ: ast.Expr(ast.Ident{
43 name: 'int'
44 })
45 },
46 ]
47 }
48 }),
49 ast.Stmt(ast.FnDecl{
50 name: 'noop'
51 typ: ast.FnType{}
52 }),
53 ]
54 },
55 ]
56}
57
58// register_fn_signatures_from_flat on a file with no fns must not change
59// fn_index or mod.funcs — same as register_fn_signatures.
60fn test_register_fn_signatures_from_flat_no_fns_matches_legacy() {
61 files := [
62 ast.File{
63 name: 'empty.v'
64 mod: 'main'
65 stmts: [ast.Stmt(ast.ModuleStmt{
66 name: 'main'
67 })]
68 },
69 ]
70 flat := ast.flatten_files(files)
71
72 env := types.Environment.new()
73
74 mut mod_legacy := Module.new('fns_empty_legacy')
75 mut b_legacy := Builder.new_with_env(mod_legacy, env)
76 b_legacy.register_fn_signatures(files[0])
77
78 mut mod_flat := Module.new('fns_empty_flat')
79 mut b_flat := Builder.new_with_env(mod_flat, env)
80 b_flat.register_fn_signatures_from_flat(flat.file_cursor(0))
81
82 assert b_legacy.fn_index.len == b_flat.fn_index.len
83 assert mod_legacy.funcs.len == mod_flat.funcs.len
84}
85
86// register_fn_signatures_from_flat on a file with two FnDecls (add + noop)
87// must register both with identical fn_index entries and matching parameter
88// counts on the SSA Function.
89fn test_register_fn_signatures_from_flat_matches_legacy_for_two_fns() {
90 files := make_fn_sigs_fixture()
91 flat := ast.flatten_files(files)
92
93 env := types.Environment.new()
94
95 mut mod_legacy := Module.new('fns_two_legacy')
96 mut b_legacy := Builder.new_with_env(mod_legacy, env)
97 b_legacy.register_fn_signatures(files[0])
98
99 mut mod_flat := Module.new('fns_two_flat')
100 mut b_flat := Builder.new_with_env(mod_flat, env)
101 b_flat.register_fn_signatures_from_flat(flat.file_cursor(0))
102
103 // Both paths registered exactly 2 fns.
104 assert b_legacy.fn_index.len == b_flat.fn_index.len
105 assert b_legacy.fn_index.len == 2
106 assert 'add' in b_legacy.fn_index
107 assert 'add' in b_flat.fn_index
108 assert 'noop' in b_legacy.fn_index
109 assert 'noop' in b_flat.fn_index
110 // Function ids match because identical registration order on isolated Modules.
111 assert b_legacy.fn_index['add'] == b_flat.fn_index['add']
112 assert b_legacy.fn_index['noop'] == b_flat.fn_index['noop']
113 // mod.funcs slot counts match.
114 assert mod_legacy.funcs.len == mod_flat.funcs.len
115 // add has 2 params on both paths.
116 add_legacy := mod_legacy.funcs[b_legacy.fn_index['add']]
117 add_flat := mod_flat.funcs[b_flat.fn_index['add']]
118 assert add_legacy.params.len == add_flat.params.len
119 assert add_legacy.params.len == 2
120 // noop has 0 params on both paths.
121 noop_legacy := mod_legacy.funcs[b_legacy.fn_index['noop']]
122 noop_flat := mod_flat.funcs[b_flat.fn_index['noop']]
123 assert noop_legacy.params.len == noop_flat.params.len
124 assert noop_legacy.params.len == 0
125}
126