Fix #1021 - Optional `or` branch doesn't define `err`

pull/1046/head
Nick Treleaven 2019-07-06 13:52:50 +01:00 committed by Alexander Medvednikov
parent 8a2d25247f
commit af19aa5096
2 changed files with 17 additions and 0 deletions

View File

@ -1142,6 +1142,13 @@ fn (p mut Parser) var_decl() {
p.next() p.next()
p.check(LCBR) p.check(LCBR)
p.genln('if (!$tmp .ok) {') 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.statements()
p.genln('$typ $name = *($typ*) $tmp . data;') p.genln('$typ $name = *($typ*) $tmp . data;')
if !p.returns && p.prev_tok2 != CONTINUE && p.prev_tok2 != BREAK { if !p.returns && p.prev_tok2 != CONTINUE && p.prev_tok2 != BREAK {

View File

@ -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
}