all: if smartcast part 2 (#5754)
parent
7ad03e9d6a
commit
5ea17ad2d4
|
@ -98,10 +98,9 @@ fn (app App) gen_api_for_module_in_os(mod_name, os_name string) string {
|
|||
for f in b.parsed_files {
|
||||
for s in f.stmts {
|
||||
if s is ast.FnDecl {
|
||||
fnd := s as ast.FnDecl
|
||||
if fnd.is_pub {
|
||||
fn_signature := fnd.stringify(b.table)
|
||||
fn_mod := fnd.modname()
|
||||
if it.is_pub {
|
||||
fn_signature := it.stringify(b.table)
|
||||
fn_mod := it.modname()
|
||||
if fn_mod == mod_name {
|
||||
fline := '${fn_mod}: $fn_signature'
|
||||
res << fline
|
||||
|
|
|
@ -33,8 +33,7 @@ fn wait() {
|
|||
|
||||
fn (l Lander) land(w World) {
|
||||
if w is Mars {
|
||||
m := w as Mars
|
||||
for m.dust_storm() {
|
||||
for it.dust_storm() {
|
||||
wait()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -279,8 +279,7 @@ pub fn (node Stmt) str() string {
|
|||
mut out := ''
|
||||
for i, left in it.left {
|
||||
if left is Ident {
|
||||
ident := left as Ident
|
||||
var_info := ident.var_info()
|
||||
var_info := it.var_info()
|
||||
if var_info.is_mut {
|
||||
out += 'mut '
|
||||
}
|
||||
|
|
|
@ -288,9 +288,8 @@ fn (b &Builder) print_warnings_and_errors() {
|
|||
for file in b.parsed_files {
|
||||
for stmt in file.stmts {
|
||||
if stmt is ast.FnDecl {
|
||||
f := stmt as ast.FnDecl
|
||||
if f.name == fn_name {
|
||||
fline := f.pos.line_nr
|
||||
if it.name == fn_name {
|
||||
fline := it.pos.line_nr
|
||||
println('$file.path:$fline:')
|
||||
}
|
||||
}
|
||||
|
|
|
@ -206,22 +206,18 @@ fn (mut c Checker) check_file_in_main(file ast.File) bool {
|
|||
}
|
||||
}
|
||||
ast.TypeDecl {
|
||||
// type_decl := stmt as ast.TypeDecl
|
||||
if stmt is ast.AliasTypeDecl {
|
||||
alias_decl := stmt as ast.AliasTypeDecl
|
||||
if alias_decl.is_pub {
|
||||
c.warn('type alias `$alias_decl.name` $no_pub_in_main_warning',
|
||||
alias_decl.pos)
|
||||
if it.is_pub {
|
||||
c.warn('type alias `$it.name` $no_pub_in_main_warning',
|
||||
it.pos)
|
||||
}
|
||||
} else if stmt is ast.SumTypeDecl {
|
||||
sum_decl := stmt as ast.SumTypeDecl
|
||||
if sum_decl.is_pub {
|
||||
c.warn('sum type `$sum_decl.name` $no_pub_in_main_warning', sum_decl.pos)
|
||||
if it.is_pub {
|
||||
c.warn('sum type `$it.name` $no_pub_in_main_warning', it.pos)
|
||||
}
|
||||
} else if stmt is ast.FnTypeDecl {
|
||||
fn_decl := stmt as ast.FnTypeDecl
|
||||
if fn_decl.is_pub {
|
||||
c.warn('type alias `$fn_decl.name` $no_pub_in_main_warning', fn_decl.pos)
|
||||
if it.is_pub {
|
||||
c.warn('type alias `$it.name` $no_pub_in_main_warning', it.pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -995,7 +991,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
|
|||
if fn_name == 'json.encode' {
|
||||
} else if fn_name == 'json.decode' {
|
||||
expr := call_expr.args[0].expr
|
||||
if !(expr is ast.Type) {
|
||||
if expr !is ast.Type {
|
||||
typ := typeof(expr)
|
||||
c.error('json.decode: first argument needs to be a type, got `$typ`', call_expr.pos)
|
||||
return table.void_type
|
||||
|
@ -1270,26 +1266,25 @@ fn (mut c Checker) type_implements(typ, inter_typ table.Type, pos token.Position
|
|||
// return the actual type of the expression, once the optional is handled
|
||||
pub fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type table.Type) table.Type {
|
||||
if expr is ast.CallExpr {
|
||||
call_expr := expr as ast.CallExpr
|
||||
if call_expr.return_type.has_flag(.optional) {
|
||||
if call_expr.or_block.kind == .absent {
|
||||
if it.return_type.has_flag(.optional) {
|
||||
if it.or_block.kind == .absent {
|
||||
if ret_type != table.void_type {
|
||||
c.error('${call_expr.name}() returns an option, but you missed to add an `or {}` block to it',
|
||||
call_expr.pos)
|
||||
c.error('${it.name}() returns an option, but you missed to add an `or {}` block to it',
|
||||
it.pos)
|
||||
}
|
||||
} else {
|
||||
c.check_or_expr(call_expr.or_block, ret_type)
|
||||
c.check_or_expr(it.or_block, ret_type)
|
||||
}
|
||||
// remove optional flag
|
||||
// return ret_type.clear_flag(.optional)
|
||||
// TODO: currently unwrapped in assign, would need to refactor assign to unwrap here
|
||||
return ret_type
|
||||
} else if call_expr.or_block.kind == .block {
|
||||
c.error('unexpected `or` block, the function `$call_expr.name` does not return an optional',
|
||||
call_expr.pos)
|
||||
} else if call_expr.or_block.kind == .propagate {
|
||||
c.error('unexpected `?`, the function `$call_expr.name`, does not return an optional',
|
||||
call_expr.pos)
|
||||
} else if it.or_block.kind == .block {
|
||||
c.error('unexpected `or` block, the function `$it.name` does not return an optional',
|
||||
it.pos)
|
||||
} else if it.or_block.kind == .propagate {
|
||||
c.error('unexpected `?`, the function `$it.name`, does not return an optional',
|
||||
it.pos)
|
||||
}
|
||||
}
|
||||
return ret_type
|
||||
|
@ -1527,8 +1522,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
|
|||
}
|
||||
if assign_stmt.left.len != right_len {
|
||||
if right_first is ast.CallExpr {
|
||||
call_expr := assign_stmt.right[0] as ast.CallExpr
|
||||
c.error('assignment mismatch: $assign_stmt.left.len variable(s) but `${call_expr.name}()` returns $right_len value(s)',
|
||||
c.error('assignment mismatch: $assign_stmt.left.len variable(s) but `${it.name}()` returns $right_len value(s)',
|
||||
assign_stmt.pos)
|
||||
} else {
|
||||
c.error('assignment mismatch: $assign_stmt.left.len variable(s) $right_len value(s)',
|
||||
|
@ -1982,7 +1976,7 @@ fn (mut c Checker) stmt(node ast.Stmt) {
|
|||
c.check_valid_snake_case(node.name, 'global name', node.pos)
|
||||
}
|
||||
ast.GoStmt {
|
||||
if !(node.call_expr is ast.CallExpr) {
|
||||
if node.call_expr !is ast.CallExpr {
|
||||
c.error('expression in `go` must be a function call', node.call_expr.position())
|
||||
}
|
||||
c.expr(node.call_expr)
|
||||
|
@ -2803,7 +2797,7 @@ pub fn (mut c Checker) enum_val(mut node ast.EnumVal) table.Type {
|
|||
c.error('expected type is not an enum', node.pos)
|
||||
return table.void_type
|
||||
}
|
||||
if !(typ_sym.info is table.Enum) {
|
||||
if typ_sym.info !is table.Enum {
|
||||
c.error('not an enum', node.pos)
|
||||
return table.void_type
|
||||
}
|
||||
|
|
|
@ -45,8 +45,7 @@ pub fn merge_comments(stmts []ast.Stmt) string {
|
|||
mut res := []string{}
|
||||
for s in stmts {
|
||||
if s is ast.Comment {
|
||||
c := s as ast.Comment
|
||||
res << c.text.trim_left('|')
|
||||
res << it.text.trim_left('|')
|
||||
}
|
||||
}
|
||||
return res.join('\n')
|
||||
|
@ -375,12 +374,11 @@ fn (mut d Doc) generate() ?Doc {
|
|||
continue
|
||||
}
|
||||
if stmt is ast.FnDecl {
|
||||
fnd := stmt as ast.FnDecl
|
||||
if fnd.is_deprecated {
|
||||
if it.is_deprecated {
|
||||
continue
|
||||
}
|
||||
if fnd.receiver.typ != 0 {
|
||||
node.attrs['parent'] = d.fmt.type_to_str(fnd.receiver.typ).trim_left('&')
|
||||
if it.receiver.typ != 0 {
|
||||
node.attrs['parent'] = d.fmt.type_to_str(it.receiver.typ).trim_left('&')
|
||||
p_idx := d.contents.index_by_name(node.attrs['parent'])
|
||||
if p_idx == -1 && node.attrs['parent'] != 'void' {
|
||||
d.contents << DocNode{
|
||||
|
@ -405,7 +403,7 @@ fn (mut d Doc) generate() ?Doc {
|
|||
ast.InterfaceDecl { node.attrs['category'] = 'Interfaces' }
|
||||
ast.StructDecl { node.attrs['category'] = 'Structs' }
|
||||
ast.TypeDecl { node.attrs['category'] = 'Typedefs' }
|
||||
ast.FnDecl {
|
||||
ast.FnDecl {
|
||||
node.attrs['category'] = if node.attrs['parent'] in ['void', ''] || !node.attrs.exists('parent') {
|
||||
'Functions'
|
||||
} else {
|
||||
|
|
|
@ -246,8 +246,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
|
|||
ast.AssignStmt {
|
||||
for i, left in node.left {
|
||||
if left is ast.Ident {
|
||||
ident := left as ast.Ident
|
||||
var_info := ident.var_info()
|
||||
var_info := it.var_info()
|
||||
if var_info.is_mut {
|
||||
f.write('mut ')
|
||||
}
|
||||
|
@ -661,9 +660,8 @@ pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) {
|
|||
mut is_pe_amp_ce := false
|
||||
mut ce := ast.CastExpr{}
|
||||
if fexpr is ast.PrefixExpr {
|
||||
pe := fexpr as ast.PrefixExpr
|
||||
if pe.right is ast.CastExpr && pe.op == .amp {
|
||||
ce = pe.right as ast.CastExpr
|
||||
if it.right is ast.CastExpr && it.op == .amp {
|
||||
ce = it.right as ast.CastExpr
|
||||
ce.typname = f.table.get_type_symbol(ce.typ).name
|
||||
is_pe_amp_ce = true
|
||||
f.expr(ce)
|
||||
|
@ -1323,8 +1321,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
|
|||
stmt := branch.stmts[0]
|
||||
if stmt is ast.ExprStmt {
|
||||
// If expressions inside match branches can't be one a single line
|
||||
expr_stmt := stmt as ast.ExprStmt
|
||||
if !expr_is_single_line(expr_stmt.expr) {
|
||||
if !expr_is_single_line(it.expr) {
|
||||
single_line = false
|
||||
break
|
||||
}
|
||||
|
|
|
@ -1205,7 +1205,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
|||
mut blank_assign := false
|
||||
mut ident := ast.Ident{}
|
||||
if left is ast.Ident {
|
||||
ident = left as ast.Ident
|
||||
ident = it
|
||||
// id_info := ident.var_info()
|
||||
// var_type = id_info.typ
|
||||
blank_assign = ident.kind == .blank_ident
|
||||
|
@ -1382,10 +1382,9 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) {
|
|||
mut has_var := false
|
||||
for lx in left {
|
||||
if lx is ast.Ident {
|
||||
ident := lx as ast.Ident
|
||||
if val.name == ident.name {
|
||||
if val.name == it.name {
|
||||
g.write('_var_')
|
||||
g.write(ident.pos.pos.str())
|
||||
g.write(it.pos.pos.str())
|
||||
has_var = true
|
||||
break
|
||||
}
|
||||
|
|
|
@ -11,11 +11,10 @@ fn (g &Gen) comptime_call(node ast.ComptimeCall) {
|
|||
if node.is_vweb {
|
||||
for stmt in node.vweb_tmpl.stmts {
|
||||
if stmt is ast.FnDecl {
|
||||
fn_decl := stmt as ast.FnDecl
|
||||
// insert stmts from vweb_tmpl fn
|
||||
if fn_decl.name.starts_with('main.vweb_tmpl') {
|
||||
if it.name.starts_with('main.vweb_tmpl') {
|
||||
g.inside_vweb_tmpl = true
|
||||
g.stmts(fn_decl.stmts)
|
||||
g.stmts(it.stmts)
|
||||
g.inside_vweb_tmpl = false
|
||||
break
|
||||
}
|
||||
|
|
|
@ -686,9 +686,8 @@ fn (mut g JsGen) gen_assign_stmt(stmt ast.AssignStmt) {
|
|||
val := stmt.right[i]
|
||||
mut is_mut := false
|
||||
if left is ast.Ident {
|
||||
ident := left as ast.Ident
|
||||
is_mut = ident.is_mut
|
||||
if ident.kind == .blank_ident || ident.name in ['', '_'] {
|
||||
is_mut = it.is_mut
|
||||
if it.kind == .blank_ident || it.name in ['', '_'] {
|
||||
tmp_var := g.new_tmp_var()
|
||||
// TODO: Can the tmp_var declaration be omitted?
|
||||
g.write('const $tmp_var = ')
|
||||
|
@ -1254,12 +1253,12 @@ fn (mut g JsGen) gen_if_expr(node ast.IfExpr) {
|
|||
}
|
||||
}
|
||||
|
||||
fn (mut g JsGen) gen_index_expr(it ast.IndexExpr) {
|
||||
left_typ := g.table.get_type_symbol(it.left_type)
|
||||
fn (mut g JsGen) gen_index_expr(expr ast.IndexExpr) {
|
||||
left_typ := g.table.get_type_symbol(expr.left_type)
|
||||
// TODO: Handle splice setting if it's implemented
|
||||
if it.index is ast.RangeExpr {
|
||||
range := it.index as ast.RangeExpr
|
||||
g.expr(it.left)
|
||||
if expr.index is ast.RangeExpr {
|
||||
range := expr.index as ast.RangeExpr
|
||||
g.expr(expr.left)
|
||||
g.write('.slice(')
|
||||
if range.has_low {
|
||||
g.expr(range.low)
|
||||
|
@ -1270,35 +1269,35 @@ fn (mut g JsGen) gen_index_expr(it ast.IndexExpr) {
|
|||
if range.has_high {
|
||||
g.expr(range.high)
|
||||
} else {
|
||||
g.expr(it.left)
|
||||
g.expr(expr.left)
|
||||
g.write('.length')
|
||||
}
|
||||
g.write(')')
|
||||
} else if left_typ.kind == .map {
|
||||
g.expr(it.left)
|
||||
if it.is_setter {
|
||||
g.expr(expr.left)
|
||||
if expr.is_setter {
|
||||
g.inside_map_set = true
|
||||
g.write('.set(')
|
||||
} else {
|
||||
g.write('.get(')
|
||||
}
|
||||
g.expr(it.index)
|
||||
if !it.is_setter { g.write(')') }
|
||||
g.expr(expr.index)
|
||||
if !expr.is_setter { g.write(')') }
|
||||
} else if left_typ.kind == .string {
|
||||
if it.is_setter {
|
||||
if expr.is_setter {
|
||||
// TODO: What's the best way to do this?
|
||||
// 'string'[3] = `o`
|
||||
} else {
|
||||
g.expr(it.left)
|
||||
g.expr(expr.left)
|
||||
g.write('.charCodeAt(')
|
||||
g.expr(it.index)
|
||||
g.expr(expr.index)
|
||||
g.write(')')
|
||||
}
|
||||
} else {
|
||||
// TODO Does this cover all cases?
|
||||
g.expr(it.left)
|
||||
g.expr(expr.left)
|
||||
g.write('[')
|
||||
g.expr(it.index)
|
||||
g.expr(expr.index)
|
||||
g.write(']')
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,7 +75,7 @@ $enc_fn_dec {
|
|||
// enc += g.encode_array(t)
|
||||
} else {
|
||||
// Structs. Range through fields
|
||||
if !(sym.info is table.Struct) {
|
||||
if sym.info !is table.Struct {
|
||||
verror('json: $sym.name is not struct')
|
||||
}
|
||||
info := sym.info as table.Struct
|
||||
|
@ -111,7 +111,7 @@ $enc_fn_dec {
|
|||
mut enc_name := js_enc_name(field_type)
|
||||
if g.table.get_type_symbol(field.typ).kind == .enum_ {
|
||||
enc.writeln('\tcJSON_AddItemToObject(o, "$name", json__encode_u64(val.${c_name(field.name)}));')
|
||||
|
||||
|
||||
} else {
|
||||
enc.writeln('\tcJSON_AddItemToObject(o, "$name", ${enc_name}(val.${c_name(field.name)}));')
|
||||
}
|
||||
|
|
|
@ -840,7 +840,7 @@ fn (mut g Gen) fn_decl(node ast.FnDecl) {
|
|||
}
|
||||
|
||||
fn (mut g Gen) postfix_expr(node ast.PostfixExpr) {
|
||||
if !(node.expr is ast.Ident) {
|
||||
if node.expr !is ast.Ident {
|
||||
return
|
||||
}
|
||||
ident := node.expr as ast.Ident
|
||||
|
|
|
@ -15,8 +15,7 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) {
|
|||
ast.Ident {
|
||||
for expr in exprs {
|
||||
if expr is ast.Ident {
|
||||
ident := expr as ast.Ident
|
||||
if ident.name == val.name {
|
||||
if it.name == val.name {
|
||||
p.error_with_pos('undefined variable: `$val.name`', val.pos)
|
||||
}
|
||||
}
|
||||
|
@ -50,8 +49,7 @@ fn (mut p Parser) check_cross_variables(exprs []ast.Expr, val ast.Expr) bool {
|
|||
ast.Ident {
|
||||
for expr in exprs {
|
||||
if expr is ast.Ident {
|
||||
ident := expr as ast.Ident
|
||||
if ident.name == val_.name {
|
||||
if it.name == val_.name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,7 +134,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall {
|
|||
tmpl_scope := file.scope.innermost(fn_decl.body_pos.pos)
|
||||
for _, obj in p.scope.objects {
|
||||
if obj is ast.Var {
|
||||
mut v := obj as ast.Var
|
||||
mut v := it
|
||||
v.pos = fn_decl.body_pos
|
||||
tmpl_scope.register(v.name, *v)
|
||||
// set the controller action var to used
|
||||
|
|
|
@ -563,8 +563,7 @@ fn (mut p Parser) fn_redefinition_error(name string) {
|
|||
fn have_fn_main(stmts []ast.Stmt) bool {
|
||||
for stmt in stmts {
|
||||
if stmt is ast.FnDecl {
|
||||
f := stmt as ast.FnDecl
|
||||
if f.name == 'main.main' && f.mod == 'main' {
|
||||
if it.name == 'main.main' && it.mod == 'main' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue