diff --git a/vlib/v/checker/tests/option_and_result_err.out b/vlib/v/checker/tests/option_and_result_err.out new file mode 100644 index 00000000000000..b96b70dad5f291 --- /dev/null +++ b/vlib/v/checker/tests/option_and_result_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/option_and_result_err.vv:1:11: error: the type must be Option or Result + 1 | fn foo() !?i32 { + | ^ + 2 | return 6 + 3 | } diff --git a/vlib/v/checker/tests/option_and_result_err.vv b/vlib/v/checker/tests/option_and_result_err.vv new file mode 100644 index 00000000000000..fcf335af69e9e8 --- /dev/null +++ b/vlib/v/checker/tests/option_and_result_err.vv @@ -0,0 +1,3 @@ +fn foo() !?i32 { + return 6 +} \ No newline at end of file diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 900c003a8e9a09..e386c7cc9277a9 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -465,9 +465,22 @@ fn (mut p Parser) parse_type() ast.Type { if p.tok.kind == .question { p.next() is_option = true + if p.tok.kind == .not { + p.next() + is_result = true + } } else if p.tok.kind == .not { p.next() is_result = true + if p.tok.kind == .question { + p.next() + is_option = true + } + } + + if is_option && is_result { + p.error_with_pos('the type must be Option or Result', p.prev_tok.pos()) + return 0 } if is_option || is_result { diff --git a/vlib/v/parser/tests/option_result_err.out b/vlib/v/parser/tests/option_result_err.out index 7d83b6738425ff..c35fe8883f3f93 100644 --- a/vlib/v/parser/tests/option_result_err.out +++ b/vlib/v/parser/tests/option_result_err.out @@ -1,5 +1,5 @@ -vlib/v/parser/tests/option_result_err.vv:2:2: error: invalid expression: unexpected keyword `return` +vlib/v/parser/tests/option_result_err.vv:1:11: error: the type must be Option or Result 1 | fn abc() ?!string { + | ^ 2 | return '' - | ~~~~~~ 3 | } diff --git a/vlib/v/parser/tests/result_option_err.out b/vlib/v/parser/tests/result_option_err.out index 4375d046a3a5b2..41b050d430741c 100644 --- a/vlib/v/parser/tests/result_option_err.out +++ b/vlib/v/parser/tests/result_option_err.out @@ -1,5 +1,5 @@ -vlib/v/parser/tests/result_option_err.vv:2:2: error: invalid expression: unexpected keyword `return` +vlib/v/parser/tests/result_option_err.vv:1:11: error: the type must be Option or Result 1 | fn abc() ?!string { + | ^ 2 | return '' - | ~~~~~~ 3 | }