From 85bcfdd636ca5882297cb4bbae872c7177ab824f Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 19 Jan 2021 09:28:34 +0200 Subject: [PATCH] checker: handle `a[i] or { statements expr }` the same as the other or blocks --- vlib/v/checker/checker.v | 4 ++++ vlib/v/tests/array_map_or_test.v | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5f731758cf..3ee80560f9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1940,6 +1940,10 @@ pub fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type table.Type) t c.error('unexpected `?`, the function `$expr.name` does not return an optional', expr.or_block.pos) } + } else if expr is ast.IndexExpr { + if expr.or_expr.kind != .absent { + c.check_or_expr(expr.or_expr, ret_type, ret_type) + } } return ret_type } diff --git a/vlib/v/tests/array_map_or_test.v b/vlib/v/tests/array_map_or_test.v index ba76ffeeca..30e26df42a 100644 --- a/vlib/v/tests/array_map_or_test.v +++ b/vlib/v/tests/array_map_or_test.v @@ -3,25 +3,33 @@ fn test_array_or() { mut testvar := 17 el := m[4] or { testvar = -43 + 999 } good := m[1] or { testvar = 11 + 0 } assert testvar == -43 - assert el == 0 + assert el == 999 assert good == 4 } fn test_map_or() { - m := {'as': 3, 'qw': 4, 'kl': 5} + m := { + 'as': 3 + 'qw': 4 + 'kl': 5 + } mut testvar := -21 el := m['pp'] or { testvar = 7931 + 7 } good := m['kl'] or { testvar = -45 + 999 } assert testvar == 7931 - assert el == 0 + assert el == 7 assert good == 5 }