diff --git a/vlib/net/unix/use_net_and_net_unix_together_test.v b/vlib/net/unix/use_net_and_net_unix_together_test.v index f4cdf77661..32458f7870 100644 --- a/vlib/net/unix/use_net_and_net_unix_together_test.v +++ b/vlib/net/unix/use_net_and_net_unix_together_test.v @@ -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!' diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 5620dc5952..853041390f 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 } // diff --git a/vlib/v/parser/tests/defer_propagate2.out b/vlib/v/parser/tests/defer_propagate2.out new file mode 100644 index 0000000000..89962fb1c6 --- /dev/null +++ b/vlib/v/parser/tests/defer_propagate2.out @@ -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 diff --git a/vlib/v/parser/tests/defer_propagate2.vv b/vlib/v/parser/tests/defer_propagate2.vv new file mode 100644 index 0000000000..65c3b8e291 --- /dev/null +++ b/vlib/v/parser/tests/defer_propagate2.vv @@ -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() ?) +}