Skip to content

Commit cd5de7b

Browse files
committed
v3: promote fastc parallel assignments
1 parent 9141bde commit cd5de7b

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

vlib/v3/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ C-string and embedded-NUL string literals, assertions, `sizeof`, comparison/logi
113113
division, and modulo expressions, and functions with narrow integer signatures are promoted to
114114
the complete lane. Inferred declarations and C-style loop initializers using the minimum `int`
115115
literal are promoted as well, as are expressions containing hexadecimal literals above the signed
116-
32-bit range. Indexing expressions are promoted, preserving V layouts, inferred types, element
117-
types, and bounds checks. Together these promotions preserve V's formatting, byte-length,
118-
diagnostics, boolean typing, integer-wrapping, safe-shift, and zero-divisor behavior instead of
119-
relying on incompatible raw C semantics.
116+
32-bit range. Parallel assignments and indexing expressions are promoted, preserving simultaneous
117+
assignment, V layouts, inferred types, element types, and bounds checks. Together these promotions
118+
preserve V's formatting, byte-length, diagnostics, boolean typing, integer-wrapping, safe-shift,
119+
and zero-divisor behavior instead of relying on incompatible raw C semantics.
120120

121121
The direct path is limited to host-target, non-production, non-test, non-shared single-file builds.
122122
Compiler/self-host, strict C, and other non-direct modes enter the complete lane before source

vlib/v3/gen/fastc/fastc.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,11 @@ fn (mut g DirectGen) read_expression_with_prefix(prefix string, stops []token.To
529529
if paren_depth == 0 && g.tok in stops {
530530
break
531531
}
532+
if paren_depth == 0 && g.tok == .comma {
533+
// V's top-level commas form simultaneous multi-target assignments.
534+
// Copying them to C would instead emit comma operators.
535+
return g.unsupported('parallel assignments')
536+
}
532537
if g.tok in [.eq, .ne, .gt, .lt, .ge, .le, .and, .logical_or, .not] {
533538
// C represents comparison and logical results as int. Without V type
534539
// information, accepting them here would make generic printing and

vlib/v3/gen/fastc/fastc_test.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ fn test_type_sensitive_expressions_request_checked_lane() {
173173
'module main\nfn main() { mut x := -2_147_483_648; x--; println(x) }\n',
174174
'module main\nfn main() { for i := -2_147_483_648; true; i-- { println(i); break } }\n',
175175
'module main\nfn main() { x := 0xffff_ffff | 0; println(x) }\n',
176+
'module main\nfn main() { mut a := 1; mut b := 2; a, b = b, a; println(a); println(b) }\n',
176177
] {
177178
mut failed := false
178179
_ := generate(source, 'typed_expression.v', prefs) or {
@@ -189,4 +190,7 @@ fn test_type_sensitive_expressions_request_checked_lane() {
189190
low_hex_c := generate('module main\nfn main() { x := 0x7fff_ffff | 0; println(x) }\n',
190191
'low_hex_literal.v', prefs) or { panic(err) }
191192
assert low_hex_c.contains('__typeof__((0x7fffffff|0)) x = (0x7fffffff|0);')
193+
call_c := generate('module main\nfn sum(a int, b int) int { return a + b }\nfn main() { println(sum(1, 2)) }\n',
194+
'call_comma.v', prefs) or { panic(err) }
195+
assert call_c.contains('println(sum(1,2));')
192196
}

vlib/v3/tests/fastc_backend_test.v

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,29 @@ fn main() {
278278
assert high_hex_run.exit_code == 0, high_hex_run.output
279279
assert high_hex_run.output.trim_space() == '-1'
280280

281+
parallel_assign_source := os.join_path(root, 'parallel_assign.v')
282+
os.write_file(parallel_assign_source, 'module main
283+
284+
fn main() {
285+
mut a := 1
286+
mut b := 2
287+
a, b = b, a
288+
println(a)
289+
println(b)
290+
}
291+
') or {
292+
panic(err)
293+
}
294+
parallel_assign_binary := os.join_path(root, 'parallel_assign')
295+
parallel_assign_compile := cmdexec.run(v3_bin, ['-silent', '-b', 'fastc', '-o',
296+
parallel_assign_binary, parallel_assign_source])
297+
assert parallel_assign_compile.exit_code == 0, parallel_assign_compile.output
298+
parallel_assign_c := os.read_file(parallel_assign_binary + '.c') or { panic(err) }
299+
assert !parallel_assign_c.contains('V_FASTC_PRINT_SELECT')
300+
parallel_assign_run := cmdexec.run(parallel_assign_binary, [])
301+
assert parallel_assign_run.exit_code == 0, parallel_assign_run.output
302+
assert parallel_assign_run.output.trim_space() == '2\n1'
303+
281304
shift_source := os.join_path(root, 'oversized_shift.v')
282305
os.write_file(shift_source, 'module main
283306

0 commit comments

Comments
 (0)