From af19aa5096b90588a73bcc86421c9735e8f20775 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sat, 6 Jul 2019 13:52:50 +0100 Subject: [PATCH] Fix #1021 - Optional `or` branch doesn't define `err` --- compiler/parser.v | 7 +++++++ compiler/tests/option_test.v | 10 ++++++++++ 2 files changed, 17 insertions(+) create mode 100644 compiler/tests/option_test.v 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 +}