1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check generic fn call argument type mismatch (#17680)

This commit is contained in:
yuyi 2023-03-17 03:19:03 +08:00 committed by GitHub
parent b345d77805
commit d349c1d86d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 4 deletions

View File

@ -1792,7 +1792,8 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
c.error('literal argument cannot be passed as reference parameter `${c.table.type_to_str(param.typ)}`',
arg.pos)
}
c.check_expected_call_arg(got_arg_typ, exp_arg_typ, node.language, arg) or {
c.check_expected_call_arg(c.unwrap_generic(got_arg_typ), exp_arg_typ, node.language,
arg) or {
// str method, allow type with str method if fn arg is string
// Passing an int or a string array produces a c error here
// Deleting this condition results in propper V error messages

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/generic_fn_call_arg_mismatch_err.vv:7:17: error: cannot use `int` as `string` in argument 1 to `strings.Builder.write_string`
5 | var := T{}
6 | ptr := &var
7 | b.write_string(*ptr)
| ~~~~
8 | }
9 |

View File

@ -0,0 +1,12 @@
import strings
fn my_fn[T]() {
mut b := strings.new_builder(16)
var := T{}
ptr := &var
b.write_string(*ptr)
}
fn main() {
my_fn[int]()
}

View File

@ -70,7 +70,7 @@ pub fn new_keywords_matcher_trie[T](kw_map map[string]T) KeywordsMatcherTrie {
km.nodes << &TrieNode(0)
}
for k, v in kw_map {
km.add_word(k, v)
km.add_word(k, int(v))
}
// dump(km.min_len)
// dump(km.max_len)

View File

@ -130,14 +130,14 @@ fn (e &Encoder) encode_value_with_level[T](val T, level int, mut wr io.Writer) !
e.encode_any(val, level, mut wr)!
} $else $if T is []Any {
e.encode_any(val, level, mut wr)!
} $else $if T in [Null, bool, $Float, $Int] {
e.encode_any(val, level, mut wr)!
} $else $if T is Encodable {
wr.write(val.json_str().bytes())!
} $else $if T is $Struct {
e.encode_struct(val, level, mut wr)!
} $else $if T is $Enum {
e.encode_any(Any(int(val)), level, mut wr)!
} $else $if T in [Null, bool, $Float, $Int] {
e.encode_any(val, level, mut wr)!
} $else {
// dump(val.str())
return error('cannot encode value with ${typeof(val).name} type')