From 55575fd7bdbb2140f96578b70f8bdc6a60651666 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 31 Aug 2023 00:34:07 +0300 Subject: [PATCH] checker: optimise out needless string interpolations from the most common case in Checker.expr_or_block_err --- vlib/v/checker/checker.v | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index e9d2cdbbf2..94e15fa7ae 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1064,23 +1064,32 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to return true } -fn (mut c Checker) expr_or_block_err(kind ast.OrKind, expr_name string, pos token.Pos, is_field bool) { - obj_does_not_return_or_is_not := if is_field { +// helper for expr_or_block_err +fn is_field_to_description(expr_name string, is_field bool) string { + return if is_field { 'field `${expr_name}` is not' } else { 'function `${expr_name}` does not return' } +} + +fn (mut c Checker) expr_or_block_err(kind ast.OrKind, expr_name string, pos token.Pos, is_field bool) { match kind { - .absent {} + .absent { + // do nothing, most common case; do not be tempted to move the call to is_field_to_description above it, since that will slow it down + } .block { + obj_does_not_return_or_is_not := is_field_to_description(expr_name, is_field) c.error('unexpected `or` block, the ${obj_does_not_return_or_is_not} an Option or a Result', pos) } .propagate_option { + obj_does_not_return_or_is_not := is_field_to_description(expr_name, is_field) c.error('unexpected `?`, the ${obj_does_not_return_or_is_not} an Option', pos) } .propagate_result { + obj_does_not_return_or_is_not := is_field_to_description(expr_name, is_field) c.error('unexpected `!`, the ${obj_does_not_return_or_is_not} a Result', pos) } }