cgen: fix translated file fixed-array assignment (#25080)
Some checks are pending
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run

This commit is contained in:
kbkpbot 2025-08-10 18:31:44 +08:00 committed by GitHub
parent 652881c73b
commit 7b77f2ee5b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 4 deletions

View file

@ -508,12 +508,14 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
unwrapped_val_type := g.unwrap_generic(val_type)
right_sym := g.table.sym(unwrapped_val_type)
unaliased_right_sym := g.table.final_sym(unwrapped_val_type)
is_fixed_array_var := !g.pref.translated && unaliased_right_sym.kind == .array_fixed
&& val !is ast.ArrayInit
unaliased_left_sym := g.table.final_sym(g.unwrap_generic(var_type))
is_fixed_array_var := unaliased_right_sym.kind == .array_fixed && val !is ast.ArrayInit
&& (val in [ast.Ident, ast.IndexExpr, ast.CallExpr, ast.SelectorExpr, ast.DumpExpr, ast.InfixExpr]
|| (val is ast.CastExpr && val.expr !is ast.ArrayInit)
|| (val is ast.PrefixExpr && val.op == .arrow)
|| (val is ast.UnsafeExpr && val.expr in [ast.SelectorExpr, ast.Ident, ast.CallExpr]))
&& !((g.pref.translated || g.file.is_translated)
&& unaliased_left_sym.kind != .array_fixed)
g.is_assign_lhs = true
g.assign_op = node.op
@ -553,10 +555,18 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
}
g.writeln(';}')
}
} else if node.op == .assign && !g.pref.translated && (is_fixed_array_init
} else if node.op == .assign && (is_fixed_array_init
|| (unaliased_right_sym.kind == .array_fixed && val in [ast.Ident, ast.CastExpr])) {
// Fixed arrays
if is_fixed_array_init && var_type.has_flag(.option) {
if unaliased_left_sym.kind != .array_fixed && unaliased_right_sym.kind == .array_fixed
&& (g.pref.translated || g.file.is_translated) {
// translated:
// arr = [5]u8{}
// ptr = arr => ptr = &arr[0]
g.expr(left)
g.write(' = ')
g.expr(val)
} else if is_fixed_array_init && var_type.has_flag(.option) {
g.expr(left)
g.write(' = ')
g.expr_with_opt(val, val_type, var_type)

View file

@ -12,3 +12,36 @@ const ssf = [1, 2, 3]!
fn test_const_name_without_main_prefix() {
assert ssf[0] == 1
}
struct ASMOperand {
constraint [2]i8
}
fn test_pointer_assign_without_memcpy() {
op := ASMOperand{
constraint: [i8(5), 4]!
}
str := &i8(0)
str = op.constraint
assert !isnil(str)
ops := [3]ASMOperand{}
pop := &ASMOperand(0)
pop = ops
assert pop[1].constraint[0] == 0
}
struct StubIndex {
pub mut:
data [5][5]map[string]string
}
fn test_pointer_assign_with_memcpy() {
mut s := StubIndex{}
s.data[1] = [5]map[string]string{}
mut data := s.data[1]
data[0]['abc'] = '123'
k := data[0]['abc']
assert k == '123'
}