From 4242e7610fc2d2798e4a483963e4f908842510ac Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 5 May 2022 00:25:58 +0800 Subject: [PATCH] checker: add comptime_selector() (#14286) --- vlib/v/checker/checker.v | 23 +---------------------- vlib/v/checker/comptime.v | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 62b7b52f0c..abd604dc2a 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2663,28 +2663,7 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type { return c.comptime_call(mut node) } ast.ComptimeSelector { - node.left_type = c.expr(node.left) - expr_type := c.unwrap_generic(c.expr(node.field_expr)) - expr_sym := c.table.sym(expr_type) - if expr_type != ast.string_type { - c.error('expected `string` instead of `$expr_sym.name` (e.g. `field.name`)', - node.field_expr.pos()) - } - if mut node.field_expr is ast.SelectorExpr { - left_pos := node.field_expr.expr.pos() - if c.comptime_fields_type.len == 0 { - c.error('compile time field access can only be used when iterating over `T.fields`', - left_pos) - } - expr_name := node.field_expr.expr.str() - if expr_name in c.comptime_fields_type { - return c.comptime_fields_type[expr_name] - } - c.error('unknown `\$for` variable `$expr_name`', left_pos) - } else { - c.error('expected selector expression e.g. `$(field.name)`', node.field_expr.pos()) - } - return ast.void_type + return c.comptime_selector(mut node) } ast.ConcatExpr { return c.concat_expr(mut node) diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 8138105bef..0175364894 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -90,6 +90,30 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type { return f.return_type } +fn (mut c Checker) comptime_selector(mut node ast.ComptimeSelector) ast.Type { + node.left_type = c.expr(node.left) + expr_type := c.unwrap_generic(c.expr(node.field_expr)) + expr_sym := c.table.sym(expr_type) + if expr_type != ast.string_type { + c.error('expected `string` instead of `$expr_sym.name` (e.g. `field.name`)', node.field_expr.pos()) + } + if mut node.field_expr is ast.SelectorExpr { + left_pos := node.field_expr.expr.pos() + if c.comptime_fields_type.len == 0 { + c.error('compile time field access can only be used when iterating over `T.fields`', + left_pos) + } + expr_name := node.field_expr.expr.str() + if expr_name in c.comptime_fields_type { + return c.comptime_fields_type[expr_name] + } + c.error('unknown `\$for` variable `$expr_name`', left_pos) + } else { + c.error('expected selector expression e.g. `$(field.name)`', node.field_expr.pos()) + } + return ast.void_type +} + fn (mut c Checker) comptime_for(node ast.ComptimeFor) { typ := c.unwrap_generic(node.typ) sym := c.table.sym(typ)