mirror of
https://github.com/vlang/v.git
synced 2025-09-09 23:39:39 -04:00
checker: check generic fn call argument type mismatch (#17680)
This commit is contained in:
parent
b345d77805
commit
d349c1d86d
@ -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)}`',
|
c.error('literal argument cannot be passed as reference parameter `${c.table.type_to_str(param.typ)}`',
|
||||||
arg.pos)
|
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
|
// str method, allow type with str method if fn arg is string
|
||||||
// Passing an int or a string array produces a c error here
|
// Passing an int or a string array produces a c error here
|
||||||
// Deleting this condition results in propper V error messages
|
// Deleting this condition results in propper V error messages
|
||||||
|
@ -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 |
|
12
vlib/v/checker/tests/generic_fn_call_arg_mismatch_err.vv
Normal file
12
vlib/v/checker/tests/generic_fn_call_arg_mismatch_err.vv
Normal 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]()
|
||||||
|
}
|
@ -70,7 +70,7 @@ pub fn new_keywords_matcher_trie[T](kw_map map[string]T) KeywordsMatcherTrie {
|
|||||||
km.nodes << &TrieNode(0)
|
km.nodes << &TrieNode(0)
|
||||||
}
|
}
|
||||||
for k, v in kw_map {
|
for k, v in kw_map {
|
||||||
km.add_word(k, v)
|
km.add_word(k, int(v))
|
||||||
}
|
}
|
||||||
// dump(km.min_len)
|
// dump(km.min_len)
|
||||||
// dump(km.max_len)
|
// dump(km.max_len)
|
||||||
|
@ -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)!
|
e.encode_any(val, level, mut wr)!
|
||||||
} $else $if T is []Any {
|
} $else $if T is []Any {
|
||||||
e.encode_any(val, level, mut wr)!
|
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 {
|
} $else $if T is Encodable {
|
||||||
wr.write(val.json_str().bytes())!
|
wr.write(val.json_str().bytes())!
|
||||||
} $else $if T is $Struct {
|
} $else $if T is $Struct {
|
||||||
e.encode_struct(val, level, mut wr)!
|
e.encode_struct(val, level, mut wr)!
|
||||||
} $else $if T is $Enum {
|
} $else $if T is $Enum {
|
||||||
e.encode_any(Any(int(val)), level, mut wr)!
|
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 {
|
} $else {
|
||||||
// dump(val.str())
|
// dump(val.str())
|
||||||
return error('cannot encode value with ${typeof(val).name} type')
|
return error('cannot encode value with ${typeof(val).name} type')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user