Skip to content

Commit

Permalink
checker: extend byte deprecation warning to struct fields (#19653)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Oct 28, 2023
1 parent 573188a commit 705eea8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion vlib/v/ast/type_size_test.v
Expand Up @@ -4,7 +4,7 @@ import v.pref

struct T01 {
a int
b byte
b u8
c int
}

Expand Down
48 changes: 27 additions & 21 deletions vlib/v/checker/struct.v
Expand Up @@ -131,30 +131,37 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
}
}
}
if sym.kind == .struct_ {
info := sym.info as ast.Struct
if info.is_heap && !field.typ.is_ptr() {
struct_sym.info.is_heap = true
match sym.kind {
.struct_ {
info := sym.info as ast.Struct
if info.is_heap && !field.typ.is_ptr() {
struct_sym.info.is_heap = true
}
for ct in info.concrete_types {
ct_sym := c.table.sym(ct)
if ct_sym.kind == .placeholder {
c.error('unknown type `${ct_sym.name}`', field.type_pos)
}
}
}
.multi_return {
c.error('cannot use multi return as field type', field.type_pos)
}
.none_ {
c.error('cannot use `none` as field type', field.type_pos)
}
for ct in info.concrete_types {
ct_sym := c.table.sym(ct)
if ct_sym.kind == .placeholder {
c.error('unknown type `${ct_sym.name}`', field.type_pos)
.map {
info := sym.map_info()
if info.value_type.has_flag(.result) {
c.error('cannot use Result type as map value type', field.type_pos)
}
}
}
if sym.kind == .multi_return {
c.error('cannot use multi return as field type', field.type_pos)
}

if sym.kind == .none_ {
c.error('cannot use `none` as field type', field.type_pos)
}
if sym.kind == .map {
info := sym.map_info()
if info.value_type.has_flag(.result) {
c.error('cannot use Result type as map value type', field.type_pos)
.alias {
if sym.name == 'byte' {
c.warn('byte is deprecated, use u8 instead', field.type_pos)
}
}
else {}
}

if field.has_default_expr {
Expand Down Expand Up @@ -208,7 +215,6 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
}
}
}

if field.typ.has_flag(.option) {
if field.default_expr is ast.None {
c.warn('unnecessary default value of `none`: struct fields are zeroed by default',
Expand Down

0 comments on commit 705eea8

Please sign in to comment.