parser: dont allow single letter enums (#6526)

pull/6529/head
Henrixounez 2020-10-01 22:29:49 +02:00 committed by GitHub
parent f55f2fb9d1
commit a57e01fee7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 1 deletions

View File

@ -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 | }

View File

@ -0,0 +1,4 @@
enum E {
v w
}
println(E.v)

View File

@ -206,7 +206,7 @@ pub fn (mut p Parser) parse_any_type(language table.Language, is_ptr, check_dot
} }
} else if p.expr_mod != '' { } else if p.expr_mod != '' {
name = p.expr_mod + '.' + name 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` // `Foo` in module `mod` means `mod.Foo`
name = p.mod + '.' + name name = p.mod + '.' + name
} }

View File

@ -1723,6 +1723,10 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
p.check(.key_enum) p.check(.key_enum)
end_pos := p.tok.position() end_pos := p.tok.position()
enum_name := p.check_name() 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) name := p.prepend_mod(enum_name)
p.check(.lcbr) p.check(.lcbr)
enum_decl_comments := p.eat_comments() enum_decl_comments := p.eat_comments()