From 4051ce869c258c0b1b3bcec9bf95cf0bf23a72f0 Mon Sep 17 00:00:00 2001 From: Larpon Date: Thu, 5 Nov 2020 11:59:49 +0100 Subject: [PATCH] all: remove unused enum value and improve error message for `@` tokens in scanner (#6751) --- vlib/v/checker/checker.v | 5 +++-- vlib/v/scanner/scanner.v | 7 ++++++- vlib/v/token/token.v | 2 -- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b564d66203..01a118ec16 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3053,8 +3053,9 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) table.Type { } node.val = c.vmod_file_content } - .unknown, ._end_ { - c.error('unknown @ identifier: $node.name', node.pos) + .unknown { + c.error('unknown @ identifier: ${node.name}. Available identifiers: $token.valid_at_tokens', + node.pos) } } return table.string_type diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 965b742220..8a556aa494 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -760,7 +760,12 @@ fn (mut s Scanner) text_scan() token.Token { return s.new_token(.at, '@' + name, name.len + 1) } if !token.is_key(name) { - s.error('@ must be used before keywords (e.g. `@type string`)') + mut at_error_msg := '@ must be used before keywords or compile time variables (e.g. `@type string` or `@FN`)' + // If name is all uppercase, the user is probably looking for a compile time variable ("at-token") + if name.is_upper() { + at_error_msg += '\nAvailable compile time variables:\n$token.valid_at_tokens' + } + s.error(at_error_msg) } return s.new_token(.name, name, name.len) } diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 1585eeb077..02d5ab8c54 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -164,11 +164,9 @@ pub enum AtKind { column_nr vhash vmod_file - _end_ } const ( valid_at_tokens = ['@FN','@MOD','@STRUCT','@VEXE','@FILE','@LINE','@COLUMN','@VHASH','@VMOD_FILE'] - //valid_at_tokens_len = int(AtKind._end_) ) // build_keys genereates a map with keywords' string values: