1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: add missing check for mismatch anon struct to typed struct (#18250)

This commit is contained in:
Felipe Pena 2023-05-24 21:53:14 -03:00 committed by GitHub
parent 64a4a3316a
commit 9d56432e55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -698,6 +698,9 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
}
}
}
if left_sym.kind == .struct_ && right is ast.StructInit && (right as ast.StructInit).is_anon {
c.error('cannot assign anonymous `struct` to a typed `struct`', right.pos())
}
}
// this needs to run after the assign stmt left exprs have been run through checker
// so that ident.obj is set

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/anon_struct_assign_err.vv:9:12: error: cannot assign anonymous `struct` to a typed `struct`
7 | }
8 | println(y)
9 | y = struct {
| ^
10 | name: 'Def'
11 | }

View File

@ -0,0 +1,12 @@
struct Abc {
name string
}
mut y := Abc{
name: 'Abc'
}
println(y)
y = struct {
name: 'Def'
}
println(y)