checker: fix struct init update with generics (fix #20136) (#20139)

This commit is contained in:
shove 2023-12-11 06:59:50 +08:00 committed by GitHub
parent cfcbcb416a
commit 083eb3450b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -874,9 +874,10 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
if node.has_update_expr {
update_type := c.expr(mut node.update_expr)
node.update_expr_type = update_type
expr_sym := c.table.final_sym(c.unwrap_generic(update_type))
if node.update_expr is ast.ComptimeSelector {
c.error('cannot use struct update syntax in compile time expressions', node.update_expr_pos)
} else if c.table.final_sym(update_type).kind != .struct_ {
} else if expr_sym.kind != .struct_ {
s := c.table.type_to_str(update_type)
c.error('expected struct, found `${s}`', node.update_expr.pos())
} else if update_type != node.typ {

View File

@ -0,0 +1,20 @@
struct Foo {
a int
b string
isbool bool
}
fn do[T](f T) T {
return T{
...f
isbool: true
}
}
fn test_main() {
foo := do(Foo{
a: 1
b: '2'
})
assert foo.a == 1 && foo.b == '2' && foo.isbool
}