From a57e01fee7a097301f390cfa8b46bb84bf7e3891 Mon Sep 17 00:00:00 2001 From: Henrixounez <30901439+Henrixounez@users.noreply.github.com> Date: Thu, 1 Oct 2020 22:29:49 +0200 Subject: [PATCH] parser: dont allow single letter enums (#6526) --- vlib/v/checker/tests/enum_single_letter.out | 5 +++++ vlib/v/checker/tests/enum_single_letter.vv | 4 ++++ vlib/v/parser/parse_type.v | 2 +- vlib/v/parser/parser.v | 4 ++++ 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/enum_single_letter.out create mode 100644 vlib/v/checker/tests/enum_single_letter.vv diff --git a/vlib/v/checker/tests/enum_single_letter.out b/vlib/v/checker/tests/enum_single_letter.out new file mode 100644 index 0000000000..b509c5e2a5 --- /dev/null +++ b/vlib/v/checker/tests/enum_single_letter.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/enum_single_letter.vv:1:6: error: single letter capital names are reserved for generic template types. + 1 | enum E { + | ^ + 2 | v w + 3 | } diff --git a/vlib/v/checker/tests/enum_single_letter.vv b/vlib/v/checker/tests/enum_single_letter.vv new file mode 100644 index 0000000000..ef7bc65cec --- /dev/null +++ b/vlib/v/checker/tests/enum_single_letter.vv @@ -0,0 +1,4 @@ +enum E { + v w +} +println(E.v) diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 977058c9eb..ec596e3275 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -206,7 +206,7 @@ pub fn (mut p Parser) parse_any_type(language table.Language, is_ptr, check_dot } } else if p.expr_mod != '' { name = p.expr_mod + '.' + name - } else if p.mod != 'builtin' && name !in p.table.type_idxs && name.len > 1 { + } else if p.mod != 'builtin' && name.len > 1 && name !in p.table.type_idxs { // `Foo` in module `mod` means `mod.Foo` name = p.mod + '.' + name } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 214af5f08f..6461709d8e 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1723,6 +1723,10 @@ fn (mut p Parser) enum_decl() ast.EnumDecl { p.check(.key_enum) end_pos := p.tok.position() enum_name := p.check_name() + if enum_name.len == 1 { + p.error_with_pos('single letter capital names are reserved for generic template types.', + end_pos) + } name := p.prepend_mod(enum_name) p.check(.lcbr) enum_decl_comments := p.eat_comments()