cgen: fix address of call with cast(&(arr.last() as Type)) (fix #23528) (#25132)
Some checks failed
native backend CI / native-backend-ubuntu (push) Has been cancelled
native backend CI / native-backend-windows (push) Has been cancelled
sdl CI / v-compiles-sdl-examples (push) Has been cancelled
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Has been cancelled
wasm backend CI / wasm-backend (windows-2022) (push) Has been cancelled
Graphics CI / gg-regressions (push) Has been cancelled
vlib modules CI / build-module-docs (push) Has been cancelled
Shy and PV CI / v-compiles-puzzle-vibes (push) Has been cancelled
Sanitized CI / sanitize-undefined-clang (push) Has been cancelled
Sanitized CI / sanitize-undefined-gcc (push) Has been cancelled
Sanitized CI / tests-sanitize-address-clang (push) Has been cancelled
Sanitized CI / sanitize-address-msvc (push) Has been cancelled
Sanitized CI / sanitize-address-gcc (push) Has been cancelled
Sanitized CI / sanitize-memory-clang (push) Has been cancelled
Time CI / time-linux (push) Has been cancelled
Time CI / time-macos (push) Has been cancelled
Time CI / time-windows (push) Has been cancelled
toml CI / toml-module-pass-external-test-suites (push) Has been cancelled
Tools CI / tools-linux (clang) (push) Has been cancelled
Tools CI / tools-linux (gcc) (push) Has been cancelled
Tools CI / tools-linux (tcc) (push) Has been cancelled
Tools CI / tools-macos (clang) (push) Has been cancelled
Tools CI / tools-windows (gcc) (push) Has been cancelled
Tools CI / tools-windows (msvc) (push) Has been cancelled
Tools CI / tools-windows (tcc) (push) Has been cancelled
Tools CI / tools-docker-ubuntu-musl (push) Has been cancelled
vab CI / vab-compiles-v-examples (push) Has been cancelled
vab CI / v-compiles-os-android (push) Has been cancelled

Co-authored-by: krchi <krchi@outlook.com>
This commit is contained in:
Krchi 2025-08-18 22:34:46 +08:00 committed by GitHub
parent 5566df56f1
commit c68e37e3e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 1 deletions

View file

@ -3937,6 +3937,25 @@ fn (mut g Gen) expr(node_ ast.Expr) {
if g.is_option_auto_heap {
g.write('(${g.base_type(node.right_type)}*)')
}
mut tmp_var := ''
if node.op == .amp {
if node.right is ast.ParExpr && node.right.expr is ast.AsCast
&& (node.right.expr as ast.AsCast).expr is ast.CallExpr {
str := g.go_before_last_stmt()
g.empty_line = true
typ := g.styp(((node.right as ast.ParExpr).expr as ast.AsCast).typ)
tmp_var = g.new_tmp_var()
g.writeln('${typ} ${tmp_var};')
g.stmts_with_tmp_var([
ast.ExprStmt{
pos: node.right.pos()
expr: node.right
},
], tmp_var)
g.set_current_pos_as_last_stmt_pos()
g.write(str)
}
}
mut has_slice_call := false
if !g.is_option_auto_heap && !(g.is_amp && node.right.is_auto_deref_var()) {
has_slice_call = node.op == .amp && node.right is ast.IndexExpr
@ -3947,7 +3966,11 @@ fn (mut g Gen) expr(node_ ast.Expr) {
g.write(node.op.str())
}
}
g.expr(node.right)
if tmp_var == '' {
g.expr(node.right)
} else {
g.write(tmp_var)
}
if has_slice_call {
g.write(')')
}

View file

@ -0,0 +1,37 @@
@[heap]
struct Foo {
mut:
val int
}
@[heap]
struct Bar {
mut:
val int
}
interface FooBar {
mut:
val int
}
fn test_main() {
mut fbs := []&FooBar{}
fbs << &Foo{1}
a := &(fbs[0] as Foo)
println(a)
b := &(fbs.last() as Foo)
println(b)
arr1 := [(fbs.last() as Foo)]
arr2 := [&(fbs.last() as Foo)]
arr3 := [&(get_foo_bar() as Foo)]
println(arr1)
println(arr2)
println(arr3)
println(&(fbs.last() as Foo))
assert arr2[0] == arr3[0]
}
fn get_foo_bar() FooBar {
return Foo{1}
}