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

parser: check error for defer propagate (fix #13534) (#13536)

This commit is contained in:
yuyi 2022-02-20 18:29:08 +08:00 committed by GitHub
parent 75ebac006d
commit 04654ed518
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 2 deletions

View File

@ -11,12 +11,12 @@ fn test_that_net_and_net_unix_can_be_imported_together_without_conflicts() ? {
mut l := unix.listen_stream(test_port) or { panic(err) }
go echo_server(mut l)
defer {
l.close() ?
l.close() or {}
}
//
mut c := unix.connect_stream(test_port) ?
defer {
c.close() ?
c.close() or {}
}
//
data := 'Hello from vlib/net!'

View File

@ -2694,6 +2694,10 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
// `foo()?`
if p.tok.kind == .question {
p.next()
if p.inside_defer {
p.error_with_pos('error propagation not allowed inside `defer` blocks',
p.prev_tok.pos())
}
or_kind = .propagate
}
//

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/defer_propagate2.vv:16:13: error: error propagation not allowed inside `defer` blocks
14 | dump(s.x)
15 | defer {
16 | s.close() ?
| ^
17 | }
18 | return s.x

View File

@ -0,0 +1,23 @@
struct Abc {
mut:
x int = 123
}
fn (mut s Abc) close() ? {
println('> CLOSE 1 s.x: $s.x')
s.x = -1
println('> CLOSE 2 s.x: $s.x')
}
fn opt2() ?int {
mut s := Abc{}
dump(s.x)
defer {
s.close() ?
}
return s.x
}
fn main() {
println(opt2() ?)
}