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

cgen: execute defer block *after* return expression is evaluated (#9893)

This commit is contained in:
Uwe Krüger
2021-04-27 00:42:16 +02:00
committed by GitHub
parent 4eb8072882
commit 787a63dab6
5 changed files with 273 additions and 118 deletions

View File

@@ -359,7 +359,7 @@ pub fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_d
return p.parse_multi_return_type()
}
else {
// no defer
// no p.next()
if name == 'map' {
return p.parse_map_type()
}
@@ -369,79 +369,80 @@ pub fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_d
if name == 'thread' {
return p.parse_thread_type()
}
defer {
p.next()
}
mut ret := ast.Type(0)
if name == '' {
// This means the developer is using some wrong syntax like `x: int` instead of `x int`
p.error('expecting type declaration')
return 0
}
match name {
'voidptr' {
return ast.voidptr_type
}
'byteptr' {
return ast.byteptr_type
}
'charptr' {
return ast.charptr_type
}
'i8' {
return ast.i8_type
}
'i16' {
return ast.i16_type
}
'int' {
return ast.int_type
}
'i64' {
return ast.i64_type
}
'byte' {
return ast.byte_type
}
'u16' {
return ast.u16_type
}
'u32' {
return ast.u32_type
}
'u64' {
return ast.u64_type
}
'f32' {
return ast.f32_type
}
'f64' {
return ast.f64_type
}
'string' {
return ast.string_type
}
'char' {
return ast.char_type
}
'bool' {
return ast.bool_type
}
'float_literal' {
return ast.float_literal_type
}
'int_literal' {
return ast.int_literal_type
}
else {
if name.len == 1 && name[0].is_capital() {
return p.parse_generic_template_type(name)
} else {
match name {
'voidptr' {
ret = ast.voidptr_type
}
if p.peek_tok.kind == .lt {
return p.parse_generic_struct_inst_type(name)
'byteptr' {
ret = ast.byteptr_type
}
'charptr' {
ret = ast.charptr_type
}
'i8' {
ret = ast.i8_type
}
'i16' {
ret = ast.i16_type
}
'int' {
ret = ast.int_type
}
'i64' {
ret = ast.i64_type
}
'byte' {
ret = ast.byte_type
}
'u16' {
ret = ast.u16_type
}
'u32' {
ret = ast.u32_type
}
'u64' {
ret = ast.u64_type
}
'f32' {
ret = ast.f32_type
}
'f64' {
ret = ast.f64_type
}
'string' {
ret = ast.string_type
}
'char' {
ret = ast.char_type
}
'bool' {
ret = ast.bool_type
}
'float_literal' {
ret = ast.float_literal_type
}
'int_literal' {
ret = ast.int_literal_type
}
else {
p.next()
if name.len == 1 && name[0].is_capital() {
return p.parse_generic_template_type(name)
}
if p.tok.kind == .lt {
return p.parse_generic_struct_inst_type(name)
}
return p.parse_enum_or_struct_type(name, language)
}
return p.parse_enum_or_struct_type(name, language)
}
}
p.next()
return ret
}
}
}