cgen: fix codegen for generic selector expr (fix #22974) (#23132)

This commit is contained in:
Felipe Pena 2024-12-11 11:05:25 -03:00 committed by GitHub
parent 09fff08611
commit 2d314fc9b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 1 deletions

View File

@ -4278,7 +4278,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
}
alias_to_ptr := sym.info is ast.Alias && sym.info.parent_type.is_ptr()
if field_is_opt
|| ((node.expr_type.is_ptr() || sym.kind == .chan || alias_to_ptr)
|| ((g.unwrap_generic(node.expr_type).is_ptr() || sym.kind == .chan || alias_to_ptr)
&& node.from_embed_types.len == 0)
|| (node.expr.is_as_cast() && g.inside_smartcast) {
g.write('->')

View File

@ -0,0 +1,19 @@
module main
struct Foo123 {
field string = 'foobar'
}
fn gen_func[T](value T) string {
$if T is i32 {
return '123'
} $else $if T is &Foo123 {
return value.field
} $else {
return '123'
}
}
fn test_main() {
assert gen_func[&Foo123](&Foo123{}) == 'foobar'
}