Skip to content

Commit

Permalink
checker
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Nov 20, 2023
1 parent 0718fd6 commit aa560c2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
8 changes: 4 additions & 4 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ pub fn (mut c Checker) check_files(ast_files []&ast.File) {
}
}
// After the main checker run, run the line info check, print line info, and exit (if it's present)
if c.pref.line_info != '' && !c.pref.linfo.is_running { //'' && c.pref.linfo.line_nr == 0 {
if !c.pref.linfo.is_running && c.pref.line_info != '' { //'' && c.pref.linfo.line_nr == 0 {
// c.do_line_info(c.pref.line_info, ast_files)
println('setting is_running=true, pref.path=${c.pref.linfo.path} curdir' + os.getwd())
c.pref.linfo.is_running = true
Expand Down Expand Up @@ -963,7 +963,7 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
styp := c.table.type_to_str(utyp)
typ_sym := c.table.sym(utyp)
mut inter_sym := c.table.sym(interface_type)
if inter_sym.mod !in [typ_sym.mod, c.mod] && !inter_sym.is_pub && typ_sym.mod != 'builtin' {
if !inter_sym.is_pub && inter_sym.mod !in [typ_sym.mod, c.mod] && typ_sym.mod != 'builtin' {
c.error('`${styp}` cannot implement private interface `${inter_sym.name}` of other module',
pos)
return false
Expand Down Expand Up @@ -2321,7 +2321,7 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) {
c.error('hash statements are only allowed in backend specific files such "x.js.v" and "x.go.v"',
node.pos)
}
if c.mod == 'main' && c.pref.backend != .golang {
if c.pref.backend != .golang && c.mod == 'main' {
c.error('hash statements are not allowed in the main module. Place them in a separate module.',
node.pos)
}
Expand Down Expand Up @@ -4600,7 +4600,7 @@ fn (c &Checker) check_struct_signature(from ast.Struct, to ast.Struct) bool {
fn (mut c Checker) fetch_field_name(field ast.StructField) string {
mut name := field.name
for attr in field.attrs {
if attr.kind == .string && attr.name == 'sql' && attr.arg != '' {
if attr.kind == .string && attr.arg != '' && attr.name == 'sql' {
name = attr.arg
break
}
Expand Down
15 changes: 8 additions & 7 deletions vlib/v/checker/errors.v
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,14 @@ fn (mut c Checker) deprecate(kind string, name string, attrs []ast.Attr, pos tok
now := time.now()
mut after_time := now
for attr in attrs {
if attr.name == 'deprecated' && attr.arg != '' {
deprecation_message = attr.arg
}
if attr.name == 'deprecated_after' && attr.arg != '' {
after_time = time.parse_iso8601(attr.arg) or {
c.error('invalid time format', attr.pos)
now
if attr.arg != '' {
if attr.name == 'deprecated' {
deprecation_message = attr.arg
} else if attr.name == 'deprecated_after' {
after_time = time.parse_iso8601(attr.arg) or {
c.error('invalid time format', attr.pos)
now
}
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
node.params[1].typ) {
c.error('expected `${receiver_sym.name}` not `${param_sym.name}` - both operands must be the same type for operator overloading',
node.params[1].type_pos)
} else if node.name in ['<', '=='] && node.return_type != ast.bool_type {
} else if node.return_type != ast.bool_type && node.name in ['<', '=='] {
c.error('operator comparison methods should return `bool`', node.pos)
} else if parent_sym.is_primitive() {
// aliases of primitive types are explicitly allowed
Expand Down Expand Up @@ -728,7 +728,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
}
}
panic('unreachable')
} else if node.args.len > 0 && fn_name == 'json.encode' && node.args[0].typ.has_flag(.shared_f) {
} else if node.args.len > 0 && node.args[0].typ.has_flag(.shared_f) && fn_name == 'json.encode' {
c.error('json.encode cannot handle shared data', node.pos)
return ast.void_type
} else if node.args.len > 0 && fn_name == 'json.decode' {
Expand Down Expand Up @@ -801,7 +801,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
}
}
// try prefix with current module as it would have never gotten prefixed
if !found && !fn_name.contains('.') && node.mod != 'builtin' {
if !found && node.mod != 'builtin' && !fn_name.contains('.') {
name_prefixed := '${node.mod}.${fn_name}'
if f := c.table.find_fn(name_prefixed) {
node.name = name_prefixed
Expand Down Expand Up @@ -2572,10 +2572,7 @@ fn (mut c Checker) map_builtin_method_call(mut node ast.CallExpr, left_type ast.
c.table.find_or_register_array(info.value_type)
}
ret_type = ast.Type(typ)
if method_name == 'keys' && info.key_type.has_flag(.generic) {
ret_type = ret_type.set_flag(.generic)
}
if method_name == 'values' && info.value_type.has_flag(.generic) {
if info.key_type.has_flag(.generic) {
ret_type = ret_type.set_flag(.generic)
}
}
Expand Down Expand Up @@ -2614,7 +2611,7 @@ fn (mut c Checker) ensure_same_array_return_type(mut node ast.CallExpr, left_typ
fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type ast.Type, left_sym ast.TypeSymbol) ast.Type {
method_name := node.name
mut elem_typ := ast.void_type
if method_name == 'slice' && !c.is_builtin_mod {
if !c.is_builtin_mod && method_name == 'slice' {
c.error('.slice() is a private method, use `x[start..end]` instead', node.pos)
return ast.void_type
}
Expand Down Expand Up @@ -2646,7 +2643,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
// position of `it` doesn't matter
scope_register_it(mut node.scope, node.pos, elem_typ)
}
} else if method_name == 'sorted_with_compare' && node.args.len == 1 {
} else if node.args.len == 1 && method_name == 'sorted_with_compare' {
if node.args.len > 0 && mut node.args[0].expr is ast.LambdaExpr {
c.support_lambda_expr_in_sort(elem_typ.ref(), ast.int_type, mut node.args[0].expr)
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
struct_sym.info.fields.sort_with_compare(minify_sort_fn)
}
for attr in node.attrs {
if attr.name == 'typedef' && node.language != .c {
if node.language != .c && attr.name == 'typedef' {
c.error('`typedef` attribute can only be used with C structs', node.pos)
}
}
Expand Down Expand Up @@ -827,7 +827,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
}
*/
// Check for `[required]` struct attr
if field.attrs.contains('required') && !node.no_keys && !node.has_update_expr {
if !node.no_keys && !node.has_update_expr && field.attrs.contains('required') {
mut found := false
for init_field in node.init_fields {
if field.name == init_field.name {
Expand Down

0 comments on commit aa560c2

Please sign in to comment.