parser: fix struct field fn type with default value (fix #19099) (#19106)

This commit is contained in:
yuyi 2023-08-11 18:50:54 +08:00 committed by GitHub
parent 387f717004
commit ec45e4736a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -424,7 +424,7 @@ fn (mut p Parser) parse_type() ast.Type {
is_required_field := p.inside_struct_field_decl && p.tok.kind == .lsbr is_required_field := p.inside_struct_field_decl && p.tok.kind == .lsbr
&& p.peek_tok.kind == .name && p.peek_tok.lit == 'required' && p.peek_tok.kind == .name && p.peek_tok.lit == 'required'
if p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar] || is_required_field { if p.tok.line_nr > line_nr || p.tok.kind in [.comma, .rpar, .assign] || is_required_field {
mut typ := ast.void_type mut typ := ast.void_type
if is_option { if is_option {
typ = typ.set_flag(.option) typ = typ.set_flag(.option)

View File

@ -0,0 +1,25 @@
struct Flip {
name string = 'NULL'
execute fn () ! = unsafe { nil }
}
fn (flip Flip) exec() ! {
if isnil(flip.execute) {
return
}
println('Executing ${flip.name}')
flip.execute()!
}
fn test_struct_field_default_fn_type_value() {
fl := Flip{
name: 'a function'
execute: fn () ! {
println('Hello, World!')
}
}
fl.exec()!
assert true
}