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

anon expr

This commit is contained in:
Alexander Medvednikov 2022-07-08 21:09:02 +03:00
parent 70890b27a5
commit 7ee7932f5e
3 changed files with 15 additions and 4 deletions

View File

@ -267,8 +267,13 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
&& c.table.cur_concrete_types.len == 0 {
pos := type_sym.name.last_index('.') or { -1 }
first_letter := type_sym.name[pos + 1]
if !first_letter.is_capital() && type_sym.kind != .placeholder {
c.error('cannot initialize builtin type `$type_sym.name`', node.pos)
if !first_letter.is_capital() && type_sym.kind != .placeholder
&& !type_sym.name.starts_with('_VAnonStruct') {
s := type_sym.name
x := s.starts_with('_')
c.error('cannot initialize builtin type1 $x "$s" "${s[..4]}" `$type_sym.name`',
node.pos)
}
}
if type_sym.kind == .sum_type && node.fields.len == 1 {

View File

@ -325,6 +325,11 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr {
node = p.map_init()
p.check(.rcbr)
}
.key_struct {
// Anonymous struct
p.next()
return p.struct_init('', false)
}
.key_fn {
if p.expecting_type {
// Anonymous function type

View File

@ -425,9 +425,10 @@ struct Book {
}
fn test_anon() {
empty_book := Book{} // author:struct{'sdf', 23}}
empty_book := Book{}
assert empty_book.author.age == 0
assert empty_book.author.name == ''
println(empty_book.author.age)
book := Book{ author:struct{'sdf', 23}}
}