v / vlib / v2 / ssa / build_string_inter_from_flat_test.v
99 lines · 92 sloc · 2.91 KB · 07d7d0a475915ac672c8ed1ed3128131e29be755
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 the cursor-native rewrite of `build_string_inter_from_flat`.
7// The previous helper rehydrated every StringInter via two `decode_expr` calls
8// (one for `expr`, one for `format_expr`) and dispatched to legacy
9// `build_string_inter_literal`. The cursor-native rewrite mirrors
10// `build_string_inter_literal` directly: literal parts come from `list_at(0)`
11// via `values_l.at(i).name()`, each inter's `format`/`resolved_fmt`/`expr_c`
12// are read from `aux`/`name`/`edge(0)`, and `build_expr_from_flat(expr_c)`
13// produces the value. The `format_expr` edge is never read — legacy
14// `build_string_inter_literal` never consults `inter.format_expr` either.
15// The `.str` selector-stripping path is implemented cursor-natively: kind
16// check + edge(1) ident name check.
17module ssa
18
19import v2.ast
20import v2.types
21
22fn si_ident(name string) ast.Expr {
23 return ast.Expr(ast.Ident{
24 name: name
25 })
26}
27
28fn make_string_inter_fixture() []ast.File {
29 mut stmts := [
30 ast.Stmt(ast.ModuleStmt{
31 name: 'main'
32 }),
33 ]
34 // fn greet() string { name := 'world' return 'hello ${name}!' }
35 stmts << ast.Stmt(ast.FnDecl{
36 name: 'greet'
37 typ: ast.FnType{
38 return_type: si_ident('string')
39 }
40 stmts: [
41 ast.Stmt(ast.AssignStmt{
42 op: .decl_assign
43 lhs: [si_ident('name')]
44 rhs: [ast.Expr(ast.StringLiteral{
45 kind: .v
46 value: "'world'"
47 })]
48 }),
49 ast.Stmt(ast.ReturnStmt{
50 exprs: [
51 ast.Expr(ast.StringInterLiteral{
52 kind: .v
53 values: ["'hello ", "!'"]
54 inters: [ast.StringInter{
55 format: .unformatted
56 expr: si_ident('name')
57 }]
58 }),
59 ]
60 }),
61 ]
62 })
63 return [
64 ast.File{
65 name: 'main.v'
66 mod: 'main'
67 stmts: stmts
68 },
69 ]
70}
71
72fn build_via_legacy_string_inter(files []ast.File, env &types.Environment, name string) &Module {
73 mut mod := Module.new(name)
74 mut b := Builder.new_with_env(mod, env)
75 b.register_fn_signatures(files[0])
76 b.build_fn_bodies(files[0])
77 return mod
78}
79
80fn build_via_flat_string_inter(files []ast.File, env &types.Environment, name string) &Module {
81 flat := ast.flatten_files(files)
82 mut mod := Module.new(name)
83 mut b := Builder.new_with_env(mod, env)
84 b.register_fn_signatures_from_flat(flat.file_cursor(0))
85 b.build_fn_bodies_from_flat(flat.file_cursor(0))
86 return mod
87}
88
89fn test_build_string_inter_from_flat_matches_legacy() {
90 files := make_string_inter_fixture()
91 env := types.Environment.new()
92 mod_legacy := build_via_legacy_string_inter(files, env, 'string_inter_legacy')
93 mod_flat := build_via_flat_string_inter(files, env, 'string_inter_flat')
94
95 assert mod_legacy.funcs.len == mod_flat.funcs.len
96 assert mod_legacy.blocks.len == mod_flat.blocks.len
97 assert mod_legacy.instrs.len == mod_flat.instrs.len
98 assert mod_legacy.values.len == mod_flat.values.len
99}
100