mirror of
https://github.com/vlang/v.git
synced 2025-08-04 02:07:28 -04:00
cgen: fix codegen for sumtype casting on selector on as cast with non pointer field (fix #23387) (#23391)
This commit is contained in:
parent
f06ff69bb9
commit
0c9d0762c7
@ -2713,6 +2713,10 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr
|
|||||||
// Note: the `_to_sumtype_` family of functions do call memdup internally, making
|
// Note: the `_to_sumtype_` family of functions do call memdup internally, making
|
||||||
// another duplicate with the HEAP macro is redundant, so use ADDR instead:
|
// another duplicate with the HEAP macro is redundant, so use ADDR instead:
|
||||||
if expr.is_as_cast() {
|
if expr.is_as_cast() {
|
||||||
|
if !got_is_ptr && expr is ast.SelectorExpr {
|
||||||
|
// (var as Type).field_non_ptr
|
||||||
|
g.write('&')
|
||||||
|
}
|
||||||
old_inside_smartcast := g.inside_smartcast
|
old_inside_smartcast := g.inside_smartcast
|
||||||
g.inside_smartcast = true
|
g.inside_smartcast = true
|
||||||
defer {
|
defer {
|
||||||
|
26
vlib/v/tests/selector_as_cast_test.v
Normal file
26
vlib/v/tests/selector_as_cast_test.v
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
struct Foo {
|
||||||
|
expr SumType
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Bar {
|
||||||
|
expr SumType
|
||||||
|
}
|
||||||
|
|
||||||
|
type SumType = Foo | string | Bar
|
||||||
|
type SumType2 = SumType | int
|
||||||
|
|
||||||
|
struct Gen {}
|
||||||
|
|
||||||
|
fn (g Gen) t(arg SumType2) {
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
gen := Gen{}
|
||||||
|
s := Bar{
|
||||||
|
expr: Foo{
|
||||||
|
expr: 'foobar'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gen.t((s.expr as Foo).expr)
|
||||||
|
assert true
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user