checker: speed up check_expected_call_arg, by only calling Table.type_to_str on errors

master
Delyan Angelov 2022-06-02 18:24:44 +03:00
parent 41414b5d5f
commit c892b3203e
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 15 additions and 6 deletions

View File

@ -232,10 +232,6 @@ pub fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type,
return return
} }
} }
got_typ_sym := c.table.sym(got)
got_typ_str := c.table.type_to_str(got.clear_flag(.variadic))
expected_typ_sym := c.table.sym(expected_)
expected_typ_str := c.table.type_to_str(expected.clear_flag(.variadic))
if c.check_types(got, expected) { if c.check_types(got, expected) {
if language != .v || expected.is_ptr() == got.is_ptr() || arg.is_mut if language != .v || expected.is_ptr() == got.is_ptr() || arg.is_mut
@ -244,6 +240,9 @@ pub fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type,
return return
} }
} else { } else {
got_typ_sym := c.table.sym(got)
expected_typ_sym := c.table.sym(expected_)
// Check on Generics types, there are some case where we have the following case // Check on Generics types, there are some case where we have the following case
// `&Type<int> == &Type<>`. This is a common case we are implementing a function // `&Type<int> == &Type<>`. This is a common case we are implementing a function
// with generic parameters like `compare(bst Bst<T> node) {}` // with generic parameters like `compare(bst Bst<T> node) {}`
@ -251,6 +250,7 @@ pub fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type,
// Check if we are making a comparison between two different types of // Check if we are making a comparison between two different types of
// the same type like `Type<int> and &Type<>` // the same type like `Type<int> and &Type<>`
if (got.is_ptr() != expected.is_ptr()) || !c.check_same_module(got, expected) { if (got.is_ptr() != expected.is_ptr()) || !c.check_same_module(got, expected) {
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
return error('cannot use `$got_typ_str` as `$expected_typ_str`') return error('cannot use `$got_typ_str` as `$expected_typ_str`')
} }
return return
@ -258,14 +258,22 @@ pub fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type,
if got == ast.void_type { if got == ast.void_type {
return error('`$arg.expr` (no value) used as value') return error('`$arg.expr` (no value) used as value')
} }
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
return error('cannot use `$got_typ_str` as `$expected_typ_str`') return error('cannot use `$got_typ_str` as `$expected_typ_str`')
} }
if got != ast.void_type { if got != ast.void_type {
got_typ_str, expected_typ_str := c.get_string_names_of(got, expected)
return error('cannot use `$got_typ_str` as `$expected_typ_str`') return error('cannot use `$got_typ_str` as `$expected_typ_str`')
} }
} }
fn (c Checker) get_string_names_of(got ast.Type, expected ast.Type) (string, string) {
got_typ_str := c.table.type_to_str(got.clear_flag(.variadic))
expected_typ_str := c.table.type_to_str(expected.clear_flag(.variadic))
return got_typ_str, expected_typ_str
}
// helper method to check if the type is of the same module. // helper method to check if the type is of the same module.
// FIXME(vincenzopalazzo) This is a work around to the issue // FIXME(vincenzopalazzo) This is a work around to the issue
// explained in the https://github.com/vlang/v/pull/13718#issuecomment-1074517800 // explained in the https://github.com/vlang/v/pull/13718#issuecomment-1074517800

View File

@ -804,7 +804,6 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
} }
} }
} }
styp := c.table.type_to_str(utyp)
if utyp.idx() == interface_type.idx() { if utyp.idx() == interface_type.idx() {
// same type -> already casted to the interface // same type -> already casted to the interface
return true return true
@ -813,6 +812,7 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
// `none` "implements" the Error interface // `none` "implements" the Error interface
return true return true
} }
styp := c.table.type_to_str(utyp)
if typ_sym.kind == .interface_ && inter_sym.kind == .interface_ && !styp.starts_with('JS.') if typ_sym.kind == .interface_ && inter_sym.kind == .interface_ && !styp.starts_with('JS.')
&& !inter_sym.name.starts_with('JS.') { && !inter_sym.name.starts_with('JS.') {
c.error('cannot implement interface `$inter_sym.name` with a different interface `$styp`', c.error('cannot implement interface `$inter_sym.name` with a different interface `$styp`',
@ -986,7 +986,6 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret
if type_fits || is_noreturn { if type_fits || is_noreturn {
return return
} }
expected_type_name := c.table.type_to_str(ret_type.clear_flag(.optional))
if stmt.typ == ast.void_type { if stmt.typ == ast.void_type {
if stmt.expr is ast.IfExpr { if stmt.expr is ast.IfExpr {
for branch in stmt.expr.branches { for branch in stmt.expr.branches {
@ -999,10 +998,12 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret
} }
return return
} }
expected_type_name := c.table.type_to_str(ret_type.clear_flag(.optional))
c.error('`or` block must provide a default value of type `$expected_type_name`, or return/continue/break or call a [noreturn] function like panic(err) or exit(1)', c.error('`or` block must provide a default value of type `$expected_type_name`, or return/continue/break or call a [noreturn] function like panic(err) or exit(1)',
stmt.expr.pos()) stmt.expr.pos())
} else { } else {
type_name := c.table.type_to_str(last_stmt_typ) type_name := c.table.type_to_str(last_stmt_typ)
expected_type_name := c.table.type_to_str(ret_type.clear_flag(.optional))
c.error('wrong return type `$type_name` in the `or {}` block, expected `$expected_type_name`', c.error('wrong return type `$type_name` in the `or {}` block, expected `$expected_type_name`',
stmt.expr.pos()) stmt.expr.pos())
} }