diff --git a/compiler/parser.v b/compiler/parser.v index 7a73a7b791..6eec2b79b1 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -1142,6 +1142,13 @@ fn (p mut Parser) var_decl() { p.next() p.check(LCBR) p.genln('if (!$tmp .ok) {') + p.register_var(Var { + name: 'err' + typ: 'string' + is_mut: false + is_used: true + }) + p.genln('string err = $tmp . error;') p.statements() p.genln('$typ $name = *($typ*) $tmp . data;') if !p.returns && p.prev_tok2 != CONTINUE && p.prev_tok2 != BREAK { diff --git a/compiler/tests/option_test.v b/compiler/tests/option_test.v new file mode 100644 index 0000000000..fbbf0f614f --- /dev/null +++ b/compiler/tests/option_test.v @@ -0,0 +1,10 @@ +fn opt_err() ?string {return error('hi')} + +fn test_err(){ + v := opt_err() or { + assert err == 'hi' + return + } + assert false + println(v) // suppress not used error +}