cgen: fix error for optional cast to interface (#13623)

pull/13629/head
yuyi 2022-03-02 01:21:55 +08:00 committed by GitHub
parent 6d57315aca
commit 7f8f571ed2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -2001,7 +2001,7 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr
if !got_is_ptr {
if !expr.is_lvalue()
|| (expr is ast.Ident && (expr as ast.Ident).obj.is_simple_define_const()) {
g.write('ADDR($got_styp, (')
g.write('HEAP($got_styp, (')
rparen_n += 2
} else {
g.write('&')

View File

@ -0,0 +1,40 @@
[heap]
struct Package {
str string
}
interface Parser {
main &Package
}
struct ParserV1 {
mut:
main &Package
}
fn new_0_parser() ?ParserV1 {
return ParserV1{
main: &Package{
str: 'test'
}
}
}
fn new_parser() ?Parser {
return Parser(new_0_parser() ?)
}
struct Engine {
parser Parser
}
fn test_cast_optional_to_interface() ? {
parser := new_parser() ?
assert parser.main.str == 'test'
eprintln(voidptr(parser.main))
e := Engine{
parser: parser
}
assert e.parser.main.str == 'test'
eprintln(voidptr(e.parser.main))
}