parser: unified way to handle struct and enum name check

pull/4865/head
yuyi 2020-05-12 19:39:32 +08:00 committed by GitHub
parent 712fd384ee
commit 99b31d8241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 19 deletions

View File

@ -212,17 +212,6 @@ pub fn (mut c Checker) type_decl(node ast.TypeDecl) {
} }
pub fn (mut c Checker) struct_decl(decl ast.StructDecl) { pub fn (mut c Checker) struct_decl(decl ast.StructDecl) {
splitted_full_name := decl.name.split('.')
is_builtin := splitted_full_name[0] == 'builtin'
name := splitted_full_name.last()
if !name[0].is_capital() && !decl.is_c && !is_builtin && name !in table.builtin_type_names {
pos := token.Position{
line_nr: decl.pos.line_nr
pos: decl.pos.pos + 7
len: name.len
}
c.error('struct name must begin with capital letter', pos)
}
for i, field in decl.fields { for i, field in decl.fields {
for j in 0 .. i { for j in 0 .. i {
if field.name == decl.fields[j].name { if field.name == decl.fields[j].name {
@ -245,15 +234,12 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) {
if !c.table.check(field_expr_type, field.typ) { if !c.table.check(field_expr_type, field.typ) {
field_expr_type_sym := c.table.get_type_symbol(field_expr_type) field_expr_type_sym := c.table.get_type_symbol(field_expr_type)
field_type_sym := c.table.get_type_symbol(field.typ) field_type_sym := c.table.get_type_symbol(field.typ)
field_name := field.name c.error('default expression for field `${field.name}` ' +
fet_name := field_expr_type_sym.name 'has type `${field_expr_type_sym.name}`, but should be `${field_type_sym.name}`',
ft_name := field_type_sym.name
c.error('default expression for field `${field_name}` ' + 'has type `${fet_name}`, but should be `${ft_name}`',
field.default_expr.position()) field.default_expr.position())
} }
} }
} }
// && (p.tok.lit[0].is_capital() || is_c || (p.builtin_mod && Sp.tok.lit in table.builtin_type_names))
} }
pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type {

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/enum_name_err.v:1:6: error: enum name `color` must begin with a capital letter vlib/v/checker/tests/enum_name_err.v:1:6: error: enum name `color` must begin with capital letter
1 | enum color { 1 | enum color {
| ~~~~~ | ~~~~~
2 | green 2 | green

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/struct_name.v:1:8: error: struct name must begin with capital letter vlib/v/checker/tests/struct_name.v:1:8: error: struct name `abc` must begin with capital letter
1 | struct abc { 1 | struct abc {
| ~~~ | ~~~
2 | 2 |

View File

@ -1190,7 +1190,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
end_pos := p.tok.position() end_pos := p.tok.position()
enum_name := p.check_name() enum_name := p.check_name()
if enum_name.len > 0 && !enum_name[0].is_capital() { if enum_name.len > 0 && !enum_name[0].is_capital() {
p.error_with_pos('enum name `$enum_name` must begin with a capital letter', end_pos) p.error_with_pos('enum name `$enum_name` must begin with capital letter', end_pos)
} }
name := p.prepend_mod(enum_name) name := p.prepend_mod(enum_name)
p.check(.lcbr) p.check(.lcbr)

View File

@ -33,6 +33,9 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
} }
end_pos := p.tok.position() end_pos := p.tok.position()
mut name := p.check_name() mut name := p.check_name()
if !is_c && !is_js && p.mod != 'builtin' && name.len > 0 && !name[0].is_capital() {
p.error_with_pos('struct name `$name` must begin with capital letter', end_pos)
}
// println('struct decl $name') // println('struct decl $name')
mut ast_fields := []ast.StructField{} mut ast_fields := []ast.StructField{}
mut fields := []table.Field{} mut fields := []table.Field{}