all: if smartcast part 2 (#5754)

pull/5756/head
Daniel Däschle 2020-07-08 15:46:58 +02:00 committed by GitHub
parent 7ad03e9d6a
commit 5ea17ad2d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 67 additions and 88 deletions

View File

@ -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 f in b.parsed_files {
for s in f.stmts { for s in f.stmts {
if s is ast.FnDecl { if s is ast.FnDecl {
fnd := s as ast.FnDecl if it.is_pub {
if fnd.is_pub { fn_signature := it.stringify(b.table)
fn_signature := fnd.stringify(b.table) fn_mod := it.modname()
fn_mod := fnd.modname()
if fn_mod == mod_name { if fn_mod == mod_name {
fline := '${fn_mod}: $fn_signature' fline := '${fn_mod}: $fn_signature'
res << fline res << fline

View File

@ -33,8 +33,7 @@ fn wait() {
fn (l Lander) land(w World) { fn (l Lander) land(w World) {
if w is Mars { if w is Mars {
m := w as Mars for it.dust_storm() {
for m.dust_storm() {
wait() wait()
} }
} }

View File

@ -279,8 +279,7 @@ pub fn (node Stmt) str() string {
mut out := '' mut out := ''
for i, left in it.left { for i, left in it.left {
if left is Ident { if left is Ident {
ident := left as Ident var_info := it.var_info()
var_info := ident.var_info()
if var_info.is_mut { if var_info.is_mut {
out += 'mut ' out += 'mut '
} }

View File

@ -288,9 +288,8 @@ fn (b &Builder) print_warnings_and_errors() {
for file in b.parsed_files { for file in b.parsed_files {
for stmt in file.stmts { for stmt in file.stmts {
if stmt is ast.FnDecl { if stmt is ast.FnDecl {
f := stmt as ast.FnDecl if it.name == fn_name {
if f.name == fn_name { fline := it.pos.line_nr
fline := f.pos.line_nr
println('$file.path:$fline:') println('$file.path:$fline:')
} }
} }

View File

@ -206,22 +206,18 @@ fn (mut c Checker) check_file_in_main(file ast.File) bool {
} }
} }
ast.TypeDecl { ast.TypeDecl {
// type_decl := stmt as ast.TypeDecl
if stmt is ast.AliasTypeDecl { if stmt is ast.AliasTypeDecl {
alias_decl := stmt as ast.AliasTypeDecl if it.is_pub {
if alias_decl.is_pub { c.warn('type alias `$it.name` $no_pub_in_main_warning',
c.warn('type alias `$alias_decl.name` $no_pub_in_main_warning', it.pos)
alias_decl.pos)
} }
} else if stmt is ast.SumTypeDecl { } else if stmt is ast.SumTypeDecl {
sum_decl := stmt as ast.SumTypeDecl if it.is_pub {
if sum_decl.is_pub { c.warn('sum type `$it.name` $no_pub_in_main_warning', it.pos)
c.warn('sum type `$sum_decl.name` $no_pub_in_main_warning', sum_decl.pos)
} }
} else if stmt is ast.FnTypeDecl { } else if stmt is ast.FnTypeDecl {
fn_decl := stmt as ast.FnTypeDecl if it.is_pub {
if fn_decl.is_pub { c.warn('type alias `$it.name` $no_pub_in_main_warning', it.pos)
c.warn('type alias `$fn_decl.name` $no_pub_in_main_warning', fn_decl.pos)
} }
} }
} }
@ -995,7 +991,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
if fn_name == 'json.encode' { if fn_name == 'json.encode' {
} else if fn_name == 'json.decode' { } else if fn_name == 'json.decode' {
expr := call_expr.args[0].expr expr := call_expr.args[0].expr
if !(expr is ast.Type) { if expr !is ast.Type {
typ := typeof(expr) typ := typeof(expr)
c.error('json.decode: first argument needs to be a type, got `$typ`', call_expr.pos) c.error('json.decode: first argument needs to be a type, got `$typ`', call_expr.pos)
return table.void_type 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 // 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 { pub fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type table.Type) table.Type {
if expr is ast.CallExpr { if expr is ast.CallExpr {
call_expr := expr as ast.CallExpr if it.return_type.has_flag(.optional) {
if call_expr.return_type.has_flag(.optional) { if it.or_block.kind == .absent {
if call_expr.or_block.kind == .absent {
if ret_type != table.void_type { if ret_type != table.void_type {
c.error('${call_expr.name}() returns an option, but you missed to add an `or {}` block to it', c.error('${it.name}() returns an option, but you missed to add an `or {}` block to it',
call_expr.pos) it.pos)
} }
} else { } else {
c.check_or_expr(call_expr.or_block, ret_type) c.check_or_expr(it.or_block, ret_type)
} }
// remove optional flag // remove optional flag
// return ret_type.clear_flag(.optional) // return ret_type.clear_flag(.optional)
// TODO: currently unwrapped in assign, would need to refactor assign to unwrap here // TODO: currently unwrapped in assign, would need to refactor assign to unwrap here
return ret_type return ret_type
} else if call_expr.or_block.kind == .block { } else if it.or_block.kind == .block {
c.error('unexpected `or` block, the function `$call_expr.name` does not return an optional', c.error('unexpected `or` block, the function `$it.name` does not return an optional',
call_expr.pos) it.pos)
} else if call_expr.or_block.kind == .propagate { } else if it.or_block.kind == .propagate {
c.error('unexpected `?`, the function `$call_expr.name`, does not return an optional', c.error('unexpected `?`, the function `$it.name`, does not return an optional',
call_expr.pos) it.pos)
} }
} }
return ret_type 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 assign_stmt.left.len != right_len {
if right_first is ast.CallExpr { 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 `${it.name}()` returns $right_len value(s)',
c.error('assignment mismatch: $assign_stmt.left.len variable(s) but `${call_expr.name}()` returns $right_len value(s)',
assign_stmt.pos) assign_stmt.pos)
} else { } else {
c.error('assignment mismatch: $assign_stmt.left.len variable(s) $right_len value(s)', 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) c.check_valid_snake_case(node.name, 'global name', node.pos)
} }
ast.GoStmt { 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.error('expression in `go` must be a function call', node.call_expr.position())
} }
c.expr(node.call_expr) 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) c.error('expected type is not an enum', node.pos)
return table.void_type 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) c.error('not an enum', node.pos)
return table.void_type return table.void_type
} }

View File

@ -45,8 +45,7 @@ pub fn merge_comments(stmts []ast.Stmt) string {
mut res := []string{} mut res := []string{}
for s in stmts { for s in stmts {
if s is ast.Comment { if s is ast.Comment {
c := s as ast.Comment res << it.text.trim_left('|')
res << c.text.trim_left('|')
} }
} }
return res.join('\n') return res.join('\n')
@ -375,12 +374,11 @@ fn (mut d Doc) generate() ?Doc {
continue continue
} }
if stmt is ast.FnDecl { if stmt is ast.FnDecl {
fnd := stmt as ast.FnDecl if it.is_deprecated {
if fnd.is_deprecated {
continue continue
} }
if fnd.receiver.typ != 0 { if it.receiver.typ != 0 {
node.attrs['parent'] = d.fmt.type_to_str(fnd.receiver.typ).trim_left('&') node.attrs['parent'] = d.fmt.type_to_str(it.receiver.typ).trim_left('&')
p_idx := d.contents.index_by_name(node.attrs['parent']) p_idx := d.contents.index_by_name(node.attrs['parent'])
if p_idx == -1 && node.attrs['parent'] != 'void' { if p_idx == -1 && node.attrs['parent'] != 'void' {
d.contents << DocNode{ d.contents << DocNode{
@ -405,7 +403,7 @@ fn (mut d Doc) generate() ?Doc {
ast.InterfaceDecl { node.attrs['category'] = 'Interfaces' } ast.InterfaceDecl { node.attrs['category'] = 'Interfaces' }
ast.StructDecl { node.attrs['category'] = 'Structs' } ast.StructDecl { node.attrs['category'] = 'Structs' }
ast.TypeDecl { node.attrs['category'] = 'Typedefs' } ast.TypeDecl { node.attrs['category'] = 'Typedefs' }
ast.FnDecl { ast.FnDecl {
node.attrs['category'] = if node.attrs['parent'] in ['void', ''] || !node.attrs.exists('parent') { node.attrs['category'] = if node.attrs['parent'] in ['void', ''] || !node.attrs.exists('parent') {
'Functions' 'Functions'
} else { } else {

View File

@ -246,8 +246,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) {
ast.AssignStmt { ast.AssignStmt {
for i, left in node.left { for i, left in node.left {
if left is ast.Ident { if left is ast.Ident {
ident := left as ast.Ident var_info := it.var_info()
var_info := ident.var_info()
if var_info.is_mut { if var_info.is_mut {
f.write('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 is_pe_amp_ce := false
mut ce := ast.CastExpr{} mut ce := ast.CastExpr{}
if fexpr is ast.PrefixExpr { if fexpr is ast.PrefixExpr {
pe := fexpr as ast.PrefixExpr if it.right is ast.CastExpr && it.op == .amp {
if pe.right is ast.CastExpr && pe.op == .amp { ce = it.right as ast.CastExpr
ce = pe.right as ast.CastExpr
ce.typname = f.table.get_type_symbol(ce.typ).name ce.typname = f.table.get_type_symbol(ce.typ).name
is_pe_amp_ce = true is_pe_amp_ce = true
f.expr(ce) f.expr(ce)
@ -1323,8 +1321,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
stmt := branch.stmts[0] stmt := branch.stmts[0]
if stmt is ast.ExprStmt { if stmt is ast.ExprStmt {
// If expressions inside match branches can't be one a single line // If expressions inside match branches can't be one a single line
expr_stmt := stmt as ast.ExprStmt if !expr_is_single_line(it.expr) {
if !expr_is_single_line(expr_stmt.expr) {
single_line = false single_line = false
break break
} }

View File

@ -1205,7 +1205,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
mut blank_assign := false mut blank_assign := false
mut ident := ast.Ident{} mut ident := ast.Ident{}
if left is ast.Ident { if left is ast.Ident {
ident = left as ast.Ident ident = it
// id_info := ident.var_info() // id_info := ident.var_info()
// var_type = id_info.typ // var_type = id_info.typ
blank_assign = ident.kind == .blank_ident 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 mut has_var := false
for lx in left { for lx in left {
if lx is ast.Ident { if lx is ast.Ident {
ident := lx as ast.Ident if val.name == it.name {
if val.name == ident.name {
g.write('_var_') g.write('_var_')
g.write(ident.pos.pos.str()) g.write(it.pos.pos.str())
has_var = true has_var = true
break break
} }

View File

@ -11,11 +11,10 @@ fn (g &Gen) comptime_call(node ast.ComptimeCall) {
if node.is_vweb { if node.is_vweb {
for stmt in node.vweb_tmpl.stmts { for stmt in node.vweb_tmpl.stmts {
if stmt is ast.FnDecl { if stmt is ast.FnDecl {
fn_decl := stmt as ast.FnDecl
// insert stmts from vweb_tmpl fn // 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.inside_vweb_tmpl = true
g.stmts(fn_decl.stmts) g.stmts(it.stmts)
g.inside_vweb_tmpl = false g.inside_vweb_tmpl = false
break break
} }

View File

@ -686,9 +686,8 @@ fn (mut g JsGen) gen_assign_stmt(stmt ast.AssignStmt) {
val := stmt.right[i] val := stmt.right[i]
mut is_mut := false mut is_mut := false
if left is ast.Ident { if left is ast.Ident {
ident := left as ast.Ident is_mut = it.is_mut
is_mut = ident.is_mut if it.kind == .blank_ident || it.name in ['', '_'] {
if ident.kind == .blank_ident || ident.name in ['', '_'] {
tmp_var := g.new_tmp_var() tmp_var := g.new_tmp_var()
// TODO: Can the tmp_var declaration be omitted? // TODO: Can the tmp_var declaration be omitted?
g.write('const $tmp_var = ') 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) { fn (mut g JsGen) gen_index_expr(expr ast.IndexExpr) {
left_typ := g.table.get_type_symbol(it.left_type) left_typ := g.table.get_type_symbol(expr.left_type)
// TODO: Handle splice setting if it's implemented // TODO: Handle splice setting if it's implemented
if it.index is ast.RangeExpr { if expr.index is ast.RangeExpr {
range := it.index as ast.RangeExpr range := expr.index as ast.RangeExpr
g.expr(it.left) g.expr(expr.left)
g.write('.slice(') g.write('.slice(')
if range.has_low { if range.has_low {
g.expr(range.low) g.expr(range.low)
@ -1270,35 +1269,35 @@ fn (mut g JsGen) gen_index_expr(it ast.IndexExpr) {
if range.has_high { if range.has_high {
g.expr(range.high) g.expr(range.high)
} else { } else {
g.expr(it.left) g.expr(expr.left)
g.write('.length') g.write('.length')
} }
g.write(')') g.write(')')
} else if left_typ.kind == .map { } else if left_typ.kind == .map {
g.expr(it.left) g.expr(expr.left)
if it.is_setter { if expr.is_setter {
g.inside_map_set = true g.inside_map_set = true
g.write('.set(') g.write('.set(')
} else { } else {
g.write('.get(') g.write('.get(')
} }
g.expr(it.index) g.expr(expr.index)
if !it.is_setter { g.write(')') } if !expr.is_setter { g.write(')') }
} else if left_typ.kind == .string { } else if left_typ.kind == .string {
if it.is_setter { if expr.is_setter {
// TODO: What's the best way to do this? // TODO: What's the best way to do this?
// 'string'[3] = `o` // 'string'[3] = `o`
} else { } else {
g.expr(it.left) g.expr(expr.left)
g.write('.charCodeAt(') g.write('.charCodeAt(')
g.expr(it.index) g.expr(expr.index)
g.write(')') g.write(')')
} }
} else { } else {
// TODO Does this cover all cases? // TODO Does this cover all cases?
g.expr(it.left) g.expr(expr.left)
g.write('[') g.write('[')
g.expr(it.index) g.expr(expr.index)
g.write(']') g.write(']')
} }
} }

View File

@ -75,7 +75,7 @@ $enc_fn_dec {
// enc += g.encode_array(t) // enc += g.encode_array(t)
} else { } else {
// Structs. Range through fields // Structs. Range through fields
if !(sym.info is table.Struct) { if sym.info !is table.Struct {
verror('json: $sym.name is not struct') verror('json: $sym.name is not struct')
} }
info := sym.info as table.Struct info := sym.info as table.Struct
@ -111,7 +111,7 @@ $enc_fn_dec {
mut enc_name := js_enc_name(field_type) mut enc_name := js_enc_name(field_type)
if g.table.get_type_symbol(field.typ).kind == .enum_ { if g.table.get_type_symbol(field.typ).kind == .enum_ {
enc.writeln('\tcJSON_AddItemToObject(o, "$name", json__encode_u64(val.${c_name(field.name)}));') enc.writeln('\tcJSON_AddItemToObject(o, "$name", json__encode_u64(val.${c_name(field.name)}));')
} else { } else {
enc.writeln('\tcJSON_AddItemToObject(o, "$name", ${enc_name}(val.${c_name(field.name)}));') enc.writeln('\tcJSON_AddItemToObject(o, "$name", ${enc_name}(val.${c_name(field.name)}));')
} }

View File

@ -840,7 +840,7 @@ fn (mut g Gen) fn_decl(node ast.FnDecl) {
} }
fn (mut g Gen) postfix_expr(node ast.PostfixExpr) { fn (mut g Gen) postfix_expr(node ast.PostfixExpr) {
if !(node.expr is ast.Ident) { if node.expr !is ast.Ident {
return return
} }
ident := node.expr as ast.Ident ident := node.expr as ast.Ident

View File

@ -15,8 +15,7 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) {
ast.Ident { ast.Ident {
for expr in exprs { for expr in exprs {
if expr is ast.Ident { if expr is ast.Ident {
ident := expr as ast.Ident if it.name == val.name {
if ident.name == val.name {
p.error_with_pos('undefined variable: `$val.name`', val.pos) 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 { ast.Ident {
for expr in exprs { for expr in exprs {
if expr is ast.Ident { if expr is ast.Ident {
ident := expr as ast.Ident if it.name == val_.name {
if ident.name == val_.name {
return true return true
} }
} }

View File

@ -134,7 +134,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall {
tmpl_scope := file.scope.innermost(fn_decl.body_pos.pos) tmpl_scope := file.scope.innermost(fn_decl.body_pos.pos)
for _, obj in p.scope.objects { for _, obj in p.scope.objects {
if obj is ast.Var { if obj is ast.Var {
mut v := obj as ast.Var mut v := it
v.pos = fn_decl.body_pos v.pos = fn_decl.body_pos
tmpl_scope.register(v.name, *v) tmpl_scope.register(v.name, *v)
// set the controller action var to used // set the controller action var to used

View File

@ -563,8 +563,7 @@ fn (mut p Parser) fn_redefinition_error(name string) {
fn have_fn_main(stmts []ast.Stmt) bool { fn have_fn_main(stmts []ast.Stmt) bool {
for stmt in stmts { for stmt in stmts {
if stmt is ast.FnDecl { if stmt is ast.FnDecl {
f := stmt as ast.FnDecl if it.name == 'main.main' && it.mod == 'main' {
if f.name == 'main.main' && f.mod == 'main' {
return true return true
} }
} }